Skip to content

Instantly share code, notes, and snippets.

@usametov
Last active October 16, 2022 18:12
Show Gist options
  • Save usametov/bea00b05481d45b6756578a40ed9eba8 to your computer and use it in GitHub Desktop.
Save usametov/bea00b05481d45b6756578a40ed9eba8 to your computer and use it in GitHub Desktop.
newton method and secant method
(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