How can I create a function with multiple variables?

조회 수: 740 (최근 30일)
Vipin Kumar Yadav
Vipin Kumar Yadav 2019년 12월 6일
편집: Image Analyst 2021년 4월 24일
hello,
I am really novice with MATLAB and I am having a hard time writing this code.
I have to make a function in this format :
y= a(1) + (a(2)/z) + (a(3)*x) + (a(4)*x^2) + ((a(5)*x)/z) + ((a(6)*x^2)/z)
where
y=function(x,z) ###### x,y,z can be taken from database and some values are mentioned below.)
% and a(1), a(2), a(3), a(4), a(5), a(6) are constant that needed to be defined
x=[0.1 0.2 0.3 0.4 0.5 0.6 0.65 0.7];
y=[1 3 5 7 15 50 1 85];
z=[313 313 313 313 313 313 313 313]
Any suggestion will be of great help.
Thanks

답변 (2개)

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH 2019년 12월 6일
y=@(a,x,z) a(1) + (a(2)./z) + (a(3).*x) + (a(4).*x.^2) + ((a(5).*x)./z) + ((a(6).*x.^2)./z);
x=[0.1 0.2 0.3 0.4 0.5 0.6 0.65 0.7];
a=[1 2 3 6 4 1];%put your values here
z=[313 313 313 313 313 313 313 313]
returnfunction=y(a,x,z)

Image Analyst
Image Analyst 2019년 12월 7일
Try this:
function y = yourFunctionName(x, z)
% x,y,z can be taken from database and some values are mentioned below.)
a = .... % Whatever you have to do do get a.
% a(1), a(2), a(3), a(4), a(5), a(6) are constant that needed to be defined
y= a(1) + (a(2)/z) + (a(3)*x) + (a(4)*x^2) + ((a(5)*x)/z) + ((a(6)*x^2)/z)
You'll need to figure out how to get the "a" values, and need to figure out how to call the function (by which I mean somehow you need to assign the x and x inputs. Save this in yourFunctionName.m or use whatever name you want for the function just make sure it's the same on the function line as the name of the m file is.

카테고리

Help CenterFile Exchange에서 Graphics Object Identification에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by