MATLAB: Fast ODE45 with for-loops

for loopode45speed

Hello,
I have a coupled first order ODE to solve of the form
dy = f(t, x) dx = d(f, x)
which depends on a common parameter which I have to solve for over some range. I can solve the system using ODE45 and a for loops which increments the parameter of interest at each loop, however, this is very slow. Can I avoid solving this problem in loops? Or what is best practice with problems of this type.

Best Answer

The infamous double post.... Asked in Newsgroup and answered. Try parfor in the parallel toolbox. If your computer has multicores, you can run 2+ simulations at the same time.
Should help.
Just as a follow up, how long does running ODE45 take? Are we talking seconds or minutes?
Is your function large? I run a simulation with ~3000 odes, using ode15s and this takes between 100-1000 seconds depending on the input parameters. This was a 10x speed up after I used the profiler on my function, NOT on the whole script. Found that I was spending a lot of time passing large structures between functions within the main function. Mostly because I was lazy. Trimmed this up and got much better results.
Add parfor with 12 cores and I was able to get through my DOEs in days rather than weeks.
Related Question