Rewrite this MatLab code. The results should be the same, but it should be a different…
- User
Rewrite this MatLab code. The results should be the same, but it should be a different code.
%% simulating a bouncing ball: my code % This script simulates a ball dropped or thrown in a vertical direction % under constant gravitational acceleration. The ball will hit the floor % and it will bounce a multiple number of times based on the coefficient % of restitution given by epsilon. We have just before/after a collision. % epsilon = |just after velocity| divided by |just before velocity| % % INPUT % yo = initial height of ball % vyo = initial velocity of ball in the y-direction % dt = time resolution % tMax = total time of the process % epsilon = coefficient of restitution % % PROCESS % solve diferential equation using Euler’s method of smaller time intervals % % OUTPUT % height versus time graph % velocity versus time graph clear all % never not do this %% set initial condition to=0; % initial time on clock (seconds) yo=2.5; % meters (m) vyo=5; % velocity --> meters/second (m/s) dt=0.001; % time resolution --> seconds (s) tMax=20; % maximum time range --> (s) epsilon=0.7; % coefficient of restitution (unitless) dtw=dt/1000 %% set constants ay = -9.8; % acceleratin due to gravity m/s^2 %% solve differential equation using Euler’s method tf = ceil(1.1tMax); % add extra time to preallocate the tArray npts = ceil((tf-to) / dt); tArray = zeros(1,npts); % time range as an array (in seconds, s) y = zeros(1,npts); % preallocate y array (height, m) vy = zeros(1,npts); % preallocate the vy array (height, m) y(1) = yo; vy(1) = vyo; tArray(1) = to;
% ————————————————————————————————— calculate position as function of time t = to; for i = 1:npts-1 % ----------------------------------------------------------- detect floor tVar = t+dt; vyVar = vy(i)+aydt; yVar = y(i) + vy(i)dt;
if (yVar <= 0) % => hit the floor with dt time resolution % ——————————————————————————————— calculate Euler’s method w/ a smaller dt tVar = t; while(1) vy(i) = vy(i) + aydtw; yVar = y(i) + vy(i)dtw; if(yVar <= 0) break; end vy(i) = vyVar; y(i) = yVar; tVar = tVar + dtw; end
f = (y(i)) / (y(i)-yVar); % fraction in terms of height vyVar = vy(i) + f(vyVar-vy(i)); % assume equal fractions tVar = tVar+fdtw; % assume equal fractions vy(i+1) = -vyVarepsilon; y(i+1) = 0; % floor height is set to 0
else % => ball didn’t interact with the wall vy(i+1)=vyVar; y(i+1)=yVar; end tArray(i+1) = tVar; t = tVar; end
- Assistant
%% simulating a bouncing ball: rewritten code
% set initial condition to = 0; % initial time on clock (seconds) yo = 2.5; % meters (m) vyo = 5; % velocity --> meters/second (m/s) dt = 0.001; % time resolution --> seconds (s) tMax = 20; % maximum time range --> (s) epsilon = 0.7; % coefficient of restitution (unitless) dtw = dt/1000;
% set constants ay = -9.8; % acceleration due to gravity m/s^2
% solve differential equation using Euler’s method tf = ceil(1.1*tMax); % add extra time to preallocate the tArray npts = ceil((tf - to) / dt); tArray = zeros(1, npts); % time range as an array (in seconds, s) y = zeros(1, npts); % preallocate y array (height, m) vy = zeros(1, npts); % preallocate the vy array (height, m) y(1) = yo; vy(1) = vyo; tArray(1) = to;
- User
- Assistant