# Attractive but unstable equilibrium (r, theta) = (1, 0) of## r+ = sqrt(r) theta+ = theta + (1/2)(1 - cos theta)## Linearization at (1, 0): eigenvalue 1/2 along w1 (radial), 1 along w2# (tangent to the unit circle). The unit circle is a homoclinic orbit.# Every motion with r(0) > 0 converges to (1, 0); a motion starting on the# unit circle at theta(0) > 0, however small, travels once around the circle.importosimportnumpyasnpimportmatplotlib.pyplotaspltfrommiscimportgnuplotsavedefstep(x,p):r,th=xc=p["c"]returnnp.array([np.sqrt(r),th+c*(1.0-np.cos(th))])p={"c":0.5}nsteps=400# initial states (r(0), theta(0))starts=np.array([[1.0,0.15],[0.05,0.6],[1.9,0.5],[0.1,4.0],[1.5,4.6],[0.22,0.0],[1.9,0.0],[1.0,2*np.pi-0.9]])data={}forj,x0inenumerate(starts):x=np.empty((nsteps+1,2))x[0]=x0forkinrange(nsteps):x[k+1]=step(x[k],p)r,th=x[:,0],x[:,1]# trajectory in the plane, x1 = r cos theta, x2 = r sin thetadata[f"traj{j+1}"]=np.column_stack([r*np.cos(th),r*np.sin(th)])ifnotos.getenv('OMIT_PLOTS')=='true':phi=np.linspace(0,2*np.pi,400)plt.plot(np.cos(phi),np.sin(phi),color="0.6")forjinrange(len(starts)):xy=data[f"traj{j+1}"]plt.plot(xy[:,0],xy[:,1],"-o",ms=2)plt.plot(xy[0,0],xy[0,1],"ok",mfc="w")plt.axis("equal")plt.xlabel("x_1")plt.ylabel("x_2")gnuplotsave("attractive_unstable.dat",data,fmt="%.10g")