2. You are analyzing a dataset, PolyData.mat, that contains stress-related hormo
ID: 3801714 • Letter: 2
Question
2. You are analyzing a dataset, PolyData.mat, that contains stress-related hormone data (1st row) as a function of time (2nd row) a. Use MATLAB to fit separate 3 th 20th. and 37th order polynomial functions to the data. Plot the data and each of your polynomial estimates on the same graph and label them. b. In three sentences or less, which of the above functions do you consider to be the best approximation of the data and why? c. Using MATLAB, fit an interpolating cubic spline to the data and plot the results overlaid with the data.
Explanation / Answer
part (a) and part(c)
% change the file name and variables names according to your data
% filename = 'example.mat';
% variables = {'x','y'};
% structuredArr = load(filename, variables{:})
% x = structuredArr.x;
% y = structuredArr.y;
x = [-1.37 -0.69 -1.22 0.31 -0.51 -0.19 0.100 0.660 0.3100 -0.240 -0.81 -1.2 -0.50 -0.22 -0.38 -0.0100 1.720 0.200 1.47 1.72 1.61 0.31 0.58 0.33 -0.49 -0.78 -0.90 -1.27 0.77 1.38 3.32 3.97 3.65 3.89 2.17 1.30 0.44 -1.21];
y = [0 0.25 0.50 0.75 1 1.25 1.50 1.75 2 2.25 2.5 2.75 3.0 3.25 3.5 3.75 4 4.25 4.50 4.75 5 5.25 5.50 5.75 6.0 6.25 6.50 6.75 7.0 7.25 7.50 7.75 8.0 8.25 8.50 8.75 9 9.25];
% fitting 3th degree polynomial
p = polyfit(x,y,3);
x1 = linspace(min(x), max(x));
y1 = polyval(p,x1);
plot(x1, y1, '--b');
%comment the following line out to plot everthing seperately
hold on;
p = polyfit(x,y,6);
x1 = linspace(min(x), max(x));
y1 = polyval(p,x1);
plot(x1, y1, '-.r');
p = polyfit(x,y,20);
x1 = linspace(min(x), max(x));
y1 = polyval(p,x1);
plot(x1, y1, '-g');
p = polyfit(x,y,37);
x1 = linspace(min(x), max(x));
y1 = polyval(p,x1);
plot(x1, y1, '-k');
hold off;
% fitting cubic spline to the data
xx = linspace(min(x), max(x));
yy = spline(x,y,xx);
plot(x, y,':*b', xx, yy);
------------------------------------------------------------------------------------------------------------------------
part(b)
If you see the individuals graphs, it's straight forward that the ploynomial of degree 3, followed by degree 6 are more accurate compared to 20th and 37th.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.