Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
10
Task/Euler-method/OCaml/euler-method-1.ocaml
Normal file
10
Task/Euler-method/OCaml/euler-method-1.ocaml
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
(* Euler integration by recurrence relation.
|
||||
* Given a function, and stepsize, provides a function of (t,y) which
|
||||
* returns the next step: (t',y'). *)
|
||||
let euler f ~step (t,y) = ( t+.step, y +. step *. f t y )
|
||||
|
||||
(* newton_cooling doesn't use time parameter, so _ is a placeholder *)
|
||||
let newton_cooling ~k ~tr _ y = -.k *. (y -. tr)
|
||||
|
||||
(* analytic solution for Newton cooling *)
|
||||
let analytic_solution ~k ~tr ~t0 t = tr +. (t0 -. tr) *. exp (-.k *. t)
|
||||
24
Task/Euler-method/OCaml/euler-method-2.ocaml
Normal file
24
Task/Euler-method/OCaml/euler-method-2.ocaml
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
(* Wrapping up the parameters in a "cool" function: *)
|
||||
let cool = euler (newton_cooling ~k:0.07 ~tr:20.)
|
||||
|
||||
(* Similarly for the analytic solution: *)
|
||||
let analytic = analytic_solution ~k:0.07 ~tr:20. ~t0:100.
|
||||
|
||||
(* (Just a loop) Apply recurrence function on state, until some condition *)
|
||||
let recur ~until f state =
|
||||
let rec loop s =
|
||||
if until s then ()
|
||||
else loop (f s)
|
||||
in loop state
|
||||
|
||||
(* 'results' generates the specified output starting from initial values t=0, temp=100C; ending at t=100s *)
|
||||
let results fn =
|
||||
Printf.printf "\t time\t euler\tanalytic\n%!";
|
||||
let until (t,y) =
|
||||
Printf.printf "\t%7.3f\t%7.3f\t%9.5f\n%!" t y (analytic t);
|
||||
t >= 100.
|
||||
in recur ~until fn (0.,100.)
|
||||
|
||||
results (cool ~step:10.)
|
||||
results (cool ~step:5.)
|
||||
results (cool ~step:2.)
|
||||
Loading…
Add table
Add a link
Reference in a new issue