How to create iteratively a vector of handle functions?

4 visualizzazioni (ultimi 30 giorni)
Hi! I want to create iteratively a vector of handle functions, for example:
If i have a large sequence of functions (sin(x),x^2,x^3,...,x^5), how can i create the vector of handle functions f(x) such that:
f= @(x) [sin(x);...
x^2;...
x^3;...
...;...
x^5] ?
The functions are most complicated in my aplication and could be diferent depending on some conditions, i.e., the secound row could be x^2 or cos(x), that is the reason why this porcess must be iterative. By last, I will use fsolve therefore that it is not possible applying cells. Thanks a lot for your answer.

Risposte (1)

Steven Lord
Steven Lord il 4 Ott 2021
c = {@sin; @cos; @tan};
f1 = @(x) cellfun(@(fh) fh(x), c); % Assuming the outputs are all scalar
y1 = f1(pi/4)
y1 = 3×1
0.7071 0.7071 1.0000
f2 = @(x) cellfun(@(fh) fh(x), c, 'UniformOutput', false); % If the outputs are not scalar
y2 = f2(0:0.25:1)
y2 = 3×1 cell array
{[0 0.2474 0.4794 0.6816 0.8415]} {[1 0.9689 0.8776 0.7317 0.5403]} {[0 0.2553 0.5463 0.9316 1.5574]}
  1 Commento
Edwin David Erazo Caicedo
Thanks a lot for your answer. I had to use a MatlabFunction to solve the problem but your sugerence is very interesting.

Accedi per commentare.

Categorie

Scopri di più su Entering Commands in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by