目录
1、概述
(1)常微分初值问题
(2)数值微分方法
2、显式Euler公式和隐式Euler公式精度比较
(1)代码
(2)结果
3、图像直观图
1、概述 (1)常微分初值问题 (2)数值微分方法 2、显式Euler公式和隐式Euler公式精度比较 (1)代码
import numpy as np import matplotlib.pyplot as plt #y'=-30y,y(0)=1,0<=x<=0.6 def funeval(x,y): fxy=-30*y return fxy def funture(x): ft=np.e**(-30*x) return ft def ImplictEuler(a,b,n): h=np.abs(b-a)/(n-1) y=np.zeros((n,1)) x=np.zeros((n,1)) y0=1 y[0]=y0 x[0]=a for i in range(1,n,1): x[i]=a+i*h y[i]=(1-30*h)*y[i-1] #y[i]=y[i-1]/(1+30*h) yt=np.e**(-30*x) return x,y,yt def main(): x,y=0,0 n = [5,10,20,40,80,160] for i in n: x,y,yt=ImplictEuler(0,0.6,i) plt.plot(x,y,label='Euler-solution-'+str(i)) plt.plot(x,yt,label='Ture-solution-'+str(i)) plt.legend() plt.show() print(x[0:5],[y-yt][0:5]) print(y) if __name__ =='__main__': main()(2)结果
#显式n=20 [[0. ] [0.03157895] [0.06315789] [0.09473684] [0.12631579]] [array([[ 0.00000000e+00], [-3.35128524e-01], [-1.47587815e-01], [-5.81570001e-02], [-2.25998240e-02], [-8.76588167e-03], [-3.39919453e-03], [-1.31807914e-03], [-5.11098880e-04], [-1.98183774e-04], [-7.68477618e-05], [-2.97984961e-05], [-1.15546679e-05], [-4.48043923e-06], [-1.73733558e-06], [-6.73669424e-07], [-2.61222125e-07], [-1.01291518e-07], [-3.92768096e-08], [-1.52299797e-08]])] [[1.00000000e+00] [5.26315789e-02] [2.77008310e-03] [1.45793847e-04] [7.67336039e-06] [4.03861073e-07] [2.12558460e-08] [1.11872874e-09] [5.88804597e-11] [3.09897157e-12] [1.63103767e-13] [8.58440877e-15] [4.51810988e-16] [2.37795257e-17] [1.25155398e-18] [6.58712623e-20] [3.46690854e-21] [1.82468871e-22] [9.60362476e-24] [5.05453935e-25]] Process finished with exit code 0
隐式n=20 [[0. ] [0.15] [0.3 ] [0.45] [0.6 ]] [array([[0. ], [0.17070919], [0.03293444], [0.00600915], [0.00109281]])] [[1. ] [0.18181818] [0.03305785] [0.00601052] [0.00109282]] [[0. ] [0.06666667] [0.13333333] [0.2 ] [0.26666667]] [array([[0.00000000e+00], [1.97998050e-01], [9.27954722e-02], [3.45582849e-02], [1.20102164e-02], [4.06982641e-03], [1.36559790e-03], [4.56415842e-04], [1.52303255e-04], [5.07900334e-05]])] [[1.00000000e+00] [3.33333333e-01] [1.11111111e-01] [3.70370370e-02] [1.23456790e-02] [4.11522634e-03] [1.37174211e-03] [4.57247371e-04] [1.52415790e-04] [5.08052634e-05]]3、图像直观图
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)