Substituting code into function handle
3 ビュー (過去 30 日間)
古いコメントを表示
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?
0 件のコメント
採用された回答
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 件のコメント
Walter Roberson
2012 年 6 月 20 日
Not really, none that would not pretty much end up doing what matlabFunction() does.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Symbolic Math Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!