Computational Science Stack Exchange is a question and answer site for scientists using computers to solve scientific problems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Let us say we have the following equations:

dy1/dt = f(y1, t)      [1]
dy2/dt = g(y2, t)      [2]

The equations are such that they are "conservative", i.e. the following condition should hold:

dy1/dt + dy2/dt = 0    [3]

Using scipy.odeint, I find that I can integrate conservative equations like this just fine for simple systems of ODEs.

However, for larger ones, I get the following issue.

Let's say that this is my derivative function:

def deriv_function(y0s, t):
    ...body defines equations 1, and 2...

    print np.sum(ode)
    return ode

Note the print statement.

I use scipy.odeint on deriv_function as follows:

odeint(deriv_fun, y0s, [0, 0.5])

Due to the print statement, the following gets printed out:

-1.38555833473e-13 <--- note, close to zero
-54.3275502654    <--- note, not close to zero

For smaller systems of equations (not the same), the following gets printed out:


I have spent a lot of time trying to figure out if the issue has something to do with the way I have defined the equations, and I am now fairly certain that this is not the case. In order to confirm this, I would like to ask: is it possible at all for a non-conservative system of equations to first print an ode sum that is close to zero, but later print values that are not close to zero?

Another way to think of the question: in the larger system of equations, the ode sum initial prints out to be approximately zero, before increasing. What could be happening in the bowels of the solver for this problem to be occurring?

share|improve this question
up vote 5 down vote accepted

In general, numerical methods do not conserve quantities that may be conserved in a continuous system. The discretized system is typically non-conservative. A smaller time step (equivalently a smaller error tolerance) should help, but if you have a quantity that must be precisely conserved, you will have to use a method that is designed to conserve the desired quantity. Symplectic integrators are perhaps the most widely known and used examples of such a case.

As an example, consider a simple mass-spring oscillator:
$$\dfrac{dx}{dt} = v$$ $$\dfrac{dv}{dt} = -\dfrac{k}{m}x$$

The total energy $E = \frac{1}{2} \left( m v^2 + k x^2\right)$ is conserved since $$\dfrac{dE}{dt} = m v \dfrac{dv}{dt}+k x \dfrac{dx}{dt} = -kv x + kxv = 0$$

However, if you integrate this system using a Runge-Kutta type method the energy will not be conserved. A quick test using MATLAB's ode45 integrator gives the following energy plot which is not as dramatic as your case, but shows a clear decay in the energy:
energy decay

This is a Hamiltonian system with Hamiltonian $H(x,v) = E(x,v) = \frac{1}{2} \left( m v^2 + k x^2\right)$ and would be a good candidate for the application of a symplectic integrator which would preserve the total energy.

share|improve this answer
Thanks for the explanation, and the pointer to what I should look into! I want to confirm though: what I am printing out is a symptom of the non-conservative effect of general numerical methods? – user89 Jun 19 '14 at 4:59
As @DavidKetcheson said in his answer, it's impossible for me to rule out a bug in your code without a lot more information and testing. If it's not a bug, it's almost certainly due to the non-conservative nature of the numerical scheme you're using which is a form of discretization error. – Doug Lipinski Jun 19 '14 at 12:24
You may want to try decreasing the tolerance or time step to see if the error decreases. If it does not, there is almost certainly a bug in you code. – Doug Lipinski Jun 19 '14 at 12:25
@DougLipinski You said "symplectic integrators are perhaps the most widely known". What are other integrators that conserve particular quantities? – Aurelius Jun 19 '14 at 13:09
@Aurelius My background is largely in fluid dynamics so I had in mind flux difference schemes that exactly conserve mass/momentum. There are many finite volume and finite difference schemes that meet this criteria. There are likely other examples as well, a search for conservative formulation turns up over 8 million results. – Doug Lipinski Jun 19 '14 at 13:52

There are two possibilities:

  1. You have a bug.
  2. You are seeing an accumulation of discretization errors.

Without more detail, it is impossible for us to tell you which is actually happening in your simulation.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.