% Example: Euler Method calculation of Falling Ball % Author: F. P. Schloerb clear g = 9.8; % gravitational acceleration X0 = 0; % initial values V0 = 0; dt = 2; % step size t = 0:dt:100; % array of times for calculation X = zeros(length(t),1); % define solution arrays V = zeros(length(t),1); % initialize soplution arrays with initial vaulues X(1) = X0; V(1) = V0; % loop to calculate solution for i=1:length(t)-1 V(i+1) = V(i) - g*dt; X(i+1) = X(i) + V(i)*dt; end % make a nice graph of position versus time plot(t,X,'md') hold on plot(t,X0+V0*t-0.5*g*t.*t,'k') hold off xlabel('t (s)'); ylabel('X (m)'); legend('Euler Method','Exact')