# DE Numerical Methods from math import * #define the function def f(t,y): return (cos(t)-y)/(t+1) def run(): print("1:Euler 2:RK4") type=int(input("?")) x0 = float(input("x0=")) y0 = float(input("y0=")) h = float(input("h=")) N = int(input("N=")) if type == 1: euler(f,x0,y0,h,N) if type == 2: RK(f,x0,y0,h,N) def euler(f,x0,y0,h,N): for i in range(1,N+1): y0 += h*f(x0,y0) x0 += h print(round(x0,5)," ",round(y0,5)) # Runge-Kutta # RK(f,x0,y0,h,N) def RK(f,x0,y0,h,N): for i in range(1,N+1): k1 = f(x0,y0) k2 = f(x0+.5*h,y0+.5*h*k1) k3 = f(x0+.5*h,y0+.5*h*k2) k4 = f(x0+h,y0+h*k3) y0 += h*(k1+2*k2+2*k3+k4)/6 x0 += h print(round(x0,5)," ",round(y0,9))