Question

MATLAB Task 4: Plotting the trajectory of a ball thrown Write a user-defined function named throwBallFunc:...

MATLAB

Task 4: Plotting the trajectory of a ball thrown

Write a user-defined function named throwBallFunc:

  1. Which takes two arguments (parameters): Velocity and Angle

  2. Inside of the function, define some values (you can pick your own variable names) • Initial height of ball at release = 1.5m

    • Gravitational acceleration = 9.8m/s2

  3. Next, create a time vector t that has 10000 values between 0 and 20, inclusive. The values must be

    sequential and not random

  4. If x is the vector with distances and y is the vector with heights, the equations below describe their dependence on the time vector and all the other parameters (initial height h, gravitational acceleration g, initial ball velocity v, angle of velocity vector in degrees θ). In your function, write expressions to solve for x and y:

    • x(t) = vcos(θ ∗ π )t. We multiply θ by π to convert degrees to radians.

180 180

• y(t)=h+vsin(θ∗ π )t−1gt2 180 2

  1. Approximate when the ball hits the ground

    • Find the index when the height first becomes negative (you can use the built-in function find) • The distance at which the ball hits the ground will be the value of x at the index found above • The time at which the ball hits the ground will be the value of t at the index found above

  2. Returns x, y, and the distance and time at which the ball hits the ground

Hint: A sample function header which returns 4 values:

function [x, y, hitDistance, hitTime] = throwBallFunc(velocity, angle)

Write a script task4.m:

  1. Ask the user to enter value for the following variables:

    • Velocity of ball at release (in m/s)
    • Angle of the velocity vector at time of release (in degrees)

  2. Call the function throwBallFunc with two user-input values (velocity and angle)

  3. Store the return values from the function in variables named x, y, hitDistance and hitTime

  4. Display the words: “The ball hits the ground at a distance of X meters”, where X is the hitDistance from the function throwBallFunc.

  5. Display the words: “The ball hits the ground at Y seconds”, where Y is the hitTime from the function throwBallFunc.

6. Plot

the ball’s trajectory

  • Plot the ground as a dashed black line. This should be a horizontal line going from 0 to the maximum value of x. The height of this line should be 0. See help plot for line colors and styles.

  • Hold on the figure (use hold on)

  • Open a new figure (use the command figure)

  • Plot the ball’s height on the y axis and the distance on the x axis (plot)

  • Label the axes meaningfully and give the figure a title (use xlabel, ylabel, and title)

  • Specify the range of the axis to clearly see the trajectory of the ball until the moment it hits the ground (use xlim and ylim)

    Here is one possible sample run (for this plot, xlim([0 3]) and ylim([-1 2]) were used):

    Enter velocity of ball at release (in m/s): 4
    Enter the angle of the velocity vector at time of release (in degrees): 45 The ball hits the ground at a distance of 2.585441 meters
    The ball hits the ground at 0.914091 seconds

2

1.5

1

0.5

0

-0.5

Ball Trajectory

-1
0 0.5 1 1.5 2 2.5 3

Distance (m)

Figure 2: The trajectory of a ball thrown

Homework Answers

Answer #1

% throwBallFunc.m

function [x, y, hitDistance, hitTime] = throwBallFunc(velocity, angle)
% Matlab function throwBallFunc to calculate the height and range of a
% ball thrown at an initial velocity and angle and having an initial
% height for time between 0 to 20 seconds and return the maximum range
  
% define variables to store Gravitational acceleration and initial
% height
h = 1.5;
g = 9.8;
% create a time vector t that has 10000 values between 0 and 20, inclusive
t = linspace(0,20,10000 );
  
% calculate the range and height of the ball at various time
x = velocity.*cos((angle.*pi)./180).*t;
y = h + velocity*sin((angle*pi)/180).*t - 0.5 * g* t.^2;
% get the index of ball when it hits the ground i.e height = 0
idx = find(y<=0);
% get the distance at hit
hitDistance = x(idx(1));
% get the time of hit
hitTime = t(idx(1));
end

%end of throwBallFunc.m


% task4.m : Matlab main script to input the initial velocity and angle and plot the
% ball trajectory
% input of velocity
velocity = input('Enter velocity of ball at release (in m/s): ');
% input of angle
angle = input('Enter the angle of the velocity vector at time of release (in degrees): ');
% call throwBallFunc
[x,y,hitDistance, hitTime] = throwBallFunc(velocity,angle);
% display the output
fprintf('The ball hits the ground at a distance of %f meters\n',hitDistance);
fprintf('The ball hits the ground at %f seconds\n',hitTime);
% plot the ball trajectory
plot(x,y);
xlabel('Distance(m)');
ylabel('Ball Height(m)');
title('Ball Trajectory');
xlim([0 hitDistance+0.5]);
ylim([-1 max(y)+0.5]);
hold on;
plot(x, x*zeros(length(x)),'k--')
hold off;
%end of script

Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
2. (15pts, 5pts each) A ball is thrown from the top of a building that is...
2. (15pts, 5pts each) A ball is thrown from the top of a building that is H = 40 m height. It has an initial velocity v0 that is at angle of  = 53° below the horizontal direction, as shown in the below Figure. The ball hits the ground t = 2.0 s after being thrown. You may neglect air resistance. a) Find the magnitude of the initial velocity v0 of the ball. b) Find the velocity of the ball just...
Using MATLAB The range of an object shot at an angle θ (with respect to x-axis),...
Using MATLAB The range of an object shot at an angle θ (with respect to x-axis), with the initial velocity of V0 (in the absence of air resistance), is calculated by the following formula: range=(Vo^2/g)(sin(2theta)) where (0<=theta<=pi/2) And the trajectory of object is given by:     h=tan(theta).x-(g/2Vo^2*cos^2(theta)).x^2 .Where h is the height of the object at each x location and g = 9.81 m/s2. a) Using π/8 increment size for the angle and V0 = 10 m/s, plot the trajectories of...
A projectile motion (i.e. cannon) can be modeled via the following equations: x=u cos⁡θ t y=-0.5...
A projectile motion (i.e. cannon) can be modeled via the following equations: x=u cos⁡θ t y=-0.5 g t^2+u sin⁡θ t Where: x: Position of the cannonball after t seconds in the x-direction (meters) y: Position of the cannonball after t seconds in the y-direction (meters) u: Initial velocity of the cannonball (meters per second) g: Acceleration due to gravity (meters per second squared) t: Time (seconds) In this question, we are trying to see the effects of the angle ϴ...
If you throw a ball at an angle a and an initial speed of 1m/s, then...
If you throw a ball at an angle a and an initial speed of 1m/s, then its trajectory at time t is given by (x, y) = (t cos(a), tsin(a) − 5t^2). What is the angle you need to throw at to ensure that it hits the ground (y = 0) the furthest possible from where you are standing (at (x, y) = (0, 0))? How far will it go?
Homework 3 As you know very well from physics, if you hit a ball forward with...
Homework 3 As you know very well from physics, if you hit a ball forward with a certain angle and a certain speed, it makes an orbital movement. The height of the ball is determined by the equation given below. y(t)=y_0+v_y0 t-1/2 gt^2 Where y0 is the first location of the ball, vy0 is the initial vertical velocity of the ball and g is the gravitational acceleration. After the ball is thrown, the horizontal distance is determined by the following...
A ball is thrown straight upward from a height of 1.0 m with a velocity of...
A ball is thrown straight upward from a height of 1.0 m with a velocity of 2.0 m/s. (a) Write an equation to describe the ball’s height as a function of time. (b) What is the maximum height the ball reaches? (c) After how many seconds does the ball hit the ground? 3. On a drive from Philadelphia to Newark, you drive 2/3 of the distance at 80 kilometers per hour. You then get stuck in a construction zone and...
You kick a soccer ball with a force (F) of 1200 N at an angle of...
You kick a soccer ball with a force (F) of 1200 N at an angle of 45 degrees. The soccer ball has an initial velocity of (v i ) of 5 m/s at 0 degrees , a mass (m) of 0.4 kg, and the time of contact (t) between yo ur foot and the ball is 0.01 s. • H ow far does the ball travel before it hits the ground (∆s x ) ? • Express your initial velocity...
Using MATLAB to solve the problem: The USS Missouri is a battleship with 16-inch guns. A...
Using MATLAB to solve the problem: The USS Missouri is a battleship with 16-inch guns. A projectile is fired from one of the guns at an angle of elevation ϴ = 42 degree from the sea-level with a velocity Vo = 820 m/s. Neglecting air resistance and assuming the projectile is fired across the sea, using Matlab determine: (a) How much time the projectile will be in flight (tf). (b) How far away from the battleship the projectile will hit...
Using MATLAB Write a user-defined MATLAB function for the following math function: y(x)= (-0.2x^3 + 7x^2)e^-0.3x...
Using MATLAB Write a user-defined MATLAB function for the following math function: y(x)= (-0.2x^3 + 7x^2)e^-0.3x The input to the function is x and the output is y. Write the function such that x can be a vector (use element-by-element operations). (a) Use the function to calculate y(-1.5) and y(5). (b) Use the function to make a plot of the function y(x) for -2 ≤ x ≤ 6.
A ball is thrown across a playing field from a height of h = 5 ft...
A ball is thrown across a playing field from a height of h = 5 ft above the ground at an angle of 45° to the horizontal at the speed of 20 ft/s. It can be deduced from physical principles that the path of the ball is modeled by the function y= -(32/400)x^2+x+5 where x is the distance in feet that the ball has traveled horizontally. 1) Find the maximum height attained by the ball. (Round your answer to three...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT