MATLAB Code:
close all
clear
clc
% Problem (a)
fprintf('Problem
(a)\n----------------------------------------\n')
% Data Points
x = [-2 1 3]';
y = [3 5 8]';
A = [ones(size(x)) x x.^2]; % system of equations
p = A\y;
fprintf('Model: y = (%.4f) + (%.4f)*x + (%.4f)*x^2\n', p)
xx = -4:0.01:4;
yy = p(1) + p(2)*xx + p(3)*xx.^2;
plot(x,y,'o',xx,yy), xlabel('x'), ylabel('y')
title('Degree 2 Polynomial Fit (Problem (a))')
legend('Data Points', 'Polynomial Fit', 'Location',
'northwest')
% Problem (b)
fprintf('\nProblem
(b)\n----------------------------------------\n')
x = 1:20; x = x';
y = [106.99 112.69 118.07 133.29 140.39 140.38 149.28 141.32 145.94
160.93 151.82 166.52 169.29 167.31 165.53 176.1 166.55 164.05 185.5
184.46]';
A = [ones(size(x)) x x.^2 x.^3 x.^4 x.^5 x.^6 x.^7 x.^8 x.^9 x.^10
x.^11 x.^12 x.^13 x.^14 x.^15 x.^16 x.^17 x.^18 x.^19]; % system of
equations
p = A\y;
fprintf('Model: y = (%.4f) + (%.4f)*x + (%.4f)*x^2 + (%.4f)*x^3 +
(%.4f)*x^4 + (%.4f)*x^5 + (%.4f)*x^6 + (%.4f)*x^7 + (%.4f)*x^8 +
(%.4f)*x^9 + (%.4f)*x^10 + (%.4f)*x^11 + (%.4f)*x^12 + (%.4f)*x^13
+ (%.4f)*x^14 + (%.4f)*x^15 + (%.4f)*x^16 + (%.4f)*x^17 +
(%.4f)*x^18 + (%.4f)*x^19\n', p)
xx = 0:0.01:25;
yy = p(1) + p(2)*xx + p(3)*xx.^2 + p(4)*xx.^3 + p(5)*xx.^4 +
p(6)*xx.^5 + p(7)*xx.^6 + p(8)*xx.^7 + p(9)*xx.^8 + p(10)*xx.^9 +
p(11)*xx.^10 + p(12)*xx.^11 + p(13)*xx.^12 + p(14)*xx.^13 +
p(15)*xx.^14 + p(16)*xx.^15 + p(17)*xx.^16 + p(18)*xx.^17 +
p(19)*xx.^18 + p(20)*xx.^19;
figure, plot(x,y,'o',xx,yy), xlabel('x'), ylabel('y')
title('Degree 19 Polynomial Fit (Problem (b))')
legend('Data Points', 'Polynomial Fit', 'Location',
'northwest')
axis([0 25 -20000 20000])
Output:
Problem (a)
----------------------------------------
Model: y = (4.0000) + (0.8333)*x + (0.1667)*x^2
Problem (b)
----------------------------------------
Model: y = (-1516876.2747) + (5341329.6562)*x + (-8194918.8757)*x^2
+ (7388782.0758)*x^3 + (-4433433.6649)*x^4 + (1894828.5409)*x^5 +
(-601596.9639)*x^6 + (145850.8328)*x^7 + (-27501.3898)*x^8 +
(4081.2961)*x^9 + (-479.9333)*x^10 + (44.8234)*x^11 +
(-3.3179)*x^12 + (0.1933)*x^13 + (-0.0087)*x^14 + (0.0003)*x^15 +
(-0.0000)*x^16 + (0.0000)*x^17 + (-0.0000)*x^18 + (0.0000)*x^19
Plots:
Choose one of the following projects to do. Follow the directions, write up clear project reports (typed is preferred, with inclusion of any necessary pictures or attached pages of written work)....