本学期我正在为其中一门数学课程编写代码,并且数学运算正确无误,但是我不确定为什么错误项的图形无法按预期方式运行。该项目的这一部分用于欧拉的显式方法,该方法位于here中。我需要能够随时随地绘制错误项的图形,该图形大多数以我期望的方式出现,但是在开始时我是一个非常大的错误,不应该发生。当然,任何建议将不胜感激。谢谢!
import numpy as np
from matplotlib import pyplot as plt
def Eulerm1():
x0=0
y0=2
n=11
xf=2
x=np.linspace(x0,xf,n)
y=np.zeros([n])
z=(3+2*np.pi**2)/(1+np.pi**2)*np.e**x+((np.pi)/(1+np.pi**2))*np.sin(np.pi*x)+np.cos(np.pi*x)/(1+np.pi*x)
L=[0]
for i in range (1,n):
deltax=(xf-x0)/(n-1)
y[0]=y0
y[i]=deltax*(np.cos(np.pi*x[i-1])+y[i-1])+y[i-1]
z[i]=(3+2*np.pi**2)/(1+np.pi**2)*np.e**(x[i])+((np.pi)/(1+np.pi**2))*np.sin(np.pi*(x[i]))-np.cos(np.pi*(x[i]))/(1+np.pi**2)
L.append(abs(y[i]-z[i]))
b=abs(y[i]-z[i])
print (x[i],y[i],z[i], abs(y[i]-z[i]))
print(max(L))
plt.plot(x,b)
plt.xlabel('Time')
plt.ylabel('Computational Errors')
plt.title('Computational Errors with respect to Time')
plt.show()
答案 0 :(得分:0)
for i in range (1,n):
deltax=(xf-x0)/(n-1)
y[0]=y0
y[i]=deltax*(np.cos(np.pi*x[i-1])+y[i-1])+y[i-1]
z[i]=(3+2*np.pi**2)/(1+np.pi**2)*np.e**(x[i])+((np.pi)/(1+np.pi**2))*np.sin(np.pi*(x[i]))-np.cos(np.pi*(x[i]))/(1+np.pi**2)
L.append(abs(y[i]-z[i]))
b=abs(y[i]-z[i])
print (x[i],y[i],z[i], abs(y[i]-z[i]))
print(max(L))
plt.plot(x,b)
plt.xlabel('Time')
plt.ylabel('Computational Errors')
plt.title('Computational Errors with respect to Time')
plt.show()
您的plot.show()
位于for循环内,因此它在第一个循环而不是完成后呈现了图形。