How can I input an array into ode45?
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am trying to solve a 2nd order ODE using ODE45. I would like to input an array of different initial conditions such that it would output an array of solutions. How can I do so?
Réponses (2)
Walter Roberson
le 18 Oct 2017
Modifié(e) : Walter Roberson
le 18 Oct 2017
Assuming the initial conditions are columns of a matrix IC:
[T_vals, Y_vals] = arrayfun( @(idx) ode45(@odefunction, tspan, IC(:,IDX)), 1:size(IC,2), 'uniform', 0);
T_vals and Y_vals will be cell arrays. If your tspan is two elements instead of a vector of length 3, then you could have a different T_vals vector for every case; if your tspan is a vector of at least 3 elements, then the T_vals entries should be the same for each case unless the ode45 gives up because of a singularity or an event function.
This is a hidden loop; MATLAB will do the loop inside of arrayfun.
There is a more compact form for the special case where the initial condition is a scalar for each call:
[T_vals, Y_vals] = arrayfun( @(ic) ode45(@odefunction, tspan, ic), IC, 'uniform', 0);
0 commentaires
Nicolas Schmit
le 18 Oct 2017
You cannot directly input an array of initial conditions to ode45. It might be possible to define a function handle solveOne(y0) = @(y0) ode45(..., ..., y0) and apply this function to your array of initial conditions using arrayfun(solveOne, ...). However, you would not get better performances than with a for loop. Furthermore, the for loop can be easily replaced by a parfor if you have the Parallel Computing Toolbox.
0 commentaires
Voir également
Catégories
En savoir plus sur Ordinary Differential Equations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!