Engineering 6 Spring 2016
Lab 3 – Section A07
Please create a script (.m file) named XX_labN.m where XX are your initials and N
is the lab number.
At the top, put the following comments:
%
%
%
When submitting your script, make sure to click the honor pledge, or else your
quiz will not be submitted. Also make sure to submit before the end of the time,
or the lab will be considered late! If your section ends at 1:00 PM, submit by 12:59
AM.
Problem 1
A biologist is interested in the average height of the trees in a certain area. After several years
of measuring, he obtains the following data set:
Time (Years) Average Height
(m)
1 2.2
2 2.7
3 3.1
4 3.8
5 4.9
6 6.2
Table 1: Data set for the tree height
Our biologist hasn’t got the any idea of the mechanism behind these numbers, but still wants to
have a line through the data points. He decides to resort to the use of polynomial functions. He
wants to fit the data using a second order polynomial, which takes the form:
𝑦 = 𝑎𝑥2 + 𝑏𝑥 + 𝑐
1) First create an array x containing the data of time and an array y containing the data of
height.
2) Use polyfit() to find the values for a, b and c for given values of y and x, where year
number as x, and average height as y. This function applies the method of least
squares.
3) Now you have found the coefficients, compute the height at year 0 and year 7 using the
function polyval(). Use fprintf() function to show your answer!
“The height at year 0 is __ m, and at year 7 is ___ m.”
4) Plot the data points (scattered points) and the best fit polynomial curve in one figure
(including 0 and 7 seconds). Add title, x/y labels, and legend to your plot.
Example figure output
Problem 2
In this problem, we will compare differentiation using MATLAB with a mathematical closed form
differentiation expression.
a. Create a vector x starts at 0 and stop at 2*pi with a step of 0.001.
b. Plot the vector y where 𝑦 = sin(𝑥)
c. Differentiate the vector y numerically using the Matlab function diff and assign the output to z.
d. On a new figure, plot (z/step) the vector created by dividing z by the step. On the same figure,
plot the mathematical closed form expression of the differentiation
𝑑𝑦
𝑑𝑥
of sin(x). Add legend, titles
and labels.