FORTRAN 90 L e c

advertisement
FORTRAN 90
Lecturer : Rafel Hekmat Hameed
University of Babylon
Subject : Fortran 90
College of Engineering
Year : Second B.Sc.
Mechanical Engineering Dep.
Curve fitting
Curve fitting, also known as regression analysis, is used to find the "best
fit" line or curve for a series of data points. Most of the time, the curve fit will
produce an equation that can be used to find points anywhere along the curve.
 Least squares method
The method of least squares is a standard approach to the approximate
solution of over determined systems, i.e., sets of equations in which there are
more equations than unknowns. "Least squares" means that the overall solution
minimizes the sum of the squares of the errors made in the results of every
single equation. The most important application is in data fitting. The best fit in
the least-squares sense minimizes the sum of squared residuals, a residual being
the difference between an observed value and the fitted value provided by a
model.
Least squares problems fall into two categories: linear or ordinary least
squares and non-linear least squares, depending on whether or not the residuals
are linear in all unknowns.
Ordinary least squares (OLS) or linear least squares is a method for
estimating the unknown parameters in a linear regression model. This method
minimizes the sum of squared vertical distances between the observed responses
in the dataset and the responses predicted by the linear approximation.
A linear curve is represented by the equation
y = a + bx
Given a set of n data points
(x1, y1) , (x2, y2) , …, (xn; yn)
ϭ
Wee find the values of a and b in the linear equation above by using the
following formulas;
Where  represents the sum from 1 to n.
Example
Use the least squares method to fit the best fit curve to the following set of
data.
(-4.5, 0.7) , (-3.2,, 2.3) , (-1.4, 3.8) , (0.8, 5.0) , (2.5, 5.5) , (4.1,
(4 6.6)
We need to come up with the a and b value that best fitss the above set of data.
To use these formulas we first need to find the following;
Filling these values into the expressions for a and b we get
a = 4.16529; and b = 0.642226
This gives us the equation y = 4.16529 + 0.642226x as the equation of
the line that is the best fit to above set of data.
A fortran 90 progra
am to compute the linear regression coefficients
program linear_regression
implicit none
integer,parameter::n=6
real, dimension(n)::x,y
integer::i
real::s1,s2,s3,s4,a,b
data x/-4.5,--3.2,-1.4,0.8,2.5,4.1/
Ϯ
data y /0.7,2.3,3.8,5.0,5.5,6.6/
do i=1,n
s1=s1+x(i)
s2=s2+x(i)**2
s3=s3+y(i)
s4=s4+x(i)*y(i)
enddo
b=((n*s4)-(s1*s3))/((n*s2)-(s1**2))
a=(s3-(s1*b))/n
write(*,6)a,b
6 format(2x,"y=",f9.7," +",f9.7,1x,"x")
end
ϯ
Download