function out = whereDidItGo( file)
% Read the data from the Excel file
data = xlsread( file) ;
velocities = data( :, 1 ) ; % First column: initial velocities
angles = data( :, 2 ) ; % Second column: launch angles
g = 9.8 ; % Acceleration due to gravity ( m/ s^ 2 )
maxHeight = - Inf; % Variable to store maximum height
maxIndex = 0 ; % Index of the trajectory with the maximum height
% Initialize arrays to store
time and horizontal distance
timeArray = zeros( length( velocities) , 1 ) ;
horizontalDistanceArray = zeros( length( velocities) , 1 ) ;
% Calculate the maximum height and corresponding
time and distance
for i = 1 : length( velocities)
% Convert angle to radians
theta = deg2rad( angles( i) ) ;
% Time of flight
timeOfFlight
= ( 2 * velocities
( i
) * sin ( theta
) ) / g
;
% Horizontal displacement
horizontalDistance
= velocities
( i
) * cos ( theta
) * timeOfFlight
;
% Calculate the maximum vertical height
height
= ( velocities
( i
) ^ 2 * ( sin ( theta
) ) ^ 2 ) / ( 2 * g
) ;
% Store values for comparison
timeArray( i) = round( timeOfFlight) ;
horizontalDistanceArray( i) = round( horizontalDistance) ;
% Update maximum height and index
if height > maxHeight
maxHeight = height;
maxIndex = i;
end
end
% Get the results for the maximum height projectile
totalTime = timeArray( maxIndex) ;
totalHorizontalDistance = horizontalDistanceArray( maxIndex) ;
% Create output sentence
out
= sprintf ( 'After %d seconds the firework debris will be %d meters away!' , totalTime
, totalHorizontalDistance
) ;
% Generate
time vector
for plots
t = linspace( 0 , totalTime, 100 ) ;
% Prepare subplots
figure;
% Calculate trajectories for plotting
for i = 1 : length( velocities)
% Convert angle to radians
theta = deg2rad( angles( i) ) ;
% Horizontal and vertical positions
x
= velocities
( i
) * cos ( theta
) * t
; % Horizontal position
y
= velocities
( i
) * sin ( theta
) * t
- 0.5 * g
* t.
^ 2 ; % Vertical position
vY
= velocities
( i
) * sin ( theta
) - g
* t
; % Vertical velocity
% Plot horizontal displacement
subplot( 3 , 1 , 1 ) ;
hold on;
plot( t, x, 'b' ) ;
% Highlight the trajectory with the maximum height
if i == maxIndex
plot( t, x, 'r' , 'LineWidth' , 2 ) ;
end
end
title( 'Time vs. Horizontal Displacement' ) ;
xlabel( 'Time (seconds)' ) ;
ylabel( 'Horizontal Displacement (meters)' ) ;
% Plot vertical displacement
subplot( 3 , 1 , 2 ) ;
hold on;
for i = 1 : length( velocities)
% Re- calculate for y
theta = deg2rad( angles( i) ) ;
y
= velocities
( i
) * sin ( theta
) * t
- 0.5 * g
* t.
^ 2 ; % Vertical position
plot( t, y, 'b' ) ;
% Highlight the trajectory with the maximum height
if i == maxIndex
plot( t, y, 'r' , 'LineWidth' , 2 ) ;
end
end
title( 'Time vs. Vertical Displacement' ) ;
xlabel( 'Time (seconds)' ) ;
ylabel( 'Vertical Displacement (meters)' ) ;
% Plot vertical velocity
subplot( 3 , 1 , 3 ) ;
hold on;
for i = 1 : length( velocities)
% Re- calculate for vertical velocity
theta = deg2rad( angles( i) ) ;
vY
= velocities
( i
) * sin ( theta
) - g
* t
; % Vertical velocity
plot( t, vY, 'b' ) ;
% Highlight the trajectory with the maximum height
if i == maxIndex
plot( t, vY, 'r' , 'LineWidth' , 2 ) ;
end
end
title( 'Time vs. Vertical Velocity' ) ;
xlabel( 'Time (seconds)' ) ;
ylabel( 'Vertical Velocity (m/s)' ) ;
% Adjust layout
tight_layout( ) ;
end
out = whereDidItGo( 'info1.xlsx' ) ;
disp( out) ;
ZnVuY3Rpb24gb3V0ID0gd2hlcmVEaWRJdEdvKGZpbGUpCiAgICAlIFJlYWQgdGhlIGRhdGEgZnJvbSB0aGUgRXhjZWwgZmlsZQogICAgZGF0YSA9IHhsc3JlYWQoZmlsZSk7CiAgICB2ZWxvY2l0aWVzID0gZGF0YSg6LCAxKTsgJSBGaXJzdCBjb2x1bW46IGluaXRpYWwgdmVsb2NpdGllcwogICAgYW5nbGVzID0gZGF0YSg6LCAyKTsgICAgICAlIFNlY29uZCBjb2x1bW46IGxhdW5jaCBhbmdsZXMKCiAgICBnID0gOS44OyAlIEFjY2VsZXJhdGlvbiBkdWUgdG8gZ3Jhdml0eSA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252ZXJ0IGFuZ2xlIHRvIHJhZGlhbnMKICAgICAgICB0aGV0YSA9IGRlZzJyYWQoYW5nbGVzKGkpKTsKICAgICAgICAKICAgICAgICAlIEhvcml6b250YWwgYW5kIHZlcnRpY2FsIHBvc2l0aW9ucwogICAgICAgIHggPSB2ZWxvY2l0aWVzKGkpICogY29zKHRoZXRhKSAqIHQ7ICUgSG9yaXpvbnRhbCBwb3NpdGlvbgogICAgICAgIHkgPSB2ZWxvY2l0aWVzKGkpICogc2luKHRoZXRhKSAqIHQgLSAwLjUgKiBnICogdC5eMjsgJSBWZXJ0aWNhbCBwb3NpdGlvbgogICAgICAgIHZZID0gdmVsb2NpdGllcyhpKSAqIHNpbih0aGV0YSkgLSBnICogdDsgJSBWZXJ0aWNhbCB2ZWxvY2l0eQogICAgICAgIAogICAgICAgICUgUGxvdCBob3Jpem9udGFsIGRpc3BsYWNlbWVudAogICAgICAgIHN1YnBsb3QoMywgMSwgMSk7CiAgICAgICAgaG9sZCBvbjsKICAgICAgICBwbG90KHQsIHgsICdiJyk7CiAgICAgICAgCiAgICAgICAgJSBIaWdobGlnaHQgdGhlIHRyYWplY3Rvcnkgd2l0aCB0aGUgbWF4aW11bSBoZWlnaHQKICAgICAgICBpZiBpID09IG1heEluZGV4CiAgICAgICAgICAgIHBsb3QodCwgeCwgJ3InLCAnTGluZVdpZHRoJywgMik7CiAgICAgICAgZW5kCiAgICBlbmQKICAgIHRpdGxlKCdUaW1lIHZzLiBIb3Jpem9udGFsIERpc3BsYWNlbWVudCcpOwogICAgeGxhYmVsKCdUaW1lIChzZWNvbmRzKScpOwogICAgeWxhYmVsKCdIb3Jpem9udGFsIERpc3BsYWNlbWVudCAobWV0ZXJzKScpOwogICAgCiAgICAlIFBsb3QgdmVydGljYWwgZGlzcGxhY2VtZW50CiAgICBzdWJwbG90KDMsIDEsIDIpOwogICAgaG9sZCBvbjsKICAgIGZvciBpID0gMTpsZW5ndGgodmVsb2NpdGllcykKICAgICAgICAlIFJlLW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