# Fast ODE45 with for-loops

26 ビュー (過去 30 日間)
Paul 2013 年 8 月 23 日
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.

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

### 採用された回答

Marc 2013 年 8 月 24 日
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.

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

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

Paul 2013 年 8 月 24 日
Sorry about : I posted this late last night and honestly cannot remember posting twice. How rude of me :)
My problem is non-stiff and no particularly large. My overall task is to invert a characteristic function via direct integration. So if the grid in the frequency domain in length(2^N) then this is the number of times I need to simulate the system above.
It turns out that I need 25,000 + odes to recover the density accurately. I need between 200 - 1000 seconds depending. Funny you say about profiling the function not the script - I was doing EXACTLY the same thing.
I will try parfor thanks. Otherwise did you consider writing a fortan / C MEX file for integrating the ODES?
##### 1 件のコメント表示非表示 なし
Marc 2013 年 8 月 25 日
I have compared limex in fortran with ode15s and cvode from Sundials in Matlab and find Matlab to be close to fortran. I did not try to optimize the fortran code, simply tested as is.
You already have available what you are asking. cvode and ida codes, part of the Sundials package have already been "mexed" and packaged for Matlab. This is from Lawrence Livermore National lab. Cvode has some nice features and requires you to format things a bit differently. Not sure if you'll get a speed up.
Limex, lsode, cvode, ida are codes I have compared to ode45 and ode15s over the years and found the Matlab codes to hold up.
So, I think you are stuck from the integrator standpoint.
My only comment would be try running your parameter maybe 5 times, say at the min, max and three intermediate points to see if you can minimize that 25,000 runs. Maybe there is something special about your system, but I tend to believe if you pick say 32 points and randomize the runs, you should get a good enough picture of what is going on.

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

### Community Treasure Hunt

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

Start Hunting!

Translated by