Create an array of n function handle

조회 수: 1 (최근 30일)
Felix Lauwaert
Felix Lauwaert 2016년 5월 12일
댓글: Guillaume 2016년 5월 13일
Hello,
How can I create a set of ode equations?
Example:
fun1 = @(t,x,k) x-k;
I want to generate a function of n entries like:
funN = @(t,x,k) [ x(1)-k
x(2)-k
...
x(n)-k ];
I want to run the program for different values of n, so I can't create the full function handle bu hand.
Thanks.

답변 (1개)

Guillaume
Guillaume 2016년 5월 12일
편집: Guillaume 2016년 5월 12일
I'm not sure why your anonymous functions take a t input that they don't use. Anyway:
funN = @(~, x, k, N) reshape(x(1:N) - k, [], 1);
and to create a cell array of them:
N = 10; %for example
allfuns = arrayfun(@(n) @(t, x, k) funN(t, x, k, n), 1:N, 'UniformOutput', false);
  댓글 수: 2
Felix Lauwaert
Felix Lauwaert 2016년 5월 12일
t is necessary to solve odes with ode45, even if it's an autonomous one.
Unfortunately, I fail at undersanding your solution. Do I have to write all three lines one under the other?
If I do so, I get this error:
Undefined function 'exist' for input arguments
of type 'cell'.
Error in odearguments (line 59)
if (exist(ode)==2)
Thanks.
Guillaume
Guillaume 2016년 5월 13일
I answered the title of your question (how to create an array of n functions handles) but I missed the subquestion (how can I create a set of ODE equations).
As far as I know, you cannot pass an array of function handles to the ODE solvers, you have to give them a single function to solve. Hence, why you're getting the error. The ODE did not expect a cell array.
However, I'm unclear on exactly what you want to solve. To solve for one n value, you'd use only one of the allfuns function:
n = 5; %for example
ode45(allfuns{5}, ...)
%or without bothering with the cell array:
ode45(@(t, x, k) funN(t, x, k, n), ...)

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

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by