Effacer les filtres
Effacer les filtres

Create an array of n function handle

2 vues (au cours des 30 derniers jours)
Felix Lauwaert
Felix Lauwaert le 12 Mai 2016
Commenté : Guillaume le 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.

Réponses (1)

Guillaume
Guillaume le 12 Mai 2016
Modifié(e) : Guillaume le 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 commentaires
Felix Lauwaert
Felix Lauwaert le 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 le 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), ...)

Connectez-vous pour commenter.

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by