How to create a fzero loop to find roots of a function file?

I'm going to simplify my equation to make it easier, however I have a .m function file, lets say:
if true
function Y=Y(x)
%variables
a=1;
y=2;
b=2;
c=3;
Y=a*x+b*y+c*xy;
end
I can use the command:
if true
fzero(@Y,2)
end
to find the root of this equation. and it works, however my question is how to write a loop to solve this equation for varying values of y. so my .m function is now of two variables:
if true
function Y=Y(x,y)
%variables
a=1;
b=2;
c=3;
Y=a*x+b*y+c*xy;
end
end
I need to now write a loop to find the zero value of x, for many values of y. Here is what I have tried to do but cannot get it to work:
if true
for x=2;
y=2:0.1:10;
x = fzero(Y(w,r),4);
end
I'm fairly new to matlab and have tried searching the forums for help but cannot find a solution. So you help will be greatly appreciated.
Kind Regards,
John.

 採用された回答

Amit
Amit 2014 年 1 月 21 日
編集済み: Amit 2014 年 1 月 21 日

0 投票

for y= 2:0.1:10
x = fzero(@(m) Y(m,y),4);
end

5 件のコメント

Amit
Amit 2014 年 1 月 21 日
if you wanna store the value of x on the way
y = 2:0.1:10;
x = zeros(1,numel(y));
for j = 1:numel(y)
x(j) = fzero(@(m) Y(m,y(j)),4);
end
John Doe
John Doe 2014 年 1 月 21 日
Hi Amit,
Thank you very much for your reply this works.
John Doe
John Doe 2014 年 1 月 21 日
But I have one question, what does the m represent in this code? why m and not x?
Amit
Amit 2014 年 1 月 21 日
@(m) Y(m,y) says that m is the variable. Its like saying if there is a difference in y = f(x) or y = f(m). It can be any variable name you like. I did not chose x, because x was used for data storage and didn't wanted to get confused.
John Doe
John Doe 2014 年 1 月 21 日
I see, thanks.
One final question that I just realized, do you know how to then save all these values? as each time it loops it overwrites the previous zero.

サインインしてコメントする。

その他の回答 (0 件)

タグ

タグが未入力です。

質問済み:

2014 年 1 月 21 日

コメント済み:

2014 年 1 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by