TP5: Linear Least Square Regression With Multiple Parameters
TP5: Linear Least Square Regression With Multiple Parameters
Code
x=[0.8;1.4;2.7;3.8;4.8;4.9]
y=[0.69;1.00;2.02;2.39;2.34;2.83]
figure
plot(x,y,'*b')
hold on
%calculate leastsquare
X=[ones(length(y),1),x,x.^2,x.^3,x.^4]
phi=inv(X'*X)*X'*y
ymodel=X*phi
plot(x,ymodel)
err=sum((y-ymodel).^2)
0.0882
The coefficients
phi =
1.7067
-2.8294
2.4564
-0.6600
0.0581
= 1.7067-2.8294x+2.4564x2-0.66x3+0.0581x4
1
Figure
2. Find the best fit model of each the data set below (plot in subplot)
Table 1 show the value of average maximum temperature, average minimum temperature and
total precipitation per each month
Duration Jan Feb Mar Apr May June July Aug Sep Oct Nov Dec Annual
Avg.max.temperature 57.8 62.2 63.4 76.0 84.3 88.2 90.7 90.7 86.9 79.5 68.0 60.5 76.3
Avg.min.temperature 39.2 43.7 50.6 59.1 67.2 72 74 74.1 70 59.2 49.4 42.2 58.5
Total precipitation 6.06 5.56 4.50 5.79 3.28 6.68 6.80 5.19 4.55 3.19 3.15 5.97 60.7
From the comparison, 4th degree is best fit model for ymodel, umodel and wmodel
2
Code
x=[1;2;3;4;5;6;7;8;9;10;11;12;13]
y=[57.8;62.2;63.4;76.0;84.3;88.2;90.7;90.7;86.9;79.5;68.0;60.5;76.3]
u=[39.2;43.7;50.6;59.1;67.2;72.0;74.0;74.1;70.0;59.2;49.4;42.2;58.5]
w=[6.06;5.56;4.50;5.79;3.28;6.68;6.80;5.19;4.55;3.19;3.15;5.97;60.70]
figure
subplot(3,1,1)
plot(x,y,'*b')
hold on
%calculate leastsquare
Y=[ones(length(y),1),x]
phiy=inv(Y'*Y)*Y'*y
ymodel=Y*phiy
subplot(3,1,1)
plot(x,ymodel)
erry=sum((y-ymodel).^2)
subplot(3,1,2)
plot(x,u,'*g')
hold on
%calculate leastsquare
U=[ones(length(y),1),x,x.^2,x.^3]
phiu=inv(U'*U)*U'*u
umodel=U*phiu
subplot(3,1,2)
plot(x,umodel)
erru=sum((u-umodel).^2)
subplot(3,1,3)
plot(x,w,'*y')
hold on
%calculate leastsquare
W=[ones(length(y),1),x,x.^2,x.^3]
phiw=inv(W'*W)*W'*w
wmodel=W*phiw
subplot(3,1,3)
plot(x,wmodel)
errw=sum((w-wmodel).^2)
erry =
91.1004
erru =
78.4832
errw =
332.0646
The coefficients
3
phiy =
73.6972
-22.8440
9.8543
-1.1932
0.0439
phiu =
52.3224
-19.6893
9.3176
-1.1659
0.0436
phiw =
30.3516
-30.1155
10.1866
-1.2681
0.0518
ymodel = a + a1x + a2x2 + a3x3 + a4x4= 73.6972 –22.8440 x + 9.8543x2 – 1.1932x3 + 0.0439x4
umodel = a + a1x + a2x2 + a3x3 + a4x4= 52.3224 – 19.6893x + 9.3176x2 –1.1659 x3 +0.0518x4
wmodel = a + a1x + a2x2 + a3x3 + a4x4= 30.3516 – 30.1155x+ 10.1866x2 – 1.2681x3+ 0.0349x4
4
Figure
3. Fit the model to the data set and estimate kc by Henrry isotherm model qe = kcCe
Code
x=[0.09;0.15;0.55;0.35;0.97;1.55;1.78;2.35;3.18]
y=[4.18;3.07;1.85;3.75;2.95;1.05;3.89;1.99;1.01]
figure
plot(x,y,'*b')
hold on
%calculate leastsquare
X=[x]
phi=inv(X'*X)*X'*y
ymodel=X*phi
5
plot(x,ymodel)
err=sum((y-ymodel).^2)
The value of errors
err =
51.9647
The coefficients(ke)
phi =
0.9941
Figure
6
7