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

In summary, the conversation is about a program for implementing Newton's method for solving equations. The code includes a do loop and a subroutine for calculating the function and its derivative. The output shows that the code is not functioning properly as it only outputs infinity for the values. The code may need adjustments, such as passing the correct value for xn and adjusting the tolerance and epsilon values.
  • #1
jjohnson8
1
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
  • #2
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...
 
  • #3


I have to say that it is impressive that you are working on a program for Newton's method in Fortran. This method is a powerful tool for solving equations and is widely used in various fields of science and engineering.

Looking at your code and output, it seems that you are having trouble with the initial guess for the root. The program is not able to converge to a solution because the initial guess is too far from the actual root. This is why your values for x0, xn, and F(xn) are all showing as infinity.

To correct this, you can try changing your initial guess to a value closer to the actual root. You can also try tweaking the TOL and EPS values to see if that helps with convergence.

Additionally, it is important to carefully check your code and ensure that all variables are correctly defined and used in the calculations. Small errors in coding can also lead to incorrect results.

Overall, keep experimenting and fine-tuning your code. Newton's method is a valuable tool and with some adjustments, you will be able to successfully solve equations using your program. Good luck!
 

1. What is Newton's Method in Fortran?

Newton's Method in Fortran is a numerical method for finding roots of equations. It is an iterative process that uses derivatives to approximate the roots of a function.

2. How does Newton's Method work in Fortran?

Newton's Method works by using an initial guess for the root of a function, and then repeatedly updating that guess using the function's derivative until a desired level of accuracy is reached. This process is repeated until the root is found.

3. What are the advantages of using Newton's Method in Fortran?

Some advantages of using Newton's Method in Fortran include its fast convergence rate, ability to handle complex equations, and its versatility for solving a wide range of mathematical problems.

4. What are the limitations of Newton's Method in Fortran?

One limitation of Newton's Method in Fortran is that it may fail to converge for certain functions or starting values. It also requires knowledge of the function's derivative, which may be difficult to obtain in some cases.

5. How can I implement Newton's Method in Fortran?

To implement Newton's Method in Fortran, you will need to write a program that defines the function you want to find the root of, calculates its derivative, and then uses those values in an iterative loop to update the root guess until the desired accuracy is achieved.

Similar threads

  • Programming and Computer Science
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
5K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
8K
Back
Top