fork download
  1. function out = whereDidItGo(file)
  2. % Read the data from the Excel file
  3. data = xlsread(file);
  4. velocities = data(:, 1); % First column: initial velocities
  5. angles = data(:, 2); % Second column: launch angles
  6.  
  7. g = 9.8; % Acceleration due to gravity (m/s^2)
  8. maxHeight = -Inf; % Variable to store maximum height
  9. maxIndex = 0; % Index of the trajectory with the maximum height
  10.  
  11. % Initialize arrays to store time and horizontal distance
  12. timeArray = zeros(length(velocities), 1);
  13. horizontalDistanceArray = zeros(length(velocities), 1);
  14.  
  15. % Calculate the maximum height and corresponding time and distance
  16. for i = 1:length(velocities)
  17. % Convert angle to radians
  18. theta = deg2rad(angles(i));
  19.  
  20. % Time of flight
  21. timeOfFlight = (2 * velocities(i) * sin(theta)) / g;
  22.  
  23. % Horizontal displacement
  24. horizontalDistance = velocities(i) * cos(theta) * timeOfFlight;
  25.  
  26. % Calculate the maximum vertical height
  27. height = (velocities(i)^2 * (sin(theta))^2) / (2 * g);
  28.  
  29. % Store values for comparison
  30. timeArray(i) = round(timeOfFlight);
  31. horizontalDistanceArray(i) = round(horizontalDistance);
  32.  
  33. % Update maximum height and index
  34. if height > maxHeight
  35. maxHeight = height;
  36. maxIndex = i;
  37. end
  38. end
  39.  
  40. % Get the results for the maximum height projectile
  41. totalTime = timeArray(maxIndex);
  42. totalHorizontalDistance = horizontalDistanceArray(maxIndex);
  43.  
  44. % Create output sentence
  45. out = sprintf('After %d seconds the firework debris will be %d meters away!', totalTime, totalHorizontalDistance);
  46.  
  47. % Generate time vector for plots
  48. t = linspace(0, totalTime, 100);
  49.  
  50. % Prepare subplots
  51. figure;
  52.  
  53. % Calculate trajectories for plotting
  54. for i = 1:length(velocities)
  55. % Convert angle to radians
  56. theta = deg2rad(angles(i));
  57.  
  58. % Horizontal and vertical positions
  59. x = velocities(i) * cos(theta) * t; % Horizontal position
  60. y = velocities(i) * sin(theta) * t - 0.5 * g * t.^2; % Vertical position
  61. vY = velocities(i) * sin(theta) - g * t; % Vertical velocity
  62.  
  63. % Plot horizontal displacement
  64. subplot(3, 1, 1);
  65. hold on;
  66. plot(t, x, 'b');
  67.  
  68. % Highlight the trajectory with the maximum height
  69. if i == maxIndex
  70. plot(t, x, 'r', 'LineWidth', 2);
  71. end
  72. end
  73. title('Time vs. Horizontal Displacement');
  74. xlabel('Time (seconds)');
  75. ylabel('Horizontal Displacement (meters)');
  76.  
  77. % Plot vertical displacement
  78. subplot(3, 1, 2);
  79. hold on;
  80. for i = 1:length(velocities)
  81. % Re-calculate for y
  82. theta = deg2rad(angles(i));
  83. y = velocities(i) * sin(theta) * t - 0.5 * g * t.^2; % Vertical position
  84. plot(t, y, 'b');
  85.  
  86. % Highlight the trajectory with the maximum height
  87. if i == maxIndex
  88. plot(t, y, 'r', 'LineWidth', 2);
  89. end
  90. end
  91. title('Time vs. Vertical Displacement');
  92. xlabel('Time (seconds)');
  93. ylabel('Vertical Displacement (meters)');
  94.  
  95. % Plot vertical velocity
  96. subplot(3, 1, 3);
  97. hold on;
  98. for i = 1:length(velocities)
  99. % Re-calculate for vertical velocity
  100. theta = deg2rad(angles(i));
  101. vY = velocities(i) * sin(theta) - g * t; % Vertical velocity
  102. plot(t, vY, 'b');
  103.  
  104. % Highlight the trajectory with the maximum height
  105. if i == maxIndex
  106. plot(t, vY, 'r', 'LineWidth', 2);
  107. end
  108. end
  109. title('Time vs. Vertical Velocity');
  110. xlabel('Time (seconds)');
  111. ylabel('Vertical Velocity (m/s)');
  112.  
  113. % Adjust layout
  114. tight_layout();
  115. end
  116. out = whereDidItGo('info1.xlsx');
  117. disp(out);
  118.  
Success #stdin #stdout #stderr 0.38s 49472KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
warning: Functions for spreadsheet style I/O (.xls .xlsx .sxc .ods .dbf .wk1
etc.)  are provided in the io package.  See
<https://o...content-available-to-author-only...e.io/io/>.

Please read <https://w...content-available-to-author-only...e.org/missing.html> to learn how you can
contribute missing functionality.
error: 'xlsread' undefined near line 3 column 12
error: called from
    whereDidItGo at line 3 column 10
    /home/PQufnF/prog.octave at line 116 column 5