How can I input an array into ode45?
6 ビュー (過去 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
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);
0 件のコメント
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.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Ordinary Differential Equations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!