Substituting code into function handle

조회 수: 3 (최근 30일)
Matt
Matt 2012년 6월 20일
Hi all. I have a variable myFormula that is a mathematical function of three symbolic variables, a, b, and c. For example, it may be the case that
myFormula = a^2 + b - c/2;
I need to pass this expression into a function handle (I think that is the term, at least), such that my desired result is
thisFunction = @(a,b,c) a^2 + b - c/2;
The problem is that, although myFormula always depends on a, b, and c, the relationship between these variables is arbitrary. To get around this, I have found that
thisFunction = @(a,b,c) subs(myFormula);
is a viable solution. However, thisFunction needs to be passed into ode45 (which constitutes many iterations), which causes subs() to be evaluated thousands of times, making things extremely slow. It seems like there should be a simple solution to this - any suggestions?

채택된 답변

Walter Roberson
Walter Roberson 2012년 6월 20일
If your formula is in the form of a symbolic expression, as is implied by your use of subs(), then use matlabFunction() to convert the expression into a function handle.
  댓글 수: 2
Matt
Matt 2012년 6월 20일
Excellent, thanks Walter. Out of curiosity, are there any alternatives to matlabFunction() that I could look into for this situation?
Walter Roberson
Walter Roberson 2012년 6월 20일
Not really, none that would not pretty much end up doing what matlabFunction() does.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by