import numpy as np
import matplotlib.pyplot as plt
def f(y, x):
return 1/(x+np.sqrt(x**2+1))-y/np.sqrt(x**2+1)
def PCM(f, x, t):
h=t[1]-t[0]
for i in range(len(t)-1):
xx=x[i]+f(x[i], t[i])*h
x[i+1]=x[i]+h*(f(x[i], t[i])+f(xx, t[i+1]))/2
return x
if __name__=="__main__":
x_i=0; y_i=0
x_f=1
x=np.linspace(x_i, x_f, 1000)
y=np.zeros_like(x, float)
y[0]=y_i
y=PCM(f, y, x)
plt.plot(x, y)
plt.show()