Math 371 - Project 2,3 2
Math 371 - Project 2,3 2
Math 371 - Project 2,3 2
Instructor: M. Yousuf
Abdulaziz Bakhurji
201941870
Section: 02
Serial: 19
Problem statement:
1. Write Matlab code as I have explained in the class.
2. Find least square polynomial of degree 2 and degree 3 using the data
mentioned below.
3. Compute x and y values using the function f(x) in your serial number group.
4. Plot the graphs of y = f(x) and P(x) = a0 + a1x + a2x^2 and Q(x) = b0 + b1x +
b2x^2 + b3x^3 in the same window.
5. Plot graph of f(x) in black, P(x) in red and Q(x) in green color.
6. Use legend command with dot for f(x), star for P(x) and circle for Q(x).
Serial Numbers 17 – 20
y = x^3 + 2x^2 , 0 ≤ x ≤ 1, n = 20
x y x2 x3 x4 x5 x6 xy x2y x3y
count ________________________________
1 0 0 0 0 0 0 0 0 0 0
2 0.052632 0.005686 0.00277 0.000146 7.7E-06 4E-07 0 0.000299 1.58E-05 8E-07
3 0.105263 0.023327 0.01108 0.001166 0.000123 1.29E-05 1.4E-06 0.002456 0.000259 2.72E-05
4 0.157895 0.053798 0.024931 0.003936 0.000622 9.81E-05 1.55E-05 0.008494 0.001341 0.000212
5 0.210526 0.097974 0.044321 0.009331 0.001964 0.000414 8.71E-05 0.020626 0.004342 0.000914
6 0.263158 0.156728 0.069252 0.018224 0.004796 0.001262 0.000332 0.041244 0.010854 0.002856
7 0.31579 0.230938 0.099723 0.031492 0.009945 0.00314 0.000992 0.072928 0.02303 0.007273
8 0.368421 0.321475 0.135734 0.050007 0.018424 0.006788 0.002501 0.118438 0.043635 0.016076
9 0.421053 0.429217 0.177285 0.074646 0.03143 0.013234 0.005572 0.180723 0.076094 0.03204
10 0.473684 0.555037 0.224377 0.106284 0.050345 0.023848 0.011296 0.262912 0.124537 0.058991
11 0.526316 0.699811 0.277008 0.145794 0.076734 0.040386 0.021256 0.368321 0.193853 0.102028
12 0.578947 0.864412 0.33518 0.194052 0.112346 0.065042 0.037656 0.500449 0.289734 0.167741
13 0.631579 1.049716 0.398892 0.251932 0.159115 0.100494 0.06347 0.662978 0.418723 0.264457
14 0.684211 1.256597 0.468144 0.320309 0.219159 0.149951 0.102598 0.859777 0.588269 0.4025
15 0.736842 1.485931 0.542936 0.400058 0.29478 0.217206 0.160047 1.094896 0.806766 0.594459
16 0.789474 1.738592 0.623269 0.492054 0.388464 0.306682 0.242117 1.372572 1.08361 0.855481
17 0.842105 2.015454 0.709141 0.597172 0.502881 0.423479 0.356614 1.697225 1.429242 1.203572
18 0.894737 2.317393 0.800554 0.716285 0.640887 0.573425 0.513064 2.073457 1.855198 1.659914
19 0.947368 2.645284 0.897507 0.85027 0.805519 0.763123 0.722959 2.506058 2.37416 2.249205
20 1 3 1 1 1 1 1 3 3 3
________________________________
Total 10 18.94737 6.842105 5.263158 4.317539 3.688584 3.240577 14.84385 12.32366 10.61775
Matlab code:
clear all
a=0; b=1; N =20;
syms x
f=@(x) x.^3+2*x.^2;
x = linspace(a,b,N);
for i = 1:20
y(i)=f(x(i));
end
X = sum(x);
Y = sum(y);
xy = x.*y;
x2 = x.^2;
x3 = x.^3;
x4 = x.^4;
x5 = x.^5;
x6 = x.^6;
x2y = x.^2.*y;
x3y = x.^3.*y;
XY = sum(xy);
X2 = sum(x2);
X3 = sum(x3);
X4 = sum(x4);
X5 = sum(x5);
X6 = sum(x6);
X2Y = sum(x2y);
X3Y = sum(x3y);
% degree 2 polinomial:
A1 = [N X X2
X X2 X3
X2 X3 X4];
B1 = [Y;XY;X2Y];
c1 = A1\B1;
% degree 3 polynomial:
A2 = [N X X2 X3
X X2 X3 X4
X2 X3 X4 X5
X3 X4 X5 X6];
B2 = [Y;XY;X2Y;X3Y];
c2 = A2\B2;
P=@(x) c1(1)+c1(2)*x+c1(3)*x.^2;
Q=@(x) c2(1)+c2(2)*x+c2(3)*x.^2+c2(4)*x.^3;
plot(x,f(x),'.-k',x,P(x),'.-r',x,Q(x),'.-g')
grid on
legend('f(x)','P(x)','Q(x)')
disp(' ________________________________');
disp(' x y x2 x3 x4 x5 x6 xy x2y
x3y' );
disp(' ________________________________');
Y1=[x;y;x2;x3;x4;x5;x6;xy;x2y;x3y];
fprintf(1,' %6.7f %6.7f %6.7f %6.7f %6.7f %6.7f %6.7f %6.7f %6.7f %6.7f \n',Y1);
disp(' ________________________________ ')
S=[X,Y,X2,X3,X4,X5,X6,XY,X2Y,X3Y];
fprintf(1,' %6.7f %6.7f %6.7f %6.7f %6.7f %6.7f %6.7f %6.7f %6.7f %6.7f \n',S);