SEE HERE FOR MORE INFORMATION: https://en.wikipedia.org/wiki/Van_der_Pol_oscillator ALSO SEE STROGATZ' BOOK 'NONLINEAR DYNAMICS AND CHAOS' (Westview Press, 2001)
In [4]:
%matplotlib inline
import numpy as np
from scipy.integrate import odeint
from pylab import *
from bokeh.plotting import *
from bokeh.models import ColumnDataSource
#from bokeh.io import output_notebook
# DEFINE A FUNCTION THAT OUTPUTS POSITION/VELOCITY TIME SERIES,
#GIVEN AN ARRAY OF TIME POINTS (x) AND A SCALAR PARAMETER MU THAT PARAMETRIZES
#THE NONLINEARITY AND STRENGTH OF DAMPING.
def g(x,mu):
#A = 1
#B = 3
#DEFINE OUR SYSTEM OF DIFFERENTIAL EQUATIONS dZ/dt = f(Z, t)
def f(Z,t):
X = Z[0]
Y = Z[1]
#the model equations
f0 = Y
f1 = mu*(1-np.power(X,2))*Y - X
return [f0 , f1]
#initial conditions
X0 = 0.001
Y0 = 0
Z0 = [ X0 , Y0 ]
#t = np.linspace(0, 50, 1000) # time grid
#Solve the DEs
return odeint( f , Z0 , x)
#GENERATE THE FIRST SOLUTION
x = np.linspace(0, 50, 1000)
soln = g(x , 1)
y = soln[:,0]
z = soln[:,1]
In [5]:
#LOAD THE NOTEBOOK
output_notebook()
#GET ALL DATA TOGETHER TO DEFINE THE 1ST PLOTS
source = ColumnDataSource( data = dict(x=x,y=y,z=z))
#output_file("lines1.html")
p1 = figure( width=350, height=350, title='position')
p1.line('x','y', line_width = 4 , source = source )
p1.xaxis.axis_label = 'time'
p1.yaxis.axis_label = 'position'
p2 = figure(width=350, height=350, title='velocity')
p2.line('x','z', line_width = 4 , source = source , color = 'red')
p2.xaxis.axis_label = 'time'
p2.yaxis.axis_label = 'velocity'
p3 = figure( width=350, height=350, title='Phase plane')
p3.line('y','z', line_width = 2 , source = source , color = 'green')
p = gridplot([[p1 , p2 ], [p3]])
#THIS IS HOW WE UPDATE THE PLOTS
def update(mu = 1):
source.data['y'] = g(x , mu)[:,0]
source.data['z'] = g(x , mu)[:,1]
source.push_notebook()
#LETS LOOK AT THE CONCENTRATION PROFILES AND PHASE PLANE NOW
show(p)
In [6]:
#HERE WE GENERATE SOME SLIDERS THAT ALLOW US TO CHANGE THE REACTION RATES A,B ON THE FLY
from IPython.html.widgets import interact
interact( update , mu = (1,10,0.1))
#output_file("lines1.html")
In [ ]: