According to Newton-Raphson's:
Xn+1 = Xn - f(Xn) / f'(Xn)
(newtonRhap x f fx)
(newtonRhap 0.1 sin cos) => 0
(newtonRhap 2.0
(lambda (x) (- (* x x) x 6)) ; f
(lambda (x) (- (* 2 x) 1 )) ) => 3 ; f'
How can I implement this function?
The routine will stop iteration when the change in solution is less than pre-set tolerance.
I use a global variable for it, (define TOL 1e-6)
:
#lang racket
(define TOL 1e-6)
(define (func f x) (f x))
(define (f x) (- (* x x) x 6))
(define (fx x) (- (* 2 x) 1))
(define x 2.0)
(define (newtonRhap x ff ffx)
( (> (- x (/ ff ffx)) TOL)
(newtonRhap (- x (/ ff ffx)) ff ffx)
(list x) ) )
(display (newtonRhap x (f x) (fx x)) )