# Figure 1 Least squares line y = Ax + B and least square

Document Sample

```					                                       Example: Least Squares Curve Fit

data1=[5.435,1;4.635,1.28;3.835,1.7;3.035,2.2;2.325,2.97;1.435,4.35;.635,7.5]

data1 =
5.4350    1.0000
4.6350    1.2800
3.8350    1.7000
3.0350    2.2000
2.3250    2.9700
1.4350    4.3500
0.6350    7.5000
cd Math420/NumericalMethods7
%least squares line using lsline from text
[A,B]=lsline(data1(:,1)’,data1(:,2)’)

A =   -1.2108
B =    6.6902

x=min(data1(:,1)):.001:max(data1(:,1));
plot(data1(:,1),data1(:,2),’x’,x,A*x+B) %plot least squares line against data
%Compute the least square exponential using least squares line on ln(y)=Ax+ln(C)
[A2,B2]=lsline(data1(:,1)’,log(data1(:,2))’)
A2 =   -0.4060
B2 =    2.1146

hold on %use the same figure
plot(x,exp(B2).*exp(A2*x))%plot the least squares exponential

8

7

6

5

4

LS Line
3

LS EXP
2

1

0
0    1       2            3         4        5       6

Figure 1: Least squares line y = Ax + B and least square exponential y = CeAx

% Now do a
quadratic polynomial fit and a cubic poynomial fit
1.0000    5.4350   29.5392
1.0000    4.6350   21.4832
1.0000    3.8350   14.7072
1.0000    3.0350    9.2112
1.0000    2.3250    5.4056
1.0000    1.4350    2.0592

1
1.0000    0.6350     0.4032

A =   1.0e+03 *
0.0070    0.0213     0.0828
0.0213    0.0828     0.3603
0.0828    0.3603     1.6689

B =
21.0000
42.4743
130.3413
% Using MATLAB to solve
C=A\B
C =
9.0748
-3.3833
0.3582
plot(data1(:,1),data1(:,2),’x’,x,C(1)+C(2).*x+C(3)*x.^2; % plot quadratic against data
%% Now for the cubic
hold on % will use the same second figure
FCcub=[ones(size(data1(:,1))),data1(:,1),data1(:,1).^2,data1(:,1).^3]% [xk^0,xk^1,xk^2].xk^3]

FCcub =
1.0000   5.4350    29.5392   160.5457
1.0000   4.6350    21.4832    99.5747
1.0000   3.8350    14.7072    56.4022
1.0000   3.0350     9.2112    27.9561
1.0000   2.3250     5.4056    12.5681
1.0000   1.4350     2.0592     2.9550
1.0000   0.6350     0.4032     0.2560
B=FCcub’*data1(:,2)
B =
21.0000
42.4743
130.3413
497.4902

A=FCcub’*FCcub
A =   1.0e+04 *
0.0007     0.0021    0.0083     0.0360
0.0021     0.0083    0.0360     0.1669
0.0083     0.0360    0.1669     0.8043
0.0360     0.1669    0.8043     3.9820

C=A\B %solve using matlab
C =
10.6549
-5.9473
1.3672
-0.1108
plot(data1(:,1),data1(:,2),’x’,x,C(1)+C(2).*x+C(3)*x.^2+C(4)*x.^3);
% Try quartic (4th degree)
FC4th=[ones(size(data1(:,1))),data1(:,1),data1(:,1).^2,data1(:,1).^3,data1(:,1).^4]
FC4th =
1.0000    5.4350   29.5392 160.5457 872.5658
1.0000    4.6350   21.4832  99.5747 461.5290
1.0000    3.8350   14.7072  56.4022 216.3025
1.0000    3.0350    9.2112  27.9561   84.8467
1.0000    2.3250    5.4056  12.5681   29.2208
1.0000    1.4350    2.0592   2.9550    4.2404
1.0000    0.6350    0.4032   0.2560    0.1626
B=FC4th’*data1(:,2)

2
B =   1.0e+03 *
0.0210
0.0425
0.1303
0.4975
2.1242
A=FC4th’*FC4th

A =    1.0e+06 *
0.0000    0.0000   0.0001    0.0004    0.0017
0.0000    0.0001   0.0004    0.0017    0.0080
0.0001    0.0004   0.0017    0.0080    0.0398
0.0004    0.0017   0.0080    0.0398    0.2010
0.0017    0.0080   0.0398    0.2010    1.0292
C=A\B
C =
11.9331
-8.8582
3.2718
-0.5793
0.0385
figure;plot(data1(:,1),data1(:,2),’x’,x,C(1)+C(2).*x+C(3)*x.^2+C(4)*x.^3+C(5)*x.^4);
%Examining the graphs it appears that the exponential fit will better demonstrate the trend of the graph.
% The graph for the 4th degree polynomial is very close, but this is a function of few data points
% compared to the number of unknowns (7 data points, 5 unknowns)

8                                                   8

7
7

6
6

5
5
4

4
3

LS Cubic           3
2

1                                                   2

0                                                   1
0   1    2     3       4            5   6        0     1     2     3     4     5      6

Figure 2: Least squares quadratic y = C(1) + C(2)x + C(3)x2 and least square cubic y = C(1) + C(2)x +
C(3)x2 + C(4)x3 on the left, and least squares quartic on the right.

3
% Compare with the polynomial of interpolation (in this case ,
the interpolation polynomial is surprisingly good!)

P=int_poly(data(:,2)’,data(:,1)’,x);
plot(x,P,data1(:,1),data1(:,2),’o’)

8

7

6

5

4

3

2

1

0
0   1   2     3     4    5    6

Figure 3: The interpolation polynomial for the data.

4

```
DOCUMENT INFO
Shared By:
Categories:
Stats:
 views: 21 posted: 5/22/2011 language: English pages: 4