我目前喜欢将编写计算代码作为一种爱好。现在我已经制定了一个欧拉方法,结果非常好,最多。在上,不稳定开始出现。我可以知道为什么会这样吗?方程如下:
#Euler Method
import numpy as np
import matplotlib.pyplot as plt
#Specify Step Size:
h=float(input("Size of h:"))
#Key in X value you want to obtain y(x1):
x1=float(input("input X to get Y:"))
#Number of steps:
n=int((x1)/h)+1
#Creation of array table
A=np.zeros((n,3))
A[0]=[1,0,1]
for i in range(1,n):
#Evaluating values of y:
A[i,0]=i+1
df=-(A[i-1,1]**2)/A[i-1,2]
A[i,2]=A[i-1,2]+h*df
A[i,1]=A[i-1,1]+h
print(A)
plt.plot(A[:,1], A[:,2], label='Approximation')
plt.show()