importnumpyasnpimportmatplotlib.pyplotasplt# Setting grid pointsaxisbox=[-1.5,1.5,-1,1]xa,xb,ya,yb=axisboxnpts=50theta=np.linspace(0,2*np.pi,2*npts+1)z=np.exp(1j*theta)# 2nd order Adam Bashforthnu2=(z**2-z)/((3*z-1)/2)# 3rd order Adam Bashforthnu3=(z**3-z**2)/((5-16*z+23*z**2)/12)# 4th order Adam Bashforthnu4=(z**4-z**3)/((55*z**3-59*z**2+37*z-9)/24)# chop off intersecting loops for 4th order ABforkinrange(len(nu4)-1):zz=np.array([np.real(nu4),np.imag(nu4)])iloop=0forjinrange(k+2,len(nu4)-1):# find first place where zz(k) intersects the rest of the pathA=np.column_stack([zz[:,k]-zz[:,k+1],zz[:,j+1]-zz[:,j]])ifnp.linalg.det(A)!=0:lam=np.linalg.solve(A,zz[:,j+1]-zz[:,k+1])ifnp.all((0<=lam)&(lam<=1)):iloop=list(range(k+1,j+1))zzint=lam[0]*zz[:,k]+(1-lam[0])*zz[:,k+1]breakifiloop:# chop out nu4(iloop) and replace with single interpolated value zzintzzcp=complex(zzint[0],zzint[1])nu4[iloop[0]]=zzcpnu4=np.delete(nu4,iloop[1:])# Prepare data for savingnus=np.column_stack((np.real(nu2),np.imag(nu2),np.real(nu3),np.imag(nu3)))nus4=np.column_stack((np.real(nu4),np.imag(nu4)))plt.figure()plt.plot(nus[:,0],nus[:,1],'r')plt.plot(nus[:,2],nus[:,3],'b')plt.plot(nus4[:,0],nus4[:,1],'g')plt.show(block=False)# Save to AB_stability.dat filewithopen("AB_stability.dat","w")asf:np.savetxt(f,nus,fmt='%.17e',header="nu2 and nu3 real and imag")f.write("\n\n")np.savetxt(f,nus4,fmt='%.17e',header="nu4 real and imag")