Question

I have the following python MWE (code is explained below)

#!/usr/bin/python
from scipy import integrate
from math import *
import numpy
import matplotlib.pyplot as plt

def base_equations(y,t,center):
    return [5*exp(-(t-center)**2/3),-5*exp(-(t-center)**2/3)]

def eqexec1(y,t):
    return base_equations(y,t,30)

def eqexec2(y,t):
    return base_equations(y,t,60)

inits=[0.5, 0.5]

trange=numpy.arange(0,100,0.1)
print trange

y1=integrate.odeint(eqexec1,inits, trange, full_output=0, printmessg=1)
y2=integrate.odeint(eqexec2,inits, trange, full_output=0, printmessg=1)
plt.plot(trange,y1,trange,y2)
plt.legend(["y1a","y1b","y2a","y2b"])
plt.xlabel("Time")
plt.show()

As you can see, I'm integrating a set of equations (base_equations) which are, in essence, a Gaussian pulse. I use odeint to numerically solve these equations for two center points of the pulse (30 and 60).

For the first center point (t=30), equation y1 yields expected behavior: the pulse is visible.

For the second center point (t=60), equation y2 yields unexpected behavior: no pulse is visible at all!

The switch between working and not working occurs between 47 and 48.

Graphical output is as below. The expected output is that lines y2a and y2b will show a dramatic change around 60, but they do not.

Image showing a single Gaussian pulse when there should be two.

Any thoughts as to what might be going on?

Was it helpful?

Solution

The integrator increases its step size in the initial region where the derivative is vanishing, and the steps become so large that it steps over the gaussian peak never seeing it.

You can to tell the integrator to not increase the step size too much:

y2 = integrate.odeint(eqexec2,inits, trange, full_output=0, printmessg=1,hmax=1.0)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top