Last active
October 16, 2022 18:12
-
-
Save usametov/bea00b05481d45b6756578a40ed9eba8 to your computer and use it in GitHub Desktop.
newton method and secant method
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
(defn newt [x0 f df n] (take n (iterate #(- % (double (/ (f %) (df %)))) x0))) | |
;;call it like this: | |
;;(newt 1 #(- (* % %) 2.0) #(* 2.0 %) 10.0) | |
;; secant method | |
(defn secant | |
[x0 x1 f n] | |
(letfn [(fn_ [x_n-1 x_n-2 f_] | |
(let [x_n (- x_n-1 (/ (* (f_ x_n-1) (- x_n-1 x_n-2)) | |
(- (f_ x_n-1) (f_ x_n-2))))] | |
[x_n x_n-1 f_]))] | |
(take n (map first (iterate #(apply fn_ %) [x0 x1 f]))))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment