Projectile motion with loop function

10 次查看(过去 30 天)
JXH1996
JXH1996 2016-2-22
回答: BhaTTa 2024-7-24
I need to create a loop for the projectile motion of a ball
I plot it but it continue even once the y-position is below 0 (the floor)
clc
clear all
V0=100 %%%Initial velociton
Sx0=0 %%%Initial x-position
Sy0=0 %%%Initial y-position
angle=45 %%%angle of projection
g=9.807 %%%Gravitational constant
ymin=0 %%%minimum value for y-position
t = 0:0.1:20 %%%Time
Vx=V0*cos(angle); %%%x-velocity
Vy=V0*sin(angle); %%%y-velocity
Sx=Sx0+Vx*t %%%x-position
Sy=Sy0+(Vy*t)-(0.5*g*t.^2) %%%y-position
if Sy>=min
plot (Sx,Sy)
end
grid on

回答(1 个)

BhaTTa
BhaTTa 2024-7-24
To simulate the projectile motion of a ball and stop the simulation once the y-position goes below the floor (y = 0), you can use a loop that updates the position and velocity of the ball at each time step. Here's how you can modify your code to include this logic:
clc;
clear all;
% Parameters
V0 = 100; % Initial velocity (m/s)
Sx0 = 0; % Initial x-position (m)
Sy0 = 0; % Initial y-position (m)
angle = 45; % Angle of projection (degrees)
g = 9.807; % Gravitational constant (m/s^2)
ymin = 0; % Minimum value for y-position (the floor)
dt = 0.1; % Time step (s)
% Convert angle to radians
angle_rad = deg2rad(angle);
% Initial velocities
Vx = V0 * cos(angle_rad); % x-velocity (m/s)
Vy = V0 * sin(angle_rad); % y-velocity (m/s)
% Initialize positions
Sx = Sx0;
Sy = Sy0;
% Initialize time
t = 0;
% Initialize arrays to store positions for plotting
Sx_array = [];
Sy_array = [];
% Loop to update positions and velocities
while Sy >= ymin
% Update positions
Sx = Sx + Vx * dt;
Sy = Sy + Vy * dt - 0.5 * g * dt^2;
% Update y-velocity
Vy = Vy - g * dt;
% Update time
t = t + dt;
% Store positions for plotting
Sx_array = [Sx_array, Sx];
Sy_array = [Sy_array, Sy];
end
% Plot the trajectory
plot(Sx_array, Sy_array);
xlabel('x-position (m)');
ylabel('y-position (m)');
title('Projectile Motion of a Ball');
grid on;
Explanation
  1. Parameters: Define the initial conditions, gravitational constant, and time step.
  2. Convert Angle to Radians: Convert the projection angle from degrees to radians.
  3. Initial Velocities: Calculate the initial velocities in the x and y directions.
  4. Initialize Positions: Set the initial positions.
  5. Initialize Time: Set the initial time to zero.
  6. Initialize Arrays: Create arrays to store the positions for plotting.
  7. Loop: Use a while loop to update the positions and velocities at each time step until the y-position (Sy) goes below the minimum value (ymin).
  • Update the x and y positions using the current velocities and time step.
  • Update the y-velocity to account for the effect of gravity.
  • Increment the time by the time step.
  • Store the current positions in the arrays.
  1. Plot the Trajectory: Plot the stored x and y positions to visualize the projectile motion.
This code will simulate the projectile motion of the ball and stop the simulation once the ball hits the floor (y = 0). Adjust the parameters as needed for your specific scenario.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by