Simplify this MatLab code: %% simulating a bouncing ball: my code % This script…
- User
Simplify this MatLab 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
%% Simplified MatLab code: simulating a bouncing ball % 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|
% Set initial conditions to = 0; % initial time on clock (seconds) yo = 2.5; % initial height of ball (m) vyo = 5; % initial velocity of ball in the y-direction (m/s) dt = 0.001; % time resolution (s) tMax = 20; % maximum time range (s) epsilon = 0.7; % coefficient of restitution (no units)
% 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;