Fortran How Can I Correct My Fortran Program for Newton's Method?

AI Thread Summary
The discussion revolves around a Fortran program implementing Newton's method for finding roots of the equation F(x) = x² - 4. The initial code provided is producing outputs of infinity for all iterations, indicating a problem with the calculations. The user suspects that the issue lies in the way the variable `xn` is being passed to the subroutine `FCN`. The suggested correction involves changing the subroutine to use `xn` instead of `x0` for the function evaluation, which should resolve the issue. Additionally, there is a recommendation to increase the tolerance (TOL) and epsilon (EPS) values to potentially improve convergence. The overall focus is on debugging the implementation to achieve correct results.
jjohnson8
Messages
1
Reaction score
0
I'm working on a program for Newton's method for solving equations.

This is my code:

=======================================================
program Newton
implicit real(a-h,o-z)
F(x) = x**2 - 4
!...&---1---------2---------3---------4---------5---------6---------7---

!----All values to be read in/set values----!
PRINT*, 'Please inter initial guess for root: '
READ*, x0
maxIT = 20
TOL = 1.e-14
EPS = 1.e-14
xn = 0
!----Print titles for output----!
PRINT 20, 'x0', 'maxIT', 'TOL', 'EPS', 'n', 'xn', 'F(xn)'

!----Do Loop to go from 0 to max iterations----!
DO n = 0,maxIT
CALL FCN(xn, Fn, DFn)
xn = x0 - (Fn/DFn)
Dx = xn - x0
x0 = xn
IF( ABS(Dx) .LE. TOL) THEN
IF( ABS(Fn) .LE. TOL) THEN
PRINT*, 'DONE: root=',xn,' found in',n,'iterations'
PRINT*, ' residual =', Fn
ELSE
PRINT*, 'Stuck at iteration:', n
PRINT*, ' relDX < EPS but residual=',Fn,' > TOL, exiting'
STOP
ENDIF
ENDIF
PRINT 10, x0, maxIT, TOL, EPS, n, xn, F(xn)
ENDDO
!----Formats for headings----!
10 FORMAT(F10.7,4x,I2,4x,F18.14,13x,F18.14,8x,I2,3x,F10.7,3x,F10.7)
20 FORMAT(A2,10x,A5,8x,A3,35x,A3,10x,A1,6x,A2,11x,A5)
END

subroutine FCN(xn, Fn, DFn)
Fn = (x0**2) - 4
DFn = 2*x0
return
END


Here is my output:

x0 maxIT TOL EPS n xn F(xn)
+Infinity 20 0.00000000000001 0.00000000000001 0 +Infinity +Infinity
+Infinity 20 0.00000000000001 0.00000000000001 1 +Infinity +Infinity
+Infinity 20 0.00000000000001 0.00000000000001 2 +Infinity +Infinity
+Infinity 20 0.00000000000001 0.00000000000001 3 +Infinity +Infinity
+Infinity 20 0.00000000000001 0.00000000000001 4 +Infinity +Infinity
+Infinity 20 0.00000000000001 0.00000000000001 5 +Infinity +Infinity
+Infinity 20 0.00000000000001 0.00000000000001 6 +Infinity +Infinity
+Infinity 20 0.00000000000001 0.00000000000001 7 +Infinity +Infinity
+Infinity 20 0.00000000000001 0.00000000000001 8 +Infinity +Infinity
+Infinity 20 0.00000000000001 0.00000000000001 9 +Infinity +Infinity
+Infinity 20 0.00000000000001 0.00000000000001 10 +Infinity +Infinity
+Infinity 20 0.00000000000001 0.00000000000001 11 +Infinity +Infinity
+Infinity 20 0.00000000000001 0.00000000000001 12 +Infinity +Infinity
+Infinity 20 0.00000000000001 0.00000000000001 13 +Infinity +Infinity
+Infinity 20 0.00000000000001 0.00000000000001 14 +Infinity +Infinity
+Infinity 20 0.00000000000001 0.00000000000001 15 +Infinity +Infinity
+Infinity 20 0.00000000000001 0.00000000000001 16 +Infinity +Infinity
+Infinity 20 0.00000000000001 0.00000000000001 17 +Infinity +Infinity
+Infinity 20 0.00000000000001 0.00000000000001 18 +Infinity +Infinity
+Infinity 20 0.00000000000001 0.00000000000001 19 +Infinity +Infinity
+Infinity 20 0.00000000000001 0.00000000000001 20 +Infinity +Infinity


I've tinkered with this all day, and I can't figure out how to get anything different than infinity for the values. Anyone out there that can correct me?
 
Technology news on Phys.org
Looks like you're not passing xn, the function should be

subroutine FCN(xn, Fn, DFn)
Fn = (xn**2) - 4
DFn = 2*xn
return

also try making tol and eps larger...
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top