Create an array of n function handle
조회 수: 1 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
답변 (1개)
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
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 Center 및 File Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!