How can I create a function with multiple variables?

704 views (last 30 days)
Vipin Kumar Yadav
Vipin Kumar Yadav on 6 Dec 2019
Edited: Image Analyst on 24 Apr 2021
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

Answers (2)

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH on 6 Dec 2019
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 on 7 Dec 2019
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.

Community Treasure Hunt

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

Start Hunting!

Translated by