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.
Solution of Non-Linear Equation
Newton Raphson Method
The Newton-Raphson method is one of the best known iterative
methods used to solve equations numerically. This method is used to find the
zeros or roots of equations numerically. One of the most popular usages is to
calculate the square root of a number, without using the square root function.
Derivation
The Newton-Raphson method is based on the principle that if the initial
guess of the root of f(x)=0 at xi , then if one draws the tangent to the curve at
f(xi), the point xi+1 where the tangent crosses the x -axis is an improved
estimate of the root (Figure 1).
Figure 1 Geometrical illustration of the Newton-Raphson method.
ϭ
Using the definition of the slope of a function, at x=xi
ˆ ᇱ ሺš୧ ሻ ൌ –ƒɅ
ൌ
ˆሺš୧ ሻ െ Ͳ
ǡ
š୧ െ š୧ାଵ
Which gives
‫ܑ ܠ‬ା૚ ൌ ‫ ܑ ܠ‬െ
܎ሺ‫ ܑ ܠ‬ሻ
܎ ᇱ ሺ‫ ܑ ܠ‬ሻ
You should memorize the above formula. Its application to solving
equations of the form f(x) = 0, as we now demonstrate, is called the Newton
Raphson method.
EX
solve x3 − x − 1 = 0 for x.
In this case f(x) = x3 − x − 1, so f ' (x) = 3x2 − 1
š୧ଷ െ š୧ െ ͳ
š୧ାଵ ൌ š୧ െ
͵š୧ଶ െ ͳ
Need to decide on an appropriate initial guess x0 for this problem. A
rough graph can help. Note that f(1) = −1 < 0 and f(2) = 5 > 0. Therefore, a
root of f(x) = 0 must exist between 1 and 2. Let us take x0 = 1 as our initial
guess. Then
š଴ଷ െ š଴ െ ͳ
šଵ ൌ š଴ െ
͵š଴ଶ െ ͳ
and with x0 = 1 we get x1 = 1.5.
Now
šଵଷ െ šଵ െ ͳ
šଶ ൌ šଵ െ
͵šଵଶ െ ͳ
and with x1 = 1.5 we get x2 = 1.34783. For the next stage,
šଶଷ െ šଶ െ ͳ
šଷ ൌ šଶ െ
͵šଶଶ െ ͳ
and with the value just found for x2, we find x3 = 1.32520. Carrying on, we
find that x4 = 1.32472, x5 = 1.32472, etc. We can stop when the digits stop
changing to the required degree of accuracy. We conclude that the root is
1.32472 to 5 decimal places.
Ϯ
program newton_raphson
implicit none
real, parameter::error =1e-4
integer::i
real::xo,x1,f,fd
read*,xo
i=1
10x1=xo-(f(xo)/fd(xo))
if(abs((x1-xo)/x1)<error)then
print*,"root is ", x1 ,"no. of iteration=", i
else
xo=x1
i=i+1
goto 10
endif
end
real function f(x)
real::x
end
real function fd(x
real::x
fd=3*x**2-1
end
ϯ
Download