help with fzero function

1 次查看(过去 30 天)
Ziqiang Gao
Ziqiang Gao 2016-4-5
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.

回答(1 个)

Star Strider
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
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 CenterFile Exchange 中查找有关 Optimization 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by