How to create iteratively a vector of handle functions?

조회 수: 2 (최근 30일)
Edwin David Erazo Caicedo
Edwin David Erazo Caicedo 2021년 10월 4일
댓글: Edwin David Erazo Caicedo 2021년 10월 7일
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.

답변 (1개)

Steven Lord
Steven Lord 2021년 10월 4일
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
Edwin David Erazo Caicedo
Edwin David Erazo Caicedo 2021년 10월 7일
Thanks a lot for your answer. I had to use a MatlabFunction to solve the problem but your sugerence is very interesting.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by