Solve an implicit function using fzero
7 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Walter Roberson
2017-6-26
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 个评论
Walter Roberson
2017-6-27
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surrogate Optimization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!