symbolic mathematics engine in OCaml with differentiation, integration, simplification, and numerical methods
17

Configure Feed

Select the types of activity you want to include in your feed.

leibniz / examples / numerical.ml
880 B 26 lines
1open Leibniz 2 3let () = 4 print_endline "root finding\n"; 5 6 let f = Parser.parse "x^2 - 4" in 7 print_endline ("finding roots of f(x) = " ^ Expr.to_string f); 8 9 (match Numerical.bisection f "x" 0.0 3.0 0.0001 100 with 10 | Some root -> Printf.printf "bisection: root ≈ %.6f\n" root 11 | None -> print_endline "bisection failed"); 12 13 (match Numerical.newton_raphson f "x" 1.0 0.0001 100 with 14 | Some root -> Printf.printf "newton-raphson: root ≈ %.6f\n" root 15 | None -> print_endline "newton-raphson failed"); 16 17 print_endline "\nnumerical integration\n"; 18 19 let g = Parser.parse "sin(x)" in 20 print_endline ("integrating g(x) = " ^ Expr.to_string g); 21 22 let trap = Numerical.trapezoidal g "x" 0.0 3.14159265 100 in 23 Printf.printf "trapezoidal rule: %.6f\n" trap; 24 25 let simp = Numerical.simpsons g "x" 0.0 3.14159265 100 in 26 Printf.printf "simpson's rule: %.6f\n" simp