How can I input an array into ode45?

조회 수: 5 (최근 30일)
Joshua Leong
Joshua Leong 2017년 10월 18일
편집: Walter Roberson 2017년 10월 18일
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?
  댓글 수: 1
KSSV
KSSV 2017년 10월 18일
Are you not okay with loop?

댓글을 달려면 로그인하십시오.

답변 (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