Created
February 16, 2013 20:11
-
-
Save Idorobots/4968517 to your computer and use it in GitHub Desktop.
Function roots
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (bisection f a b tol) | |
(let ((c (* (+ a b) 0.5))) | |
(cond ((<= (abs (f c)) tol) c) | |
((> (* (f a) (f c)) 0) (bisection f c b tol)) | |
(else (bisection f a c tol))))) | |
(define (newton f fp fb a b tol) | |
(let loop ((c (if (> (* (f a) (fb a)) 0) a b))) | |
(if (<= (abs (f c)) tol) | |
c | |
(loop (- c (/ (f c) (fp c))))))) | |
(define (secant f a b tol) | |
(if (<= (abs (f b)) tol) | |
b | |
(secant f b (- b (* (f b) | |
(/ (- b a) | |
(- (f b) (f a)))))))) | |
;; function bisection(f, a, b, tol) { | |
;; var i = 0; | |
;; var c = (a+b)/2.0; | |
;; | |
;; while(abs(f(c)) > tol) { | |
;; if(f(a) * f(c) > 0) { | |
;; a = c; | |
;; } | |
;; else { | |
;; b = c; | |
;; } | |
;; | |
;; c = (a + b) / 2.0; | |
;; ++i; | |
;; } | |
;; return (c, i); | |
;; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment