Clear Filters
Clear Filters

help with fzero function

1 view (last 30 days)
Ziqiang Gao
Ziqiang Gao on 5 Apr 2016
Commented: John D'Errico on 5 Apr 2016
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.

Answers (1)

Star Strider
Star Strider on 5 Apr 2016
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 Comment
John D'Errico
John D'Errico on 5 Apr 2016
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.

Sign in to comment.

Categories

Find more on Optimization in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!