Recall that the velocity and positions can be calculated usi

Recall that the velocity and positions can be calculated using the following relationships: v(t) = at + v_o x(t) = 1/2 at^2 +v_o t + x_o For an object in free-fall, acceleration is -9.80665 m/s^2. Your function takes six input arguments Acceleration a Initial time to Final time tf Number of points n Initial position xo Initial velocity vo The function file returns no output variables, but generates plots of position and velocity for the range of time specified by the user of the command window. Use the subplot command to show x(t) as a top plot and v(t) as the bottom plot. Run a case with the following input parameters: Acceleration -9.80665 m/s^2 Initial time 0s Final time 2s Number of points 20 points Initial position 1.3 m Initial velocity 0.1 m/s On your printout of the function file, write (with pen, etc.) how you called your function in the command window. Your command must be a single command line. Submission: 1 function m-file, plot.

Solution

Let us create a function file with name computeVelocityandPosition.m

Inside this file, create the function that accepts all the 6 parameters as defined above and computes the velocity and position in the following way:

function [] = computeVelocityandPosition(a,t0,tf,n,x0,v0)

% Assuming that the value of n is given to compute the velocity and times at different values of tf ending with % 2sec

xt = zeros(20,1);

vt = zeros(20,1);

% computing the values of xt and vt for the range of t = [0.1,0.2,...2]

t = [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8,1.9,2.0];

for i = 1:n

% note that 0.1*i will represent the time at which we are calculating the xt and vt values

xt(i,1) = a*(0.1*i)+v0;

vt(i,1) = (1/2)*a*(0.1*20)^2+v0*(0.1*i)+x0

end

Use the subplot command to plot the curves

To call the function from the command window, simply write the name of the function with the 6 input parameters passed simultaeously while calling. Ensure that while calling the function the directory in which the file is saved is the same in which you are trying to make a call to the function.

Example: computeVelocityandPosition(-9.80655,0,2,20,1.3,0.1)

 Recall that the velocity and positions can be calculated using the following relationships: v(t) = at + v_o x(t) = 1/2 at^2 +v_o t + x_o For an object in free-

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site