import numpy as np
##定义未知量和待定参数amplitude和detuning
amplitude=20
detuning=20
#郎之万方程经典形式
def dfun(t,x,y,q,p):
dvec=np.zeros((4))
dvec[0]=-0.5*x-(detuning+0.02*q)*y+amplitude
dvec[1]=-0.5*y+(detuning+0.02*q)*x
dvec[2]=20*p
dvec[3]=0.02*(x**2+y**2)-20*q-0.01*p
return dvec
#四阶龙格库塔:dfun=微分方程;t为自变量,x,y,q,p为因变量;求解区间(a,b);x0,y0,q0,p0=初值,h=步长
def main(dfun,a,b,t,x,y,q,p,x0,y0,q0,p0,h):
K1=dfun(t,x,y,q,p)[0]
L1=dfun(t,x,y,q,p)[1]
M1=dfun(t,x,y,q,p)[2]
N1=dfun(t,x,y,q,p)[3]
K2=dfun(t+h*0.5,x+h*0.5*K1,y+h*0.5*L1,q+h*0.5*M1,p+h*0.5*N1)[0]
L2=dfun(t+h*0.5,x+h*0.5*K1,y+h*0.5*L1,q+h*0.5*M1,p+h*0.5*N1)[1]
M2=dfun(t+h*0.5,x+h*0.5*K1,y+h*0.5*L1,q+h*0.5*M1,p+h*0.5*N1)[2]
N2=dfun(t+h*0.5,x+h*0.5*K1,y+h*0.5*L1,q+h*0.5*M1,p+h*0.5*N1)[3]
K3=dfun(t+h*0.5,x+h*0.5*K2,y+h*0.5*L2,q+h*0.5*M2,p+h*0.5*N2)[0]
L3=dfun(t+h*0.5,x+h*0.5*K2,y+h*0.5*L2,q+h*0.5*M2,p+h*0.5*N2)[1]
M3=dfun(t+h*0.5,x+h*0.5*K2,y+h*0.5*L2,q+h*0.5*M2,p+h*0.5*N2)[2]
N3=dfun(t+h*0.5,x+h*0.5*K2,y+h*0.5*L2,q+h*0.5*M2,p+h*0.5*N2)[3]
K4=dfun(t+h,x+h*K3,y+h*L3,q+h*M3,p+h*N3)[0]
L4=dfun(t+h,x+h*K3,y+h*L3,q+h*M3,p+h*N3)[1]
M4=dfun(t+h,x+h*K3,y+h*L3,q+h*M3,p+h*N3)[2]
N4=dfun(t+h,x+h*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