2. 2052 |
Write a function in MATLAB called HTBALL to calculate the height of a ball based on a single input, the elapsed time. The elapsed time will be a single variable in the range of 0 to 6. Predict the value by fitting a 2nd order polynominal to the data points shown below using the polyfit command. Use the command polyval to determine the height of the ball at the inputted time. timevals=[0,.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6]; height=[0,46,84,114,136,150,156,154,144,126,100,66,24];
|
%Please note to save the code as HTBALL.m
%the file should have the same name as function name
%code to calculate height is given below
function height_of_ball=HTBALL(time)
timevals=[0,.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5,6];
height=[0,46,84,114,136,150,156,154,144,126,100,66,24];
%here 2 means the 2nd order polynomial fit is done
line = polyfit(timevals,height,2);
height_of_ball=polyval(line,time)
%the rest of the code is to see the fit of the curve
%y1 = polyval(line,timevals);
%figure
%plot(timevals,height,'o')
%hold on
%plot(timevals,y1)
%hold off
%the function should finish with end statement
end
Get Answers For Free
Most questions answered within 1 hours.