How can I input an array into ode45?

9 visualizaciones (últimos 30 días)
Joshua Leong
Joshua Leong el 18 de Oct. de 2017
Editada: Walter Roberson el 18 de Oct. de 2017
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?

Respuestas (2)

Walter Roberson
Walter Roberson el 18 de Oct. de 2017
Editada: Walter Roberson el 18 de Oct. de 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);

Nicolas Schmit
Nicolas Schmit el 18 de Oct. de 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.

Categorías

Más información sobre Ordinary Differential Equations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by