Evaluating anonymous/symbolic array function

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)

 Accepted Answer

fun1 = matlabFunction(fx)
Do not do that. Instead,
fun1 = matlabFunction(fx, 'vars', x);

8 Comments

syms x
fx = [2]; %not dependant on x
fun1 = matlabFunction(fx, 'vars', x)
fun1 = function_handle with value:
@(x)2.0
fun1(ones(1,4))
ans = 2
This will at least avoid the problem of "too many input arguments" that would otherwise be generated.
Unfortunately, it does not solve the problem that the size of the output does not match the size of the input
syms x real
fx = abs(x) - sqrt(x^2) + 2 %not actually dependant on x
fx = 
fun1 = matlabFunction(fx, 'vars', x)
fun1 = function_handle with value:
@(x)abs(x)-sqrt(x.^2)+2.0
fun1([-3 -1 0 1 3])
ans = 1×5
2 2 2 2 2
This particular work-around using abs() is not valid for imaginary inputs.
Thanks for your help.
fun1 = matlabFunction(fx, 'vars', x)
It does solves one problem for sure.
However, the second problem (output size not matching input) couldnot be solved using the workaround suggested as it would create issues when the function is differentiated or integrated.
In this instance, the 'dfx' and 'dfun' has singularity at x=0 because of the additional terms in fx.
syms x real
fx = abs(x) - sqrt((x)^2) + 2; %not actually dependant on x
fun = matlabFunction(fx,'vars','x');
dfx = diff(fun(x));
dfun = matlabFunction(dfx);
subs(dfx,x,-4:4)
dfun(-4:4)
There are other possible identities you could use. But you would need one robust enough to survive differentiation.
Not exactly the below, because the below fails for x integer 1 or less.
syms x
fx = gamma(x) - (x-1)*gamma(x-1) + 2
fx = 
Fx = matlabFunction(fx, 'vars', x)
Fx = function_handle with value:
@(x)gamma(x)-gamma(x-1.0).*(x-1.0)+2.0
Fx(1:5)
ans = 1×5
NaN 2 2 2 2
df = diff(fx,x)
df = 
dF = matlabFunction(df, 'vars', x)
dF = function_handle with value:
@(x)-gamma(x-1.0)+gamma(x).*psi(x)-gamma(x-1.0).*psi(x-1.0).*(x-1.0)
dF(1:5)
ans = 1×5
1.0e+-14 * NaN -0.0222 0 -0.0888 0.3553
This one seems to work. Well, in the +/- 300 range anyhow. You could try reversing log and exp for more range.
syms x
fx = cos(log(exp(x))) - cos(x) + 2
fx = 
Fx = matlabFunction(fx, 'vars', x)
Fx = function_handle with value:
@(x)cos(log(exp(x)))-cos(x)+2.0
Fx(-4:4)
ans = 1×9
2 2 2 2 2 2 2 2 2
df = diff(fx,x)
df = 
dF = matlabFunction(df, 'vars', x)
dF = function_handle with value:
@(x)-sin(log(exp(x)))+sin(x)
dF(-4:4)
ans = 1×9
0 0 0 0 0 0 0 0 0
TBH, its not elegant looking, but the workaround definitely solves all the issue I had. Though, I still think why one should need such workaround in the first place.
Anyways, great help! Thanks again!
I filed an enhancement request. I do not expect a real solution any time soon.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!