Skip to content

Instantly share code, notes, and snippets.

@Mroik
Created April 21, 2022 14:52
Show Gist options
  • Save Mroik/7480bf3a624e707dcbdb7444c999ed2a to your computer and use it in GitHub Desktop.
Save Mroik/7480bf3a624e707dcbdb7444c999ed2a to your computer and use it in GitHub Desktop.
Approx e^x with taylor
let fact x =
let rec fact_r x acc =
match x with
| 1.0 -> acc
| y -> fact_r (x - 1.0) (acc * x)
fact_r x 1.0
let expo x k =
let rec expo_r x k acc =
match k with
| 0 -> acc
| y -> expo_r x (k - 1) (acc * x)
expo_r x k 1.0
let f x k =
let rec f_r x k acc =
match k with
| 0 -> acc + 1.0
| y -> f_r x (k - 1) (acc + ((expo x k) / fact k))
f_r x k 0
let apprTaylor x =
Seq.initInfinite (fun v -> f x v)
let apprExp x delta =
let a = apprTaylor x
let rec appr_r x delta i =
if abs((Seq.item (i + 1) a) - (Seq.item i a)) < delta then
Seq.item i a
else
appr_r x delta (i + 1)
appr_r x delta 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment