supermanhelp.com

ALGORITHM Newton

REAL :: Input, X, NewX, Tolerance
INTEGER :: Count

READ(*,*) Input, Tolerance

Count = 0 ! count starts with 0
X = Input ! X starts with the input value
DO ! for each iteration
Count = Count + 1 ! increase the iteration count
NewX = 0.5*(ans + Input/X) ! compute a new approximation
IF (ABS(X - NewX) < Tolerance) EXIT ! if they are very close, exit
X = NewX ! otherwise, keep the new one
END DO

WRITE(*,*) ‘After ‘, Count, ’ iterations:’
WRITE(*,*) ’ The estimated square root is ‘, NewX
WRITE(*,*) ’ The square root from SQRT() is ‘, SQRT(Input)
WRITE(*,*) ’ Absolute error = ‘, ABS(SQRT(Input) - NewX)

END PROGRAM SquareRoot

http://supermanhelp.com

Reply

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <hr> <ul> <ol> <li> <dl> <dt> <dd> <img>
  • Lines and paragraphs break automatically.
  • Adds typographic refinements.

More information about formatting options