Effacer les filtres
Effacer les filtres

Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

Need help with array of functions

1 vue (au cours des 30 derniers jours)
Stephen
Stephen le 15 Mai 2018
Clôturé : MATLAB Answer Bot le 20 Août 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 commentaire
Stephen23
Stephen23 le 15 Mai 2018
Modifié(e) : Stephen23 le 15 Mai 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.

Réponses (0)

Cette question est clôturée.

Community Treasure Hunt

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

Start Hunting!

Translated by