Van der Pol like equation

Van der Pol like equation#

Introduction#

Wait a few seconds until Python interaction ready! is shown in the top.

After that follow the instructions in the Grasple Exercise.

On this webpage the system of differential equations

\[\begin{split} \begin{cases}\dfrac{dx}{dt}=y, \\\\ \dfrac{dy}{dt} = \begin{cases}-x+\lambda+\mu(1+\lambda^2-2\lambda x+x^2)y, & \text{when}\,|x-\lambda|\leqslant 1,\\-x+\lambda+\gamma\mu\left(1-\sqrt{|x-\lambda|}\right)y, & \text{when}\,|x-\lambda|\gt 1.\end{cases} \end{cases} \end{split}\]

is considered, with several initial conditions.

This webpage solves this initial-value problem numerically, using numpy, and then uses numpy and matplotlib to display the obtained approximate solution.

import micropip
await micropip.install("ipympl")
import matplotlib
if not hasattr(matplotlib.rcParams, '_get'):
    matplotlib.rcParams._get = matplotlib.rcParams.get
%matplotlib widget
import numpy as np
import matplotlib.pyplot as plt
    
def Van_der_Pol_like_equation(labda,mu,gamma,x0,y0,tend):
    
    prop_cycle = plt.rcParams['axes.prop_cycle']
    colors = prop_cycle.by_key()['color']

    def f(x,labda,gamma):
        if np.abs(x-labda)<=1:
            return 1+labda**2-2*labda*x+x**2
        else:
            return gamma*(1-np.sqrt(np.abs(x-labda)))

    def ode(t,y,labda,gamma,mu):
        x = y[0]
        y = y[1]
        rhsx = y
        rhsy = -x+labda+mu*f(x,labda,gamma)*y
        rhs = np.array([rhsx,rhsy])
        return rhs

    def solve_my_ivp(x0,y0,tend,labda,gamma,mu):
        t0 = 0
        trange = [t0,tend]
        y0 = [x0,y0]
        N = int(np.min([tend*1e3,1e4]))
        tout = np.linspace(t0,tend,N+1)
        dt = np.mean(np.diff(tout))
        yout = np.zeros((2,N+1))
        yout[:,0] = y0
        for n in np.arange(0,N):
            k1 = dt*ode(tout[n],yout[:,n],labda,gamma,mu)
            k2 = dt*ode(tout[n]+dt/2,yout[:,n]+1/2*k1,labda,gamma,mu)
            k3 = dt*ode(tout[n]+dt/2,yout[:,n]+1/2*k2,labda,gamma,mu)
            k4 = dt*ode(tout[n]+dt,yout[:,n]+k3,labda,gamma,mu)
            yout[:,n+1] = yout[:,n]+1/6*(k1+2*k2+2*k3+k4)
        return tout, yout[0,:], yout[1,:]

    plt.close('all')
    fig = plt.figure()
    t,x,y = solve_my_ivp(x0,y0,tend,labda,gamma,mu)
    plt.plot(x,y,color=colors[0])
    plt.plot(x0,y0,ls='',marker='o',color=colors[0],label='$x(0)='+str(x0)+', y(0)='+str(y0)+'$')
    plt.plot(labda,0,ls='',marker='o',color=colors[1],label='$x_{eq}='+str(labda)+', y_{eq}='+str(0)+'$')
    plt.xlabel('$x(t)$')
    plt.ylabel('$y(t)$')
    plt.grid()
    plt.legend(loc='upper center',bbox_to_anchor=(0.5, 1.15));
    fig.canvas.toolbar_visible = True
    
    pass

INPUT CELL#

In the cell below you should put the values of the parameters you see in Grasple.

Please note: Each attempt in Grasple contains different values for the parameters.

After you run the cell the results of the calculations are shown with the set parameter values.

If you point your mouse below a figure, controls will appear that you can use to pan and zoom.

''' Set the given values in the following lines '''
labda = 2
mu = 0.67
gamma = 2
x_0 = -1
y_0 = 4
t_end = 50

''' The next line performs the calculations and shows the results. '''
Van_der_Pol_like_equation(labda,mu,gamma,x_0,y_0,t_end)