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 % use crude approximation to determine when ball hits floor % % 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 (no units) %% set constants ay=-9.8; % acceleratin due to gravity m/s^2 %% solve differential equation using Euler's method tArray = to:dt:(to+tMax); % time range as an array (s) npts = length(tArray); y = zeros(1,npts); % preallocate the y array (height, m) vy = zeros(1,npts); % preallocate the vy array (height, m) y(1)=yo; vy(1)=vyo; % ——————————————————————————————— calculate position as a function of time t = to; for i=1:npts-1 % ——————————————————————————————————————————————————————————— detect floor tVar = t+dt; vyVar = vy(i)+ay*dt; yVar = y(i) + vy(i)*dt;
if (yVar <= 0) % hit the floor with dt time resolution % ———————————————————————————— do linear interpolation as an approximation f = (y(i)) / (y(i)-yVar); % fraction in terms of height vyVar = vy(i) + f*(vyVar-vy(i)); % assume equal fractions tVar=t+fdt; % assume equal fractions vy(i+1) = -vyVarepsilon; y(i+1) = 0; % floor height = 0
else % ball didn't interact w/ the wall vy(i+1) = vyVar; y(i+1) = yVar; end
tArray(i+1) = tVar; t = tVar; end
vy(i+1) = vy(i) + ay*dt; y(i+1) = y(i) + vy(i)*dt; if( y(i+1) <= 0 ) % => hit the floor with dt time resolution vy(i+1) = - vy(i)*epsilon; y(i+1) = 0; end
- Assistant
%% simulating a bouncing ball: rewritten 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 = 2.5; % initial height of ball in meters (m) vyo = 5; % initial velocity of ball in the y-direction in meters/second (m/s) dt = 0.001; % time resolution in seconds (s) tMax = 20; % maximum time range in seconds (s) epsilon = 0.7; % coefficient of restitution (no units)
% PROCESS % Solve differential equation using Euler's method % Use crude approximation to determine when ball hits floor
% OUTPUT % Height versus time graph % Velocity versus time graph
clear all % always clear workspace before running
%% Set initial condition to = 0; % initial time on clock (seconds)
%% Set constants ay = -9.8; % acceleration due to gravity m/s^2
%% Solve differential equation using Euler's method tArray = to:dt:(to+tMax); % time range as an array (s) npts = length(tArray); y = zeros(1, npts); % preallocate the y array (height, m) vy = zeros(1, npts); % preallocate the vy array (height, m) y(1) = yo; vy(1) = vyo;
% Calculate position as a function of time for i = 1:npts-1 tVar = tArray(i) + dt; vyVar = vy(i) + ay * dt; yVar = y(i) + vy(i) * dt;
% Detect floor if (yVar <= 0) % hit the floor with dt time resolution % Do linear interpolation as an approximation f = (y(i)) / (y(i) - yVar); % fraction in terms of height vyVar = vy(i) + f * (vyVar - vy(i)); % assume equal fractions tVar = tArray(i) + f * dt; % assume equal fractions vy(i+1) = -vyVar * epsilon; y(i+1) = 0; % floor height = 0 else % ball didn't interact with the floor vy(i+1) = vyVar; y(i+1) = yVar; end tArray(i+1) = tVar;end
% Update velocity and height if the ball didn't hit the floor at the end if (y(i+1) > 0) vy(i+1) = vy(i) + ay * dt; y(i+1) = y(i) + vy(i) * dt; if( y(i+1) <= 0 ) % hit the floor with dt time resolution vy(i+1) = -vy(i) * epsilon; y(i+1) = 0; end end
- 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.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;
% ————————————————————————————————— calculate position as function of time t = to; for i = 1:npts-1 % ----------------------------------------------------------- detect floor tVar = t+dt; vyVar = vy(i)+ay*dt; 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) + ay*dtw; 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; endtArray(i+1) = tVar; t = tVar; end
- Assistant
%% simulating a bouncing ball: rewritten 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 = 2.5; % initial height of ball in meters (m) vyo = 5; % initial velocity of ball in the y-direction in meters/second (m/s) dt = 0.001; % time resolution in seconds (s) tMax = 20; % maximum time range in seconds (s) epsilon = 0.7; % coefficient of restitution (no units) dtw = dt / 1000; % smaller time interval for Euler's method
% PROCESS % Solve differential equation using Euler's method of smaller time intervals
% OUTPUT % Height versus time graph % Velocity versus time graph
clear all % always clear workspace before running
%% Set initial condition to = 0; % initial time on clock (seconds)
%% 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 (s) y = zeros(1, npts); % preallocate y array (height, m) vy = zeros(1, npts); % preallocate the vy array (velocity, m/s) y(1) = yo; vy(1) = vyo; tArray(1) = to;
% Calculate position as a function of time t = to; for i = 1:npts-1 tVar = t + dt; vyVar = vy(i) + ay * dt; yVar = y(i) + vy(i) * dt;
% Detect floor if (yVar <= 0) % hit the floor with dt time resolution % Calculate Euler's method with a smaller dt tVar = t; while (1) vy(i) = vy(i) + ay * dtw; 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 + f * dtw; % assume equal fractions vy(i+1) = -vyVar * epsilon; y(i+1) = 0; % floor height is set to 0 else % ball didn't interact with the floor vy(i+1) = vyVar; y(i+1) = yVar; end tArray(i+1) = tVar; t = tVar;end