本文是一片学习笔记,记录了解一类一阶微分方程组的方法。
原文DIFFERENTIAL EQUATIONS An Introduction to Modern Methods and Applications第8章(James R. Brannan , William E. Boyce ),内容我直接贴图了。
背景原理部分:Euler's method :
Euler's method 实质上是一种做切线逼近的思维,详细的原文如下
然而这种方法为了更加精确地逼近正确的曲线,就得缩小h的大小,这样效率就降低了,而且误差相对较大,因此我们需要改进这种方法。
2.Improved Euler and Runge–Kutta Methods
详细原文如下
从table8.3.3中可以看出,Euler被优化了很多。
3.有了前面的基础下面就可以看方程组的问题了,以文中二维的为例,在实际运用中可以类推至n个变量。
代码部分
以8.4方程组为例写一段python代码
import matplotlib.pyplot as plt
import numpy as np
h1 = 0.1
h2 = 0.2
x0 = 2
y0 = -0.5
n1 = 10 #iter
n2 = 10
xp1 = []
yp1 = []
xp2 = []
yp2 = []
'''
Euler Methods
'''
def xt(x,y) :
return 4*y - x
def yt(x,y) :
return x - y
def f(x,y,h,n):
for i in range(0,n):
k = np.array([x,y]) + h*np.array([xt(x,y),yt(x,y)])
x , y = k[0] , k[1]
np.array(xp1.append(x))
np.array(yp1.append(y))
return xp1,yp1
'''
Runge–Kutta Methods (improved from euler , more accuracy and efficiency)
'''
def xn1(x,y): #X(T)’
return xt(x,y)
def xn2(x,y,h):
return xn1(x+0.5*h,y+0.5*h*xn1(x,y))
def xn3(x,y,h):
return xn1(x+0.5*h,y+0.5*h*xn2(x,y,h))
def xn4(x,y,h):
return xn1(x+h,y+h*xn3(x,y,h))
def yn1(x,y): #Y(T)’
return yt(x,y)
def yn2(x,y,h):
return yn1(x+0.5*h,y+0.5*h*yn1(x,y))
def yn3(x,y,h):
return yn1(x+0.5*h,y+0.5*h*yn2(x,y,h))
def yn4(x,y,h):
return yn1(x+h,y+h*yn3(x,y,h))
def g(x,y,h,n):
for i in range(0,n):
k = np.array([x,y]) + h/6*(np.array([xn1(x,y)+2*xn2(x,y,h)+2*xn3(x,y,h)+xn4(x,y,h),yn1(x,y)+2*yn2(x,y,h)+2*yn3(x,y,h)+yn4(x,y,h)]))
x,y = k[0] , k[1]
np.array(xp2.append(x))
np.array(yp2.append(y))
return xp2,yp2
f(x0,y0,h1,n1)
g(x0,y0,h2,n2)
plt.scatter(xp1,yp1,1)
plt.scatter(xp2,yp2,1)
plt.ylabel('Y(T) axis')
plt.xlabel('X(T) axis')
plt.show()