Solve an implicit function using fzero

7 visualizaciones (últimos 30 días)
Charmayne Floyd
Charmayne Floyd el 26 de Jun. de 2017
Comentada: Walter Roberson el 27 de Jun. de 2017
I have a function R=(1-exp(-X*T))/(1-exp(T)), where I know the values of R and x and can define them in Matlab. I can't seem to figure out, however, how to write code to solve for T in Matlab using fzero without encountering a parenthesis error.
I have, in one file:
function F=findt(R,X)
F=(1-exp(-X*T))/(1-exp(-T))-R
and in a different file,
R=2.2;
X=40;
T=fzero(@findt(R,X),0)
When I run the second file, it says that I have an unbalanced or unexpected parenthesis, but I can't find where. I've gone through the documentation and I think there is something I'm not understanding about how to properly call a function and the parentheses surrounding that. Would it be better to put this as a nested function so I don't have to call it (the second file would become the parent function), and if so, how do I do that? Thank you for your help.

Respuesta aceptada

Walter Roberson
Walter Roberson el 26 de Jun. de 2017
function F=findt(T, R,X)
F = (1-exp(-X*T))/(1-exp(-T))-R
and
R=2.2;
X=40;
T = fzero(@(t) findt(t, R, X), 0)
  2 comentarios
Charmayne Floyd
Charmayne Floyd el 26 de Jun. de 2017
Thank you! It works perfectly. Just for reference, the Ts in the last line I think are supposed to be capitalized and I changed the 0 to 0.0001 because I realized the function is undefined at 0.
Walter Roberson
Walter Roberson el 27 de Jun. de 2017
The lower-case t on the last line are fine as they are. They are parameter names for the anonymous function, and it is perfectly fine that they do not match any name anywhere else. The situation is exactly the same as if you had coded
function F = findt(bananorange, SpinningBox, GoGo_Boots)
F = (1-exp(-GoGo_Boots*bananorange))/(1-exp(-bananorange))-SpinningBox;
with
R=2.2;
X=40;
T = fzero(@(t) findt(t, R, X), 0);
in that the names of variables or expressions outside of a function have no effect on the names inside the function: everything is passed by position.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Surrogate Optimization en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by