In: Computer Science
Prompt: Produce a 4th order Runge Kutta code in PYTHON that evaluates the following second order ode with the given initial conditions, (d^2y/dt^2) +4(dy/dt)+2y=0, y(0)=1 and y'(0)=3. After your code can evaluate the 2nd order ode, add a final command to plot your results. You may only use python.
import numpy as np
import matplotlib.pyplot as plt
def ode(y):
return np.array([y[1],(-2*y[0]-4*y[1])])
tStart=0
tEnd=5
h=.1
t=np.arange(tStart,tEnd+h,h)
y=np.zeros((len(t),2))
y[0,:]=[1,3]
for i in range(1,len(t)):
k1=ode(y[i-1,:])
k2=ode(y[i-1,:]+h*k1/2)
k3=ode(y[i-1,:]+h*k2/2)
k4=ode(y[i-1,:]+h*k3)
y[i,:]=y[i-1,:]+(h/6)*(k1+2*k3+2*k3+k4)
plt.plot(t,y)
plt.grid()
plt.gca().legend(('y(t)',"y'(t)"))
plt.show()