Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

Make slight changes to this MatLab code, but make it have the same results when running…

  1. User

    Make slight changes to this MatLab code, but make it have the same results when running it:

    %% 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 (unitless) %% 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 tTest = t+dt; vyTest = vy(i)+ay*dt; yTest = y(i) + vy(i)*dt;

    if (yTest <= 0) % => hit the floor with dt time resolution % ———————————————————————————— do linear interpolation as an approximation f = (y(i) - 0) / (y(i) - yTest); % fraction in terms of height vyTest = vy(i) + f*(vyTest-vy(i)); % assume equal fractions tTest = t+fdt; % assume equal fractions vy(i+1) = -vyTestepsilon; y(i+1) = 0; % floor height is set to 0

    else                             % => ball didn't interact w/ the wall
    vy(i+1) = vyTest;
    y(i+1) = yTest;
    end
    

    tArray(i+1) = tTest; t = tTest; 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 %% plot results figure(1); clf; plot(tArray,y,'b','lineWidth',1.8); xlabel('time (seconds)'); ylabel('height (meters)'); title(['coefficient of restitution = ',num2str(epsilon)]);

    figure(2); clf; plot(tArray,vy,'b','lineWidth',1.8); xlabel('time (seconds)'); ylabel('velocity (meters)'); title(['coefficient of restitution = ',num2str(epsilon)]);

  2. Assistant

    %% 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 (unitless) %% 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 tTest = t+dt; vyTest = vy(i)+ay*dt; yTest = y(i) + vy(i)*dt;

    if abs(yTest) <= 0.00001 % => hit the floor with dt time resolution f = (y(i) - 0) / (y(i) - yTest); % fraction in terms of height vyTest = vy(i) + f*(vyTest-vy(i)); % assume equal fractions tTest = t+fdt; % assume equal fractions vy(i+1) = -vyTestepsilon; y(i+1) = 0; % floor height is set to 0

    else % => ball didn't interact w/ the wall vy(i+1) = vyTest; y(i+1) = yTest; end

    tArray(i+1) = tTest; t = tTest; end

    vy(i+1) = vy(i) + ay*dt; y(i+1) = y(i) + vy(i)*dt; if( abs(y(i+1)) <= 0.00001 ) % => hit the floor with dt time resolution vy(i+1) = - vy(i)*epsilon; y(i+1) = 0; end %% plot results figure(1); clf; plot(tArray,y,'b','lineWidth',1.8); xlabel('time (seconds)'); ylabel('height (meters)'); title(['coefficient of restitution = ',num2str(epsilon)]);

    figure(2); clf; plot(tArray,vy,'b','lineWidth',1.8); xlabel('time (seconds)'); ylabel('velocity (meters)'); title(['coefficient of restitution = ',num2str(epsilon)]);

  3. User

    For "% ———————————————————————————— do linear interpolation as an approximation", modify the "if" and "else" functions. Make it do the same thing, though/

  4. Assistant

    % ———————————————————————————— do linear interpolation as an approximation if yTest <= 0 % => hit the floor with dt time resolution f = y(i) / (y(i) - yTest); % fraction in terms of height vyTest = vy(i) + f*(vyTest-vy(i)); % assume equal fractions tTest = t+fdt; % assume equal fractions vy(i+1) = -vyTestepsilon; y(i+1) = 0; % floor height is set to 0 else % => ball didn't interact w/ the wall vy(i+1) = vyTest; y(i+1) = yTest; end