Filter löschen
Filter löschen

Create an array of n function handle

2 Ansichten (letzte 30 Tage)
Felix Lauwaert
Felix Lauwaert am 12 Mai 2016
Kommentiert: Guillaume am 13 Mai 2016
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.

Antworten (1)

Guillaume
Guillaume am 12 Mai 2016
Bearbeitet: Guillaume am 12 Mai 2016
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 Kommentare
Felix Lauwaert
Felix Lauwaert am 12 Mai 2016
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 am 13 Mai 2016
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), ...)

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by