Borrar filtros
Borrar filtros

Create an array of n function handle

2 visualizaciones (últimos 30 días)
Felix Lauwaert
Felix Lauwaert el 12 de Mayo de 2016
Comentada: Guillaume el 13 de Mayo de 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.

Respuestas (1)

Guillaume
Guillaume el 12 de Mayo de 2016
Editada: Guillaume el 12 de Mayo de 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 comentarios
Felix Lauwaert
Felix Lauwaert el 12 de Mayo de 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 el 13 de Mayo de 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), ...)

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by