import numpy as np;
import matplotlib.pyplot as plt;
plt.rcParams.update({"text.usetex":True});
%config InlineBackend.figure_format = "svg"
from ipywidgets import interactive
from scipy.integrate import solve_ivp
from mayavi import mlab
x = -1; y = -1;
A = np.array([[0, 1-3*y**2],[-1, -2*y]])
l, v = np.linalg.eig(A)
print(l)
print(v)
[-0.73205081 2.73205081] [[-0.9390708 0.59069049] [-0.34372377 -0.80689822]]
x = np.linspace(-3, 3, 100); y = np.linspace(-3, 3, 100);
X, Y = np.meshgrid(x, y);
U = Y-Y**3;
V = -X-Y**2;
#plt.quiver(X, Y, U/(U**2+V**2)**0.5, V/(U**2+V**2)**0.5);
plt.streamplot(X, Y, U, V, integration_direction='forward', density=1.5)
ax = plt.gca(); ax.set_aspect(1);
x = np.linspace(-6, 6, 100); y = np.linspace(-3, 3, 100);
X, Y = np.meshgrid(x, y);
U = Y;
V = -np.sin(X);
#plt.quiver(X, Y, U/(U**2+V**2)**0.5, V/(U**2+V**2)**0.5);
plt.streamplot(X, Y, U, V, integration_direction='forward', density=1.5)
ax = plt.gca(); ax.set_aspect(1);
def mysys(t, x): # returns the RHS
return ([x[1], -np.sin(x[0])]);
tspan = [0,40]
x0 = 0;
for y0 in np.linspace(-2.5, 2.5,100):
ics = [x0, y0]
sol = solve_ivp(mysys, tspan, ics, dense_output=True, rtol=1e-8);
tout = np.linspace(0, np.max(tspan), 200);
xout = sol.sol(tout)[0];
yout = sol.sol(tout)[1];
E = 1/2*yout**2 - np.cos(xout);
mlab.plot3d(np.cos(xout), np.sin(xout), yout )
mlab.show();
def show_2dphase(b = 0.1):
x = np.linspace(-12, 12, 30); y = np.linspace(-3, 3, 30);
X, Y = np.meshgrid(x, y);
U = Y;
V = -b*Y-np.sin(X);
#plt.quiver(X, Y, U/(U**2+V**2)**0.5, V/(U**2+V**2)**0.5);
plt.streamplot(X, Y, U, V, integration_direction='forward', density=1.5)
ax = plt.gca(); ax.set_aspect(1);
w = interactive(show_2dphase, b=(0, 1, 0.05))
w
def mysys(t, x): # returns the RHS
return ([x[1], -0.25*x[1]-np.sin(x[0])]);
tspan = [0,10]
x0 = 0;
y0 = 5;
ics = [x0, y0]
sol = solve_ivp(mysys, tspan, ics, dense_output=True, rtol=1e-8);
tout = np.linspace(0, np.max(tspan), 100);
xout = sol.sol(tout)[0];
yout = sol.sol(tout)[1];
E = 1/2*yout**2 - np.cos(xout);
plt.plot(tout, xout, tout, yout)
#mlab.plot3d(np.cos(xout), np.sin(xout), yout )
[<matplotlib.lines.Line2D at 0x25275d28fd0>, <matplotlib.lines.Line2D at 0x25275d320d0>]
We now focus our discussion to Limit Cycles
def mysys(t, r): # returns the RHS
return ([r[0]*(1-r[0]**2), 1]);
tspan = [0,10]
x0 = 0;
y0 = 2;
r0 = (x0**2 + y0**2)**0.5; t0 = np.arctan2(y0, x0);
ics = [r0, t0]
sol = solve_ivp(mysys, tspan, ics, dense_output=True, rtol=1e-8);
tout = np.linspace(0, np.max(tspan), 100);
rout = sol.sol(tout)[0];
tout = sol.sol(tout)[1];
xout = rout*np.cos(tout); yout = rout*np.sin(tout);
fig, ax = plt.subplots(2,1)
ax[0].plot(xout, yout);
ax[0].set_aspect(1);
ax[1].plot(tout, xout, tout, yout);
#mlab.plot3d(np.cos(xout), np.sin(xout), yout )
def show_traj(x0=0.9,y0=1.0, a = 0.0):
x = np.linspace(-4, 4, 14); y = np.linspace(-4, 4, 14);
X, Y = np.meshgrid(x, y);
U = Y;
V = -X-a*(X**2-1)*Y;
plt.quiver(X, Y, U/(U**2+V**2)**0.5, V/(U**2+V**2)**0.5);
ax = plt.gca(); ax.set_aspect(1);
def mysys(t, x): # returns the RHS
return ([x[1], -x[0]-a*(x[0]**2-1)*x[1]]);
tspan = [0,20]
#x0 = -1.1; y0 = 2.5
ics = [x0, y0]
sol = solve_ivp(mysys, tspan, ics, dense_output=True, rtol=1e-8);
tout = np.linspace(0, np.max(tspan), 200);
xout = sol.sol(tout)[0];
yout = sol.sol(tout)[1];
plt.plot(xout,yout)
#plt.plot(tout, xout, tout, yout)
w = interactive(show_traj, x0 = (-1.5, 1.5, 0.1), y0 = (-1.5, 1.5, 0.1), a = (-1.5, 1.5, 0.05))
w
def show_traj(x0=0.9,y0=1.0, a = 0.0):
x = np.linspace(-4, 4, 14); y = np.linspace(-4, 4, 14);
X, Y = np.meshgrid(x, y);
U = Y;
V = -X-a*(X**2-1)*Y;
#plt.quiver(X, Y, U/(U**2+V**2)**0.5, V/(U**2+V**2)**0.5);
#ax = plt.gca(); ax.set_aspect(1);
def mysys(t, x): # returns the RHS
return ([x[1], -x[0]-a*(x[0]**2-1)*x[1]]);
tspan = [0,20]
#x0 = -1.1; y0 = 2.5
ics = [x0, y0]
sol = solve_ivp(mysys, tspan, ics, dense_output=True, rtol=1e-8);
tout = np.linspace(0, np.max(tspan), 200);
xout = sol.sol(tout)[0];
yout = sol.sol(tout)[1];
#plt.plot(xout,yout)
plt.plot(tout, xout, tout, yout)
w = interactive(show_traj, x0 = (-1.5, 1.5, 0.1), y0 = (-1.5, 1.5, 0.1), a = (-1.5, 1.5, 0.05))
w
def show_traj(x0=0.9, y0=1.0, a = 0.0, b = 0.1):
x = np.linspace(0, 4, 14); y = np.linspace(0, 4, 14);
X, Y = np.meshgrid(x, y);
U = -X + a*Y + X**2*Y;
V = b - a*Y - X**2*Y;
def mysys(t, x): # returns the RHS
return ([-x[0] + a*x[1] + x[0]**2*x[1], b - a*x[1] - x[0]**2*x[1]]);
tspan = [0,40]
#x0 = -1.1; y0 = 2.5
ics = [x0, y0]
sol = solve_ivp(mysys, tspan, ics, dense_output=True, rtol=1e-8);
tout = np.linspace(0, np.max(tspan), 200);
xout = sol.sol(tout)[0];
yout = sol.sol(tout)[1];
fig, ax = plt.subplots(1,2)
ax[0].quiver(X, Y, U/(U**2+V**2)**0.5, V/(U**2+V**2)**0.5);
ax[0].set_aspect(1)
ax[0].plot(xout, yout)
ax[0].plot(x0, y0, 'ok');
ax[0].plot(b, b/(a+b**2), 'sr')
ax[0].set_xlim(0,2.5)
fig.set_size_inches(10, 5)
ax[1].plot(tout, xout, tout, yout)
w = interactive(show_traj, x0 = (0, 1.5, 0.1), y0 = (0, 1.5, 0.1), a = (0, 0.2, 0.01), b = (0, 1.5, 0.01))
w