Position and velocity of a Ball If a stationary ball is rele
Position and velocity of a Ball If a stationary ball is released at a height n_a above the surface of the Earth with a vertical velocity v_0 the position and velocity of the ball as a function of time will be given by the equations: h(t) = 1/2 gt^2 + v_0 t + h_0 v(t) = gt + v_0 where g is the acceleration due to gravity (9.81 m/s^2), h is the height above the surface of the Earth (assuming no air friction), and v is the vertical component of velocity. In this problem, you will write a MATIAB script that prompts a user for the initial height of the ball (in meters) and velocity of the ball (in meters per second), and plots the height and velocity as a function of time on the same set of axes for the first 10 seconds of flight. Be sure to include a proper title, axis labels, a legend and axis grid lines in your plots. Note: To receive full credit, you must use MATLAB commands, not the plot editor. Below is some code to help you get started. Notice that good programming practices are used and the time vector uses an appropriate increment that makes the curves meaningful. % Purpose: % To calculate and display the trajectory of a ball % thrown upward at a user-specified height and speed. % Define variables: % g -- Acceleration due to gravity (m/s^2) % h -- Height (m) % h_0 -- Initial height (m) % t -- Time (s) % v --Vertical Speed (m/s) % v_0 -- Initial\' Vertical Speed (m/s) %Initialize the acceleration due to gravity g - -9.81; % Prompt the user for the initial velocity. V_0 = input (\'Enter the initial velocity of the ball: \'); % Prompt the user for the initial height h_0 = input (\'Enter the initial height of the ball;\') % we will calculate the speed and height for the first %10 seconds of flight. T = 0:0.5:10; h = zeros (size(t)); %
Solution
clc
clear all
% to calculate and display trajectory of a ball
% define variables
% g = accelaration due to gravity (m/s2)
% h = height (m)
% h0 = initial height (m)
% t = time (sec)
% v = velocity (m/s)
% v0 = initial velcoty (m/s)
%initialise the accelaration due to gravity
g = -9.81;
%prompt the user for initial velocity
v0 =input(\'rnter the initial velocity of the ball :\');
%prompt the user for initial height
h0 =input(\'enter the initial height of the ball :\');
t =0:0.5:10
h =zeros(size(t));
v =zeros(size(t));
h =0.5*g.*t.^2 + v0.*t + h0
v =g.*t + v0
plot(t,h,\'color\',\'r\'); hold on; % plot the curve of h and v vs time
plot(t,v,\'color\',\'b\');
