Solving vectorized ODE (Solving same ODE with many initial conditions at once).
이전 댓글 표시
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개)
Jan
2021년 3월 17일
1 개 추천
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
Yi-xiao Liu
2021년 3월 17일
Jan
2021년 3월 18일
The error can be small, if the trajectory is not very sensitive for a variation of the initial values. If you integrate a chaotic function like a double pendulum, the error will get much larger.
You can improve the performance by choosing another integrator or my modifying the tolerances. In your case the function to be uintegrated is tiny. Therefore saving function calls is not the standard way to improve the speed. If this function contains thousands of lines of code, the situation would be different.
Yi-xiao Liu
2021년 3월 18일
Jan
2021년 3월 18일
Do you have the Parallel Processing Toolbox? Then try to replace the FOR by PARFOR.
카테고리
도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!