# Solving vectorized ODE (Solving same ODE with many initial conditions at once).

8 ビュー (過去 30 日間)
Yi-xiao Liu 2021 年 3 月 17 日
コメント済み: Yi-xiao Liu 2021 年 3 月 21 日
I am tring to apply 2nd newton law to many points, where m,r,v,f is a n-by-1, n-by-3, n-by-3, n-by-3, n-by-3, matrixes for mass, position,velocity, and force for n points. The 3 columns are the x,y,z components for those values.
options = odeset('Vectorized','on');
y0=[r,v];
[t,YSol]=ode45(@(t,y) MotionODE(t,y,m,f),[0,dt],y0,options);
r=YSol(t==dt,1:3)';
v=YSol(t==dt,4:5)';
function d2rdt2= MotionODE(t,Y,m,f)
%ODE function for motion
r=Y(:,1:3);
v=Y(:,4:5);
drdt=v;
dvdt=f./m;
d2rdt2=[drdt,dvdt];
end
However I got an error: "Index in position 2 exceeds array bounds (must not exceed 1).". Is the option "vectorized" designed for what I think it is for? How do I get this run correctly (other than a for loop)?
##### 1 件のコメント表示非表示 なし
Yi-xiao Liu 2021 年 3 月 21 日
Walter's answer was accepted because it answers my original question. But Jan's answer is also informative regarding accuracy of this approach and I encourge everyone to give a read.
I also found this link to be revelant:

サインインしてコメントする。

### 採用された回答

Walter Roberson 2021 年 3 月 17 日
r = Y(1,:);
v = Y(2,:);
drdt = v;
dvdt = f./m * ones(size(v));
d2rdt2=[drdt;dvdt];
##### 3 件のコメント表示非表示 2 件の古いコメント
Yi-xiao Liu 2021 年 3 月 17 日
it should be v=Y(:,4:6) in the ODE function, which is the mistake I originally made in the question.
Other than that it works great, Thanks!

サインインしてコメントする。

### その他の回答 (1 件)

Jan 2021 年 3 月 17 日
same ODE with many initial conditions at once
This is a bad idea. Remember that the step size control is triggered by the most susceptible component of the trajectory. This reduces the stepsize for all components and in consequence increases the accumulated rounding errors. The total number of calculations can be larger than running the integration for each initial value separately.
##### 4 件のコメント表示非表示 3 件の古いコメント
Jan 2021 年 3 月 18 日
Do you have the Parallel Processing Toolbox? Then try to replace the FOR by PARFOR.

サインインしてコメントする。

### カテゴリ

Find more on Programming in Help Center and File Exchange

R2019b

### Community Treasure Hunt

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

Start Hunting!

Translated by