View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from

Be the first to rate this file! 3 Downloads (last 30 days) File Size: 8 KB File ID: #31265 Version: 1.2




01 May 2011 (Updated )

Splits evaluation of a system of differential equations into chunks to avoid out of memory errors

| Watch this File

File Information

Occasionally I have had to solve large systems of differential equations which result in matlab running out of memory during evaluation. Odesplit breaks up the simulation period into a series of chunks to ease this problem. At each split point in the evaluation a function specified by the user is called with the current set of results to do whatever is necessary with the ouput. The next section is then evaluated using the final value of the previous section as the new starting point.

This behaviour differs from matlab's odextend as it allows you operate only on small sections of the solution, whereas odeextend always returns the full solution from the original start time to the specified final time. With odesplit, you could also choose to extract only particular solution components of interest, but which depend on the full solution, saving more resources.


Create the ode function described in Example 1 of the documentation for
ode45 (doc ode45)

  function dy = rigid(t,y)
      dy = zeros(3,1); % a column vector
      dy(1) = y(2) * y(3);
      dy(2) = -y(1) * y(3);
      dy(3) = -0.51 * y(1) * y(2);

Create a function to be called at each split point

   function results = rigidspfcn(results, sol)
       if nargin == 0
           results.iters = 0;
           save(['rigidodesave_', int2str(results.iters), '.mat'], 'sol');
           results.iters = results.iters + 1;

Set the options etc. and call odesplit, splitting the computation into
three blocks

  odeoptions = odeset('RelTol',1e-4,'AbsTol',[1e-4 1e-4 1e-5]);

  odefcn = @ode45;
  odeevfcn = @rigid;
  tspan = [0, 30];
  y0 = [0 1 1];
  spfcn = @rigidspfcn;

  [results, tspan] = odesplit(odefcn, odeevfcn, tspan, ...
                    y0, odeoptions, spfcn, 'Blocks', 3)

In this case, odesplit evaluate the ode and saves the results of each section to disk. However, we could have done anything we wished with this section of data, such as appending solution components of interest to 'results'. The 'results' variable is maintained throughout the calculation in odesplit and returned at the end.

If you ask it to, odesplit will also look out for an out-of-memory error and restart the calculation with a greater number of split points.

MATLAB release MATLAB 7.7 (R2008b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (4)
03 May 2011 Richard Crozier

@Mukhtar, I'll update the description as you suggest.

Comment only
03 May 2011 Mukhtar Ullah

I see. Thanks for clarifying. It is definitely useful then. You should have mentioned this in your description because otherwise people think you may have overlooked it!

Comment only
03 May 2011 Richard Crozier

@Mukhtar, odextend returns the whole solution, and would therefore result in the out of memory problem that odesplit avoids for large solutions. From the matlab documentation:

The following command

sol=ode45(@vdp1,[0 10],[2 0]);

uses ode45 to solve the system y' = vdp1(t,y), where vdp1 is an example of an ODE function provided with MATLAB software, on the interval [0 10]. Then, the commands


extend the solution to the interval [0 20] and plot the first component of the solution on [0 20].

So you get the whole solution from t=zero to tfinal, which may casue your machine to run out of memory.

Comment only
03 May 2011 Mukhtar Ullah

Matlab already has a function ODEXTEND for that.

Comment only
03 May 2011 1.1

Added more detail to description to make clear benefits over odextend

24 Jan 2012 1.2

Changed summary description.

Contact us