Last active
January 30, 2019 05:38
-
-
Save Tevinthuku/bdf280614711c20a6c81de44b4b8477d to your computer and use it in GitHub Desktop.
Ocaml challenge on Creating The Mathematical SImpson's rule (https://www.codewars.com/kata/565abd876ed46506d600000d/train/ocaml) .
This file contains 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
let simpson n = | |
let f x = (3.0 /. 2.0) *. sin x *. sin x *. sin x in | |
let pi = (float_of_int 22 /. float_of_int 7) in | |
let h = pi /. float_of_int n in | |
let rec fstSum idx n acc = | |
if idx > n then (acc) else | |
fstSum (idx+1) n (acc +. f (float_of_int ((2 * idx) -1) *. h)) in | |
let rec sndSum idx n acc = | |
if idx > n then (acc) else | |
sndSum (idx+1) n (acc +. f (float_of_int (2 * idx) *. h)) in | |
let aux n = pi /. float_of_int (3 * n) *. ( f 0.0 +. f pi +. (4.0 *. fstSum 1 (n/2) 0.0) +. (2.0 *. sndSum 1 (n/2 - 1) 0.0)) in | |
aux n |
I just realized sin^3(x)!== sin (xxx)
but
sin^3(x) === [sin(x)]^3
Made necessary change, the solution now works.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The above method isn't accurate, will be making changes soon
The math equation image above is what I'm trying to solve where f is