Sie sind auf Seite 1von 2

1

Least square fit of a straight line to data:

The equation of a straight line is y  mx  b


Consider the data points ( x1 , y1 ) , ( x 2 , y 2 )…….etc.
n
Error is defined as  (m, b)   (mxi  b  y i ) .
2

i 1
For the best fit, this error should be minimum.
 
Therefore,  0 and  0.
m b
   n
2
n

Now,  
m m  i 1
 ( mx i  b  y i )  = 
 i 1 m
(mxi  b  y i ) 2
n

=  2.(mxi  b  y i ) (mxi  b  y i )
i 1 m
n n n n
= 2 (mxi  b  y i ) xi = 2m xi  2b xi   y i xi = 0
2
(1)
i 1 i 1 i 1 i 1

 n n
Similarly,  0  m xi  nb   y i (2)
b i 1 i 1
From (1) and (2),
 n
  n 
 n

i 1
x i    yi 
 b    i 1 
 n n    n
2  m  
  xi  xi    y i xi 
 i 1 i 1   i 1 
n n n n n n n
n xi y i   y i  xi  y i  xi   x i  xi y i
2

Slope, m  and Intercept, b 


i 1 i 1 i 1 i 1 i 1 i 1 i 1
n n n n .
n  xi  (  xi ) n xi  ( xi )
2 2 2 2

i 1 i 1 i 1 i 1

Example:
For the data points (1,2), (2,3), (3,4), (4,5)
n n
n  4, x
i 1
i  1  2  3  4  10 , y
i 1
i  2  3  4  5  14
n n

 xi yi  1 2  2  3  3  4  4  5  40 , x 2
i  1  1  2  2  3  3  4  4  30
i 1 i 1

 m  4  40  14 210  160  140  20  1 , b


14  30  10  40 420  400 20
   1.
4  30  10 120  100 20 4  30  10 2 120  100 20
2

FORTRAN PROGRAM FOR LEAST SQUARE FITTING:

C Least square fitting of a straight line to data points


write(*,*)'Give the Number of Points'
read(*,*)n
write(*,*)'Write the data points: x,y'
sumx=0.0
sumy=0.0
sumsqx=0.0
sumxy=0.0
do i=1,n
read(*,*)x,y
sumx=sumx+x
sumy=sumy+y
sumsqx=sumsqx+x*x
sumxy=sumxy+x*y
enddo
deno=n*sumsqx-sumx*sumx
slope=(n*sumxy-sumx*sumy)/deno
b=(sumsqx*sumy-sumx*sumxy)/deno
write(*,*)'Slope, Intercept= ',slope,b
stop
end

Das könnte Ihnen auch gefallen