Could you please write a simple program with "4th Order Runge-Kutta Method" for projectile motion(no air resistance) in MATLAB?
import numpy import zeros, linspace, pi, cos, array import matplotlib.pyplot as plt omega = 2 P = 2*pi/omega dt = P/20 T = 3*P N_t = int(round(T/dt)) t = linspace(0, N_t*dt, N_t+1) u = zeros(N_t+1) v = zeros(N_t+1) # Initial condition X_0 = 2 u[0] = X_0 v[0] = 0 # Step equations forward in time for n in range(N_t): u[n+1] = u[n] + dt*v[n] v[n+1] = v[n] - dt*omega**2*u[n] fig = plt.figure() l1, l2 = plt.plot(t, u, 'b-', t, X_0*cos(omega*t), 'r--') fig.legend((l1, l2), ('numerical', 'exact'), 'upper left') plt.xlabel('t') plt.show() plt.savefig('tmp.pdf'); plt.savefig('tmp.png')
Get Answers For Free
Most questions answered within 1 hours.