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.

采纳的回答

Walter Roberson
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 个评论
Charmayne Floyd
Charmayne Floyd 2017-6-26
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 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 CenterFile Exchange 中查找有关 Surrogate Optimization 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by