View the code and add the following: This is also inside the code to show you where it must be added. % use an if/else statement to check to see if the ball is moving % down (negative). If it is, then drag has an opposite sign % as gravity in the acceleration formula. Otherwise gravity % and drag have the same sign. Calculated the acceleration % in the y direction inside the if/else statement. % Now calculate the acceleration in the x . % ******************************************************* % ***************************************************** % calculate the new velocity at the end of the time step % this will have X and Y components, so you need a variable % for each. One is velFinalX and the other is velFinalY. % ******************************************************* % ****************************************************** % Get a new velocity vector and angle given the X and Y % The velocity is the variable "vel" and angle is "angle" %******************************************************* CODE: % Create a program to plot the motion of the ping pong ball with drag. % The program will take inputs for velocity in m/s and angle of launch. % note that with a high speed camera I estmated a launch speed for a ping % pong ball could be 30 m/s. clear clc vel=input('input the initial velocity in m/sec: '); angle=input('input the angle of launch in degrees: '); % convert the degrees into radians so MATLAB likes it angle=angle*pi/180; % set initial position and time x(1)=0; % meters y(1)=.01; % meters time(1)=0; % seconds mass=.00247; %kg ping pong ball .00247 Kg g=-9.8; % m/sec^2 c=-0.0005; % coefficient of drag with density and area in this constant index=1; % so I can load an array for plotting % start to increment the motion velx=vel*cos(angle); VelocityX(1)=velx; vely=vel*sin(angle); VelocityY(1)=vely; % set a time step deltaTime=.001 % seconds height=y(1); while height>=0 % it has not hit ground yet index=index+1; % break velocity into its components velx=vel*cos(angle); vely=vel*sin(angle); % ******************************************************* % use an if/else statement to check to see if the ball is moving % down (negative). If it is, then drag has an opposite sign % as gravity in the acceleration formula. Otherwise gravity % and drag have the same sign. Calculated the acceleration % in the y direction inside the if/else statement. % Now calculate the acceleration in the x . % ******************************************************* % ***************************************************** % calculate the new velocity at the end of the time step % this will have X and Y components, so you need a variable % for each. One is velFinalX and the other is velFinalY. % ******************************************************* % ****************************************************** % Get a new velocity vector and angle given the X and Y % The velocity is the variable "vel" and angle is "angle" %******************************************************* % now save my values at this time step time(index)=time(index-1)+deltaTime; VelocityX(index)=velFinalX; VelocityY(index)=velFinalY; % and distance numbers distX=velx*deltaTime; distY=vely*deltaTime; % save distance values x(index)=x(index-1)+distX; y(index)=y(index-1)+distY; height=y(index); end plot(x,y) title('distance traveled by ping pong ball in meters') xlabel('horixontal distance traveled (meters)') ylabel('vertical distance traveled (meters)')
Control structures
Control structures are block of statements that analyze the value of variables and determine the flow of execution based on those values. When a program is running, the CPU executes the code line by line. After sometime, the program reaches the point where it has to make a decision on whether it has to go to another part of the code or repeat execution of certain part of the code. These results affect the flow of the program's code and these are called control structures.
Switch Statement
The switch statement is a key feature that is used by the programmers a lot in the world of programming and coding, as well as in information technology in general. The switch statement is a selection control mechanism that allows the variable value to change the order of the individual statements in the software execution via search.
View the code and add the following: This is also inside the code to show you where it must be added.
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 6 images