Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- open Printf
- let a = float_of_string Sys.argv.(1)
- let pi = 4.0 *. atan 1.0
- let f x = sin((a*.x)/.(1.+.x))-.x**2.
- let df x = sin((a*.x)/.(1.+.x))+. x*. cos((a*.x)/.(1.+.x))*.(a/.(x+.1.)**2.)-.2.*.x
- let get_bound_plus k = (2.*.pi*.k)/.(a-.2.*.k*.pi)
- let get_bound_moins k = (pi+.2.*.k*.pi)/.(a-.pi-.2.*.k*.pi)
- let good_enough a b err =
- abs_float(a -. b) < err *. ((abs_float(a) +. abs_float(b))/. 2.)
- let rec bissec g borneA borneB accuracy =
- if g borneA =0. then
- borneA
- else if g borneB =0. then
- borneB
- else if g borneA *. g borneB > 0. then
- -.1.
- else
- let mid = ((borneA +. borneB)/. 2.)in
- if good_enough borneA borneB accuracy then
- mid
- else if g borneA *. g mid <= 0. then
- bissec g borneA mid accuracy
- else
- bissec g mid borneB accuracy
- let find_max accuracy step =
- let borne_pos =
- let x = get_bound_plus (ceil (step/.2.)) in
- if x = 0. then accuracy else x in
- let borne_neg = get_bound_moins (floor (step/.2.)) in
- bissec df borne_pos borne_neg accuracy
- let main g b accuracy=
- if a<= pi then
- let tmp = bissec g (0.+.accuracy) 1. accuracy in
- printf "%.13g %.13g\n" tmp (sqrt(tmp))
- else
- let step = ref (0.) in
- let der_rac_one = ref (0.) in
- let der_rac_two = ref (find_max accuracy 0.) in
- while (!current_bound_neg < 1. && !current_bound_pos < 1.) do
- let tmp = bissec f !der_rac_one !der_rac_two accuracy in
- if tmp = -.1. then (
- step := !step+.1.;
- if ((int_of_float !step) mod 2 = 1) then
- der_rac_one := (find_max accuracy !step)
- else
- der_rac_two := (find_max accuracy !step)
- )
- else (
- printf "%.13g %.13g\n" tmp (sqrt(tmp));
- step := !step+.1.;
- if (int_of_float !step) mod 2 = 1 then
- der_rac_one := (find_max accuracy !step)
- else
- der_rac_two := (find_max accuracy !step)
- )
- done;
- let borne = min der_rac_one der_rac_two in
- (*min car si on sort de la boucle c'est qu'une borne depasse 1 *)
- let x = bissec g borne 1. accuracy in
- if x <> -1. then
- printf "%.13g %.13g\n" x (sqrt(x))
- let ()=
- printf "%g\n" a;
- let tmp = bissec df (1e-14) (get_bound_moins (floor (1./.2.))) (1e-14) in
- printf "%g\n" tmp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement