Last active
January 8, 2018 03:47
-
-
Save 3100/42bafb3ed6088dd46899736c380e498b to your computer and use it in GitHub Desktop.
台形近似で定積分を求める関数 (『プログラミング in OCaml』 練習問題 3.14 )
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
(* @param f float -> float | |
* @param a, b a < b | |
* @return result | |
* @example | |
* let pi = 3.1415926535;; | |
* integral sin 0.0 pi;; | |
*) | |
let integral f a b = | |
let n = 1e5 in | |
let delta = (b -. a) /. n in | |
let x i = a +. float_of_int(i) *. delta in | |
let y i = f (x i) in | |
let delta_area i = | |
(y (i - 1) +. y i) *. delta /. 2.0 | |
in | |
let rec loop i acc = | |
if x i > b then acc | |
else loop (i + 1) (delta_area i +. acc) | |
in loop 1 0.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment