(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