Solve an implicit function using fzero
7 views (last 30 days)
Show older comments
Charmayne Floyd
on 26 Jun 2017
Commented: Walter Roberson
on 27 Jun 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.
0 Comments
Accepted Answer
Walter Roberson
on 26 Jun 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 Comments
Walter Roberson
on 27 Jun 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.
More Answers (0)
See Also
Categories
Find more on Surrogate Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!