Determine the Trajectory of a Thrown Ball Due: Friday November 19, 2010 before 1
ID: 3621793 • Letter: D
Question
Determine the Trajectory of a Thrown BallDue: Friday November 19, 2010 before 11:59 pm
For this project, you are to write a Matlab script that will have a user guess the angle and velocity a ball needs to be thrown to land on a target a specified distance away.
The program is to prompt for the distance (in meters) a target is away from the spot were the ball is being thrown. The program will print out the result of the throw and plot the trajectory.
We will assume the target is 4 meters wide. So if the throw is within 2 meters of the target, we will consider the target hit by the ball.
The first number read in is a real value that indicates the distance from the spot were the ball is thrown to the target. If this value is not greater than or equal to zero (or if the value is non-numeric), an error message should be displayed and prompt for a new distance.
Once the distance is known to be valid, display the distance to the target and prompt the user for the angle and initial velocity of the thrown ball. The angle will be given in degrees and must be a numeric value from zero to 90. Any other input (numeric or non-numeric) is to result in an error message and reprompt the user for a valid angle. The initial velocity must a numeric value greater than zero. Any other input (numeric or non-numeric) is to result in an error message and reprompt the user for a valid angle. The input statement should be used to prompt the user for these values. If the user continues to enter invalid input, your function should continue to display error messages and reprompt for a valid inout.
For this problem we will assume that gravity is the only force acting on the ball. The earth's gravity, g, is -9.80665 m/s 2. You are to use at least the value of -9.81 m/s 2 in your program. You may use a higher precision value if you wish. For this problem we will also assume that the earth is perfectly flat. Thus the target is at the same height as the thrower. The thrower is assumed to have a height of zero.
When a ball is thrown, its height at time t is:
y(t) = y 0 + v y0t + 1/2gt 2 (formula 1)
where
y o is the initial height (zero for this program)
v y0 is the initial vertical velocity of the ball
t is given in seconds
The distance of the ball at time t is:
x(t) = x 0 + v x0t (formula 2)
where
x o is the initial distance (zero for this program)
v x0 is the initial horizontal velocity of the ball
t is given in seconds
The initial vertical and horizontal velocity can be determined from an initial velocity, v 0, and angle of ? degrees from the earth's surface. These calculations are:
v y0 = v 0 sin ? (formula 3)
and
v x0 = v 0 cos ? (formula 4)
Note that the angle ? is being given by the user in degrees, if you want the value to be given in radians, you will need to convert. To convert from degrees to radian multiply the degree by pi and divide by 180.
To determine how far the ball was thrown, we first must determine how long the ball is in the air. We can solve for t from formula 1 when y(t) = 0. This results in the formula of:
t = -2 v y0 / g (formula 5)
Note: Since formula 1 is a quadratic equation, there will normally be two values for t when the height, y(t), is zero. One time is when the ball lands as expressed in formula 5. The other time is when the ball is thrown. This would be at time zero and would be of no use in solving our problem, so that formula is ignored in this discussion.
We can now plug in this value of t into formula 2 to determine the distance the ball was thrown. If this value is less than the distance to the target minus 2 meters, the ball was underthrown. In this case, your program is to display a message something like: "The ball was short X meters." If this value is greater than the distance to the target plus 2 meters, the ball was overthrown. In this case, your program is to display a message something like: "The ball was long by X meters." Otherwise, the ball hit the target. In this case, your program is to display a message something like: "The ball hit the target."
After you have displayed this message, your program is to print a table that lists the x and y coordinates (the trajectory of the ball) for 40 instances between launch and landing.
As an example:
Welcome to the Project 3,
Determine the Trajectory of a Thrown Ball
The program you are running is capable of
calculating the trajectory of a thrown ball and
whether the ball will hit a target a known distance away.
Please enter the distance to the target in meters:
100
You have entered a distance of 100.00 meters.
Now we need to know the velocity and angle at which the ball
will be thrown. Please enter the angle (in degrees):
10
Please enter the velocity (in m/sec):
54
The ball thrown with velocity 54.00 m/s at an angle of 10.00 degrees
traveled 101.66 meters and hit the target!! YEAH!!
The table showing the x,y coordinates of the ball, for the time period
under examination will be printed next
Press enter to continue
enter
Trajectory of the Ball
Time X Y
========== ========== ==========
0.0000E+00 0.0000E+00 0.0000E+00
4.7793E-02 2.5416E+00 4.3695E-01
9.5586E-02 5.0832E+00 8.5150E-01
1.4338E-01 7.6249E+00 1.2436E+00
1.9117E-01 1.0166E+01 1.6134E+00
2.3897E-01 1.2708E+01 1.9607E+00
2.8676E-01 1.5250E+01 2.2856E+00
3.3455E-01 1.7791E+01 2.5881E+00
3.8234E-01 2.0333E+01 2.8682E+00
4.3014E-01 2.2875E+01 3.1259E+00
4.7793E-01 2.5416E+01 3.3612E+00
5.2572E-01 2.7958E+01 3.5740E+00
5.7352E-01 3.0499E+01 3.7645E+00
6.2131E-01 3.3041E+01 3.9326E+00
6.6910E-01 3.5583E+01 4.0782E+00
7.1690E-01 3.8124E+01 4.2015E+00
7.6469E-01 4.0666E+01 4.3023E+00
8.1248E-01 4.3208E+01 4.3807E+00
8.6028E-01 4.5749E+01 4.4368E+00
Press enter to continue
enter
Time X Y
========== ========== ==========
9.0807E-01 4.8291E+01 4.4704E+00
9.5586E-01 5.0832E+01 4.4816E+00
1.0037E+00 5.3374E+01 4.4704E+00
1.0514E+00 5.5916E+01 4.4368E+00
1.0992E+00 5.8457E+01 4.3807E+00
1.1470E+00 6.0999E+01 4.3023E+00
1.1948E+00 6.3541E+01 4.2015E+00
1.2426E+00 6.6082E+01 4.0782E+00
1.2904E+00 6.8624E+01 3.9326E+00
1.3382E+00 7.1165E+01 3.7645E+00
1.3860E+00 7.3707E+01 3.5740E+00
1.4338E+00 7.6249E+01 3.3612E+00
1.4816E+00 7.8790E+01 3.1259E+00
1.5294E+00 8.1332E+01 2.8682E+00
1.5772E+00 8.3873E+01 2.5881E+00
1.6250E+00 8.6415E+01 2.2856E+00
1.6728E+00 8.8957E+01 1.9607E+00
1.7206E+00 9.1498E+01 1.6134E+00
Press enter to continue
enter
Time X Y
========== ========== ==========
1.7683E+00 9.4040E+01 1.2436E+00
1.8161E+00 9.6582E+01 8.5150E-01
1.8639E+00 9.9123E+01 4.3695E-01
1.9117E+00 1.0166E+02 1.9073E-06
GoodBye.
Explanation / Answer
here you go. i just tested it for the example given, but it should work for everything distance = -1; v0 = -1; theta = -1; g = -9.81; while(distance < 0) distance = input("Enter the distance for the target (must be >= 0): "); if (distance < 0) disp("Please enter a distance greater than or equal to 0") end end disp("The distance of the target is") disp(distance) while(v0 < 0) v0 = input("What is the initial velocity of the projectile? "); if(v0 < 0) disp("Please enter a velocity greater than 0") end end while(theta < 0 || theta > 90) theta = input("What is the initial angle the projectile will be fired at? "); if(theta < 0 || theta > 90) disp("Please enter an angle between 0 and 90 degrees") end end theta = theta*(pi/180); v_y0 = v0*sin(theta); v_x0 = v0*cos(theta); t = -2*v_y0/g; x = 0 + v_x0*t; if((distance - x) > 2) disp("You were short") elseif((distance - x) < -2) disp("You were long") else disp("You hit!") end disp("Time X Y") t = 0:t/40:t; for i=1:41 x = v_x0*t(i); y = 0 + v_y0*t(i)+0.5*g*t(i)^2; data = [t(i),x,y]; disp(data) endRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.