Skip to content

Instantly share code, notes, and snippets.

@Tevinthuku
Last active January 30, 2019 05:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tevinthuku/bdf280614711c20a6c81de44b4b8477d to your computer and use it in GitHub Desktop.
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) .
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
@Tevinthuku
Copy link
Author

Tevinthuku commented Jan 29, 2019

The above method isn't accurate, will be making changes soon

codecogseqn

The math equation image above is what I'm trying to solve where f is
codecogseqn 1

@Tevinthuku
Copy link
Author

Tevinthuku commented Jan 30, 2019

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