SECANT
!Program name: SECANT
!Function:
x^3+x-3
!Initial
x values are 0 and 3.0
!Tolerance
1E-06
!Root=1.2134118
Real
:: x0, x1, x2, xn, c
Real
:: tolx
integer
:: i
f(x)=x**3+x-3
tolx=1.E-06
x1=0.
x2=3.
write
(*,5) "iter", "x1",
"x2","y1","y2","c","xn-x0"
5
format (a5X,a10X,a20X,a20X,a15X, a15X,a15X,a15x)
do i=0,100
x0=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1))
c=f(x1)*f(x0)
x1=x2
x2=x0
xn=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1))
print *,i, x1, x2, f(x1), f(x2), c, xn-x0
if (c == 0.) exit
if (abs(xn-x0) .gt. tolx) cycle
if (abs(xn-x0) .lt. tolx) exit
end
do
Print *, "The root of the equation is:
", x0
stop
Comentários
Postar um comentário