Effacer les filtres
Effacer les filtres

help with fzero function

1 vue (au cours des 30 derniers jours)
Ziqiang Gao
Ziqiang Gao le 5 Avr 2016
Commenté : John D'Errico le 5 Avr 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.

Réponses (1)

Star Strider
Star Strider le 5 Avr 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 commentaire
John D'Errico
John D'Errico le 5 Avr 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Optimization dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by