How to create iteratively a vector of handle functions?

2 views (last 30 days)
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.

Answers (1)

Steven Lord
Steven Lord on 4 Oct 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 Comment
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.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!