MATLAB: ODE Solver Running Very “Slowly”

ode solverode solver slowslow issue

Hello,
I am using an ODE solver inside a for loop to run trajectories of particles in a magnetic field, however whenever I run the code it takes the ODE solver a very long time to solve these trajecotries. On average about 3-7 seconds in each iteration. This for loop is repeated up to a 100 times and I also want to do a double for loop so there are multiple particles, so as you can see, that time adds up quickly. My issue previously is that when inputed initial conditions into the ODE solver with the following format:
icv = [x; y; z; vx; vy; vz]; %Initial conditions defined earlier within for loop

tspan = [0 4]; %Time span

[T,S] = ode15s(@bdipuniodefun, tspan, icv); %Trajectory solver

It would be hardstuck on a single iteration. I believe this is because the ODE solver is attempting to integrate over many, discrete points within the time span which slows it down tremendously. I tried fixing this issue by defining a tiertiary time step inbetween the initial and final so that it forces larger discretization:
icv = [x; y; z; vx; vy; vz]; %Initial conditions defined earlier within for loop
tspan = [0 2 4]; %Time span
[T,S] = ode15s(@bdipuniodefun, tspan, icv); %Trajectory solver
Although this does help a lot (Computing time decreased by 100x) this is still extremely slow. Any ideas why this ODE solver is slow? Or is my formatting for the initial conditions flawed so that it makes it run slow?
PS – I need to use this ODE solver within a for loop, because there is an additional process (Collisions between the particle and other particles) that needs to be solved at each time step, therfore this solver must be within the for loop. I just defined my time step to be equivalent to the time span in "tspan". If you want to see more code, I can also post more for better context.

Best Answer

Hi,
it is a very bad idea what happens with B_test.m while the solver runs:
In every call of the ode function by the solver B_test is called and does the same calculation with the same result. Since B_test has no input arguments you should calculate Bx By and Bz once at the beginning. Do the symbolic computation once (in a seperate matlab session) and use the resulting function handles to calculate Bx By Bz. Do it one time only and give the calculated results as extra parameters to the ode function. They should be workspace variables that are calculated one time and then be used. using symbolic calculations in this way again and again repeated is a huge waste of time, since symbolic calculations are slow. in your case they are slow and not needed. change this and you will get much faster results.
Also do not think that you have an influence on how many time step the solver makess during the calculation. using
tspan = [0 2 4]
does not mean that you save time. it means that you get three points as result back - not that the solver calculates only 3 results.
Best regards
Stephan
Related Question