Evaluating a cell array of function handles
2 ビュー (過去 30 日間)
古いコメントを表示
In the following code I assemble a sequence of polynomials (the Chebyshev polynomials, to be exact) symbolically, and then use matlabFunction() to convert each symbolic expression into a function handle. Each function handle is then placed in the cell array.
N = 10;
C = cell(N,1);
syms y;
T = sym(zeros(N,1));
T(1) = 1;
T(2) = y;
C{1} = matlabFunction(T(1));
C{2} = matlabFunction(T(2));
for n = 2:1:N-1
T(n+1) = 2*y*T(n) - T(n-1);
C{n+1} = matlabFunction(T(n+1));
end
evalArray = @(y)cellfun(@(f)f(y),C);
evalArray(3)
However, evalArray(3), which is intended to evaluate each polynomial at y=3, gives the error: "Error using symengine>@()1.0;Too many input arguments."
Any suggestions? Thanks all for your help.
0 件のコメント
採用された回答
Matt Fig
2012 年 10 月 9 日
編集済み: Matt Fig
2012 年 10 月 9 日
Exactly as you have made it, C{1} is a function that takes no input arguments.
Try this instead:
C{1} = matlabFunction(T(1),'vars',y);
2 件のコメント
Walter Roberson
2012 年 10 月 9 日
Right. By default matlabFunction searches for variables in the symbolic code and uses that many variables as input; since "1" is constant (0 variables), that is 0 inputs; Matt's solution shows how to force one input.
Matt Fig
2012 年 10 月 9 日
編集済み: Matt Fig
2012 年 10 月 9 日
matlabFunction is even smarter than that!
T(1) = 1 + 0*y; % Fails to produce f = f(y)
T(1) = y - 2 + 3 - y; % Also fails!
I tried these and other ways of "tricking" the conversion, without success. Finally, I checked the doc for matlabFunction... d'oh.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Number Theory についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!