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!