How can I input an array into ode45?

9 次查看(过去 30 天)
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?

回答(2 个)

Walter Roberson
Walter Roberson 2017-10-18
编辑:Walter Roberson 2017-10-18
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 2017-10-18
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.

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by