我得到如下微分方程y’’+a y’−y+b y^3=c cos(kx),初始条件y(0)=y’(0)=0,参数值a=0.05,b=k=1,c=0.5。在
现在,我要做的是减少解算器的相对公差,直到数值解y(t)在x=x_max处的变化小于10^8
这是我的代码:from scipy.integrate import odeint
from scipy.optimize import brent
import numpy as np
def de( Y, t ): # de as an array
a = 0.05 # parameters
b = 1.0
c = 0.5
k = 1.0
return np.array([Y[1], Y[0] - a*Y[1] - b*(Y[0])**3 - c*np.cos(k*t)])
def Ydiff(h, *args): # calculates difference between the solution Y1
de = args[0] # (with default relative error) and Y2 (with new
t = args[1] # relative error)
y0 = args[2]
Y1 = args[3]
Y2 = odeint( de, y0, t, full_output=0, rtol=h)
return np.linalg.norm(Y1[-1,0] - Y2[-1,0]) # linalg.norm isn't
# n