H.W.11

y=(x+C)/(x+sqrt(x^2+1))
# Euler Method HW 11
# x_i+1 = x_i+h * dx/dt
# dy/dx=1/(x+sqrt(x^2+1))-y/sqrt(x^2+1)
# y_i+1 = y_i+h * dy/dx
import numpy as np
import matplotlib.pyplot as plt
def f(y, x): # dy/dx = f(x, y)
return 1/(x+np.sqrt(x**2+1))-y/np.sqrt(x**2+1)
def EM(f, x, t): # t is np.array
h=t[1]-t[0]
for i in range(len(t)-1):
x[i+1]=x[i]+f(x[i], t[i])*h
return x
if __name__=="__main__":
# initial state
x_i=0; y_i=0
x_f=1
N=1000
x=np.linspace(x_i, x_f, N)
y=np.zeros(len(x), float)
y[0]=y_i
y=EM(f, y, x)
plt.plot(x, y)
plt.title(r"$\frac{dy}{dx}+\frac{y}{\sqrt{x^2+1}}=\frac{1}{x+\sqrt{x^2+1}}$")
plt.xlabel(r"$x$")
plt.ylabel(r"$y$")
plt.show()