g = 9.81; m = 68.1; c = 0.25; h = 2; t = 0;
% Initial guess v_eu = 0; v_true = 0; x_eu = 350; x_true = 0; err_v = 100; err_x = 0;
i = 1; fprintf('Iter\t t\t\t x_true\t\t v_ture\t\t x_Euler\t v_Euler\t Error(x)\t Error(v)\n') while x_eu > -50 % Stopping criteria fprintf('%u\t %8.2f\t %8.2f\t%8.2f\t %8.2f\t %8.2f\t %8.2f\t%8.2f\n',i,t,x_true,v_true,x_eu,v_eu,err_x,err_v) slope = g-(c/m)*v_eu^2; % v' = slope
t = t+h; %Interval
x_true =(m/c)*log(cosh(t*sqrt(g*c/m))); x_eu = x_eu - v_eu*h; % Velocity
v_true = sqrt(m*g/c)*tanh(t*sqrt(g*c/m)); v_eu = v_eu + slope*h; err_v = abs((v_true - v_eu)/v_true*100); err_x = abs((x_true -(x_eu))/x_true*100); *v_plot(1)=0; v_plot(i+1)=v_eu;**x_plot(1)=350;x_plot(i+1)= x_eu*;** i = i +1;
end t_plot=0:h:t; plot(t_plot,v_plot,t_plot,x_plot) title('(Problem 1(a) Euler Method') xlabel('Time (s)'); ylabel('V and X');
Best Answer