Curve Fitting Least Square Fit Method: Sandeep Kumar

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 27

CURVE FITTING

LEAST SQUARE FIT METHOD


PRESENTED BY-
SAKSHI HANSARIA
SANDEEP KUMAR
OBJECTIVES

 WHAT IS CURVE FITTING


 WHAT IS LEAST SQUARE METHOD
 DESIGNING A FORTRAN PROGRAM FOR IT
 APPLICATIONS
WHAT IS CURVE FITTING??

Curve fitting examines the relationship between one or more independent variables and dependent
variables with the goal of defining the ‘best fit’ model.
WE WILL USE
LEAST SQUARE METHOD
TO FIT THE CURVE
LEAST SQAURE METHOD

Observation points (Data points):


Independent variable x x1 x2 x3 x4 x5
Dependent variable y y1 y2 y3 y4 y5

f(x) f(x1) f(x2) f(x3) f(x4) f(x5)

Error: y

at x = x1; e1 = y 1
– f(x1)
e4 f(x)
at x = x2; e2 = y 2 e3
e5

– f(x2)
at x = x3; e3 = y 3 e2
e1
–.f(x3) .
. .
at x = xi; ei = yi – f(xi) x

We assume our function to be


f(x) = a + bx
𝑛
𝐸=∑ 𝑒 𝑖2
𝑖=1

𝑛
𝐸 =∑ ( 𝑦 𝑖 − 𝑓 ( 𝑥 𝑖 ) )
2

𝑖=1
𝑛
𝐸=∑ ( 𝑦 𝑖 − ( 𝑏 𝑥𝑖 + 𝑎 ) )
2

𝑖=1

We want to minimize the error as much as possible .


Here, b and a are unknown quantities.

𝛿𝐸 𝛿𝐸
=0 and =0
𝛿𝑎 𝛿𝑏
𝑛 𝛿𝐸
=0
𝐸=∑ 𝑒 𝑖2 𝛿𝑎

( )
𝑖=1 𝑛
𝛿𝐸 𝛿
∑ [ 𝑦 𝑖 − ( 𝑏 𝑥 𝑖 − 𝑎) ] =0
2
𝑛 =
𝐸 =∑ ( 𝑦 𝑖 − 𝑓 ( 𝑥 𝑖 ) ) 𝛿 𝑎 𝛿𝑎
2
𝑖

∑ 𝑦𝑖=𝑛𝑎+𝑏 ∑ 𝑥𝑖
𝑖=1
𝑛 (A)
𝐸=∑ ( 𝑦 𝑖 − ( 𝑏 𝑥𝑖 + 𝑎 ) )
2

𝑖=1

We want to minimize the error as much as possible. 𝛿𝐸


Here, b and a are unknown quantities. =0
𝛿𝑏

(∑ [ )
𝑛
𝛿𝐸 𝛿𝐸 𝛿𝐸 𝛿 2
𝛿𝑎
=0 and
𝛿𝑏
=0 = 𝑦 𝑖 − ( 𝑏 𝑥 𝑖 − 𝑎 ) ] =0
𝛿𝑏 𝛿𝑏 𝑖

∑ 𝑥𝑖 𝑦𝑖=𝑎 ∑ 𝑥𝑖+𝑏∑ 𝑥𝑖 2
(B)
On solving the two equation (A) and (B), we get:

( ∑ 𝑦 ∑ 𝑥 2 ) − ( ∑ 𝑥 ∑ 𝑥𝑦 )
𝑎= 2
𝑛∑ 𝑥 − ( ∑ 𝑥 )
2

( ∑ 𝑥 ∑ 𝑦 ) − ( 𝑛 ∑ 𝑥𝑦 )
𝑏= 2
( ∑ 𝑥 ) − 𝑛 ∑ 𝑥2

Therefore, we can now define our best fit line


as:
𝑦 =𝑎 +𝑏 𝑥
REGRESSION

 IT IS THE MEASURE OF THEAVERAGE RELATIONSHJIP BETWEEN TWO


OR MORE VARIABLES .
 REGRESSION ANALYSIS ATTEMPTS TO ESTABLISH THE NATURE OF
RELATIONSHIP BETWEEN VARIABLES AND THEREBY PROVIDES
MECHANISM FOR PREDICTION.
Designing a Fortran program
PROGRAMMING IN PHYSICS?????
 HANDLING LARGE DATA SHEETS
 MANIPULATING DATA
 ANALYSING DATA
WHY FORTRAN?????
 It is the oldest high level programming language.
 It was developed in 1957.
 Based on user feedback it was developed and many
versions came up.
 The most extensive used version was FORTRAN 77 .
 FORTRAN 77
 MERITS

SIMPLE EFFICIENT
COMPLIER
 Presently
we are using fortran 95 which has
many new features as compared to Fortran
77.
FLOWCHART

START Read values


of x and y
from file
ENTER NO. OF
DATA PAIRS (n) ( ∑ 𝑦 ∑ 𝑥 2 ) − ( ∑ 𝑥 ∑ 𝑥𝑦 )
sumx = sumx + x(i) 𝑎= 2
𝑛∑ 𝑥 − ( ∑ 𝑥 )
sumy = sumy + y(i) 2

SET sumxy = sumxy + x(i)y(i)


sumx = 0 sumxx = sumxx + x(i)2
sumyy = sumyy + y(i)2
sumy = 0
sumxy = 0 ( ∑ 𝑥 ∑ 𝑦 ) − ( 𝑛𝑥 ∑ 𝑥𝑦 )
𝑏= 2
sumxx = 0
sumyy = 0 (∑ 𝑥 ) − 𝑛 ∑ 𝑥 2

PRINT a and b
DO i = 1 to n

END
NEXT i
ALGORITHM
1. Start the program.
6. In the same loop we update the sums as:
2. Use implicit none to diable the already
declared variables. sumx = sumx + x(i)

3. Define the variables with required data sumy = sumy + y(i)


types. sumxy = sumxy + x(i)y(i)
4. Ask user to enter no. of data pair points (n). sumxx = sumxx + x(i)2
5. Initialise: sumyy = sumyy + y(i)2
sumx = 0 7. Using the formula from Least Square method,
declare:
sumy = 0
a = (sumy*sumx - n*sumxy)/((sumx)**2 -
sumxy = 0 n*(sumxx))
sumxx = 0 b = (sumxx*sumy-sumx*sumxy)/(n*sumxx -
sumyy = 0 (sumx)**2)
8. Print values of a and b for y = a + bx.

5. Run a loop for n times where we read the 9. End program.


data of x and y from a file.
program curve
implicit none

!Declare the variables as per their data types


integer :: i , n
real :: a , b , x , y , sx , sy , sx2 , sxy
sx = 0 ; sy = 0 ; sxy = 0 ; sx2 = 0

print*, "Enter the number of points" !Insert the number of data of the experiment
read*, n

open(unit=1, file='curvedata1.dat', status='old’)

!A do loop to calculate the required parameters needed in our calculation

do i=1, n
read(1, *)x,y !Inserts input from a pre-existing file
sx=sx+x
sy=sy+y
sxy=sxy+(x*y)
sx2=sx2+(x**2)
enddo
a=((sy*sx2)-(sx*sxy))/((n*sx2)-(sx**2)) !Calculates the intercept
b=((n*sxy)-(sx*sy))/((n*sx2)-(sx**2)) !Calculates the slope
print*,'y=',b,'x+(',a,')’ !Prints the required result
end program curve
DATA FILE

Curvedata1.dat

0.5,0.31
1,0.82
1.5,1.29
2,1.85
2.5,2.51
3,3.02
OUTPUT
Need of curve fitting??
 TESTING EXISTING MATHEMATICAL RELATION
 ESTABLISHING NEW ONES
 PREDICTING UNKNOWN VALUES
IN PHYSICS
THANK YOU

You might also like