Skip to content

Instantly share code, notes, and snippets.

@Ansarina
Last active June 6, 2016 07:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ansarina/ed95ece7bbdc85c0c56fc20a592d388c to your computer and use it in GitHub Desktop.
Save Ansarina/ed95ece7bbdc85c0c56fc20a592d388c to your computer and use it in GitHub Desktop.
PROGRAM ME
EXTERNAL ff,ffder
x0=1
CALL newton(ff,ffder,x0,1e-7,1e-7,10,iflag)
IF(iflag>1) THEN
PRINT *, "there was an error occurred"
ENDIF
fx0=ff(x0)
PRINT *, x0,fx0
READ (*,*)
END PROGRAM ME
SUBROUTINE newton(F,Fder,X0,Xtol,Ftol,Ntol,iflag)
PRINT *, " N x0 F(x0) der(F)"
iflag=0
DO N=1,Ntol
Fx0=F(x0)
IF(abs(Fx0)<ftol) THEN
RETURN
ENDIF
derF=Fder(x0)
PRINT *, N,x0,Fx0,derF
IF(derF==0)THEN
PRINT *, "error, derivative=0"
iflag=2
RETURN
ENDIF
DELTAx=Fx0/derF
x0=x0-DELTAx
IF(abs(DELTAx)<xtol)THEN
RETURN
ENDIF
ENDDO
END SUBROUTINE newton
FUNCTION ff(x)
ff=x*x*x-x-1
RETURN
END FUNCTION
FUNCTION ffder(x)
ffder=3*x*x-1
RETURN
END FUNCTION ffder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment