/
phase_portraits.py
executable file
·62 lines (49 loc) · 1.81 KB
/
phase_portraits.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import numpy as np
import scipy as sc
import matplotlib.pyplot as plt
colors = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728',
'#9467bd', '#8c564b', '#e377c2', '#7f7f7f',
'#bcbd22', '#17becf']
def plot_flow(ax,rhs,xrange=[-2,2],yrange=[-2,2],xnum=100,ynum=100,nullclines=False):
# Set up u,v space
u = np.linspace(xrange[0], xrange[1], xnum)
v = np.linspace(yrange[0], yrange[1], ynum)
uu, vv = np.meshgrid(u, v)
# Compute derivatives
u_vel = np.empty_like(uu)
v_vel = np.empty_like(vv)
for i in range(uu.shape[0]):
for j in range(uu.shape[1]):
u_vel[i,j], v_vel[i,j] = rhs(0,np.array([uu[i,j], vv[i,j]]))
# Make stream plot
ax.streamplot(uu, vv, u_vel, v_vel,color='grey')
#Nullclines
if nullclines:
ax.contour(uu, vv, u_vel, [0],colors=('r'))
ax.contour(uu, vv, v_vel, [0],colors=('b'))
return ax
# A function that plots a path
def plot_solution(ax,rhs,y0,t_end,t_dim=2000,color='green',init_pt=True,linewidth=2.5):
t = np.linspace(0, t_end, t_dim)
X = sc.integrate.solve_ivp(rhs,t_span=[t[0],t[-1]],y0=y0,t_eval=t)
x,y=X.y
t=X.t
ax.plot(x,y, color=color,linewidth=linewidth)
if (init_pt):
ax=plot_point(ax,y0,color=color)
return ax
#A function to add fixed points to a phase diagram
def plot_point(ax, coord, color='black',markersize=20):
"""Add fixed points to plot."""
x,y=coord
ax.plot(x,y, '.', color='black', markerfacecolor=color, markersize=markersize)
return ax
def plot_jordan(ax,rhs,r0=[0,0],rr=1,num=25,vectors=True, color='blue',linewidth=1.5):
theta=np.linspace(0,2*np.pi, num)
x=rr*np.cos(theta)+r0[0]
y=rr*np.sin(theta)+r0[1]
ax.plot(x,y,linewidth=linewidth, color=color)
if vectors:
U,V=rhs(0,[x,y])
ax.quiver(x,y,U,V)
return ax