How can I create a function with multiple variables?

707 ビュー (過去 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 Center および File ExchangeGraphics Object Identification についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by