MATLAB
Task 4: Plotting the trajectory of a ball thrown
Write a user-defined function named throwBallFunc:
Which takes two arguments (parameters): Velocity and Angle
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
Next, create a time vector t that has 10000 values between 0 and 20, inclusive. The values must be
sequential and not random
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
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
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:
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)
Call the function throwBallFunc with two user-input values (velocity and angle)
Store the return values from the function in variables named x, y, hitDistance and hitTime
Display the words: “The ball hits the ground at a distance of X meters”, where X is the hitDistance from the function throwBallFunc.
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
% 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:
Get Answers For Free
Most questions answered within 1 hours.