Evaluating anonymous/symbolic array function

3 ビュー (過去 30 日間)
Navinder Singh
Navinder Singh 2021 年 12 月 22 日
コメント済み: Walter Roberson 2021 年 12 月 24 日
I use symbolic and anonymous array functions interchangeably using "matlabFunction". I wish to extract the function value at discrete points using array. The issue comes when any of the entries is not a function of the independent variables.
Usually, script A gives error: Dimensions of arrays being concatenated are not consistent. So, I use the workaround in script B that solves the error and gives the correct output. But when converting from a symbolic function as shown in script C, this error could not be avoided. I know we could use "subs" as in script D, but its often slow as the actual function (fx) in my case is computationally tasking to be evaluated this way. Currently the way I have implemented it as - I am cheking each entry of symbolic array function and if its a constant, then get the corresponding values using a simple multiplication as show in script E. I wish to ask if there is a better/efficient way of handling this
%---------------------
% Script A
fun = @(x) [1;x]
value = fun(1:5)
%---------------------
% Script B
fun = @(x) [x.^0;x];
value = fun(1:5)
%---------------------
% Script C
syms x
fx = [1;x]
fun1 = matlabFunction(fx)
value = fun1(1:5)
%---------------------
% Script D
syms x
fx = [1;x]
value = subs(fx,x,1:5)
%---------------------
% Script E
arr = 1:5;
for i=1:length(fx)
if length(symvar(fx(i)))<1
value(:,i) = double(fx(i))*arr.^0;
else
fun2 = matlabFunction(fx(i));
value(:,i) = fun2(arr);
end
end
disp(value)

採用された回答

Walter Roberson
Walter Roberson 2021 年 12 月 22 日
fun1 = matlabFunction(fx)
Do not do that. Instead,
fun1 = matlabFunction(fx, 'vars', x);
  8 件のコメント
Walter Roberson
Walter Roberson 2021 年 12 月 24 日
It is a challenge :(
Walter Roberson
Walter Roberson 2021 年 12 月 24 日
I filed an enhancement request. I do not expect a real solution any time soon.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeConversion Between Symbolic and Numeric についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by