help with fzero function
1 次查看(过去 30 天)
显示 更早的评论
Hi,I don't know why this is not correct. Could you please help me to modify it.
phi=3+t1;
beta=2*t1;
fun=@(t1) phi-beta;
x0=[1 9];
z=fzero(fun,x0);
disp(z)
----------------
and the command window shows
Error using fzero (line 242)
Function values at interval endpoints must be finite and real.
Error in Untitled (line 5)
z=fzero(fun,x0);
Please help. Thank you very much. If you can make it correct, I will really appreciate this.
0 个评论
回答(1 个)
Star Strider
2016-4-5
You have to make anonymous functions out of ‘phi’ and ‘beta’, and then refer to them as functions in the fzero call:
phi = @(t1) 3+t1;
beta = @(t1) 2*t1;
fun=@(t1) phi(t1)-beta(t1);
x0=[1 9];
z=fzero(fun,x0);
disp(z)
1 个评论
John D'Errico
2016-4-5
Think of it like this. As you wrote it, phi is NOT a function. It is a simple variable that takes on some known value, as long as t1 is already defined.
phi=3+t1;
beta=2*t1;
So, whatever t1 was at that point, phi is just a number, NOT a function. Had t1 not been defined before that point, MATLAB would have generated an error, telling you that t1 was undefined.
As Star points out, you can explicitly define phi and beta as functions of some parameter (call it t1), and then use them in the function fun.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calculus 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!