Info

This question is closed. Reopen it to edit or answer.

Need help with array of functions

1 view (last 30 days)
Stephen
Stephen on 15 May 2018
Closed: MATLAB Answer Bot on 20 Aug 2021
Hello all, I have a script that uses symbolic variables heavily. Here is the code,
syms x y z
as = 3*x;
bs = 4*y + z;
cs = x*z;
ds = x+y+z;
F_g = [as;bs;cs;ds];
sel = [1 3 4 2 4 3 2];
theResult_sym = F_g(sel)
xv = 1;
yv = 2;
zv = 3;
subs(theResult_sym, {x y z}, {xv yv zv})
What it does is
- First, all the symbolic equations are saved in a vector F_g
- According to the value specified in sel, pick the corresponding equations in F_g to evaluate.
It has come to my notice that "eval" is quite computationally expensive. I'd like to first convert the symbolic equations to function handles first. Then I will perform calculations. Here is my code
syms x y z
as = 3*x;
bs = 4*y + z;
cs = x*z;
ds = x+y+z;
F_g = [as ;bs;cs;ds];
ParaSet_sym = [x,y,z];
F_g_fun = matlabFunction(F_g, 'vars', ParaSet_sym);
sel = [1 3 4 2 4 3 2];
Does F_g_fun store an array of function handles? If yes, I wonder how do I access its elements? I can't access with () because values in it would be considered as function parameters input.
  1 Comment
Stephen23
Stephen23 on 15 May 2018
Edited: Stephen23 on 15 May 2018
"Does F_g_fun store an array of function handles?"
MATLAB does not support arrays of function handles: "A function handle is always scalar (1-by-1)."
It is possible to put any number of (scalar) function handles into a cell array, struct, or other container, but of course that is not the same thing as a function handle array.
Note that the matlabFunction describes its output as being a "Function handle that can serve as an input argument to numerical functions, returned as a MATLAB function handle." It clearly only mentions one function handle, and uses the singular everywhere.

Answers (0)

This question is closed.

Community Treasure Hunt

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

Start Hunting!