Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 2.55 KB | None | 0 0
  1. open Printf
  2.  
  3. let a = float_of_string Sys.argv.(1)
  4.  
  5. let pi = 4.0 *. atan 1.0
  6.  
  7. let f x = sin((a*.x)/.(1.+.x))-.x**2.
  8. let df x = sin((a*.x)/.(1.+.x))+. x*. cos((a*.x)/.(1.+.x))*.(a/.(x+.1.)**2.)-.2.*.x
  9.  
  10. let get_bound_plus k = (2.*.pi*.k)/.(a-.2.*.k*.pi)
  11. let get_bound_moins k = (pi+.2.*.k*.pi)/.(a-.pi-.2.*.k*.pi)
  12.  
  13. let good_enough a b err =
  14.   abs_float(a -. b) < err *. ((abs_float(a) +. abs_float(b))/. 2.)
  15.  
  16. let rec bissec g borneA borneB accuracy =
  17.       if g borneA =0. then
  18.           borneA
  19.       else if g borneB =0. then
  20.           borneB
  21.       else if g borneA *. g borneB > 0. then
  22.           -.1.
  23.       else
  24.           let mid = ((borneA +. borneB)/. 2.)in
  25.           if good_enough borneA borneB accuracy then
  26.               mid
  27.           else if g borneA *. g mid <= 0. then
  28.               bissec g borneA mid accuracy
  29.           else
  30.               bissec g mid borneB accuracy
  31.  
  32.  
  33. let find_max accuracy step =
  34.     let borne_pos =
  35.         let x = get_bound_plus (ceil (step/.2.)) in
  36.         if x = 0. then accuracy else x in
  37.     let borne_neg = get_bound_moins (floor (step/.2.)) in
  38.     bissec df borne_pos borne_neg accuracy
  39.  
  40. let main g b accuracy=
  41.     if a<= pi then
  42.         let tmp = bissec g (0.+.accuracy) 1. accuracy in
  43.         printf "%.13g %.13g\n" tmp (sqrt(tmp))
  44.     else
  45.         let step = ref (0.) in
  46.         let der_rac_one = ref (0.) in
  47.         let der_rac_two = ref (find_max accuracy 0.) in
  48.         while (!current_bound_neg < 1. && !current_bound_pos < 1.) do
  49.             let tmp = bissec f !der_rac_one !der_rac_two accuracy in
  50.             if tmp = -.1. then (
  51.                 step := !step+.1.;
  52.                 if ((int_of_float !step) mod 2 = 1) then
  53.                     der_rac_one := (find_max accuracy !step)
  54.                 else
  55.                     der_rac_two := (find_max accuracy !step)
  56.             )
  57.             else (
  58.                 printf "%.13g %.13g\n" tmp (sqrt(tmp));
  59.                 step := !step+.1.;
  60.                 if (int_of_float !step) mod 2 = 1 then
  61.                     der_rac_one := (find_max accuracy !step)
  62.                 else
  63.                     der_rac_two := (find_max accuracy !step)
  64.             )
  65.         done;
  66.         let borne = min der_rac_one der_rac_two in
  67.         (*min car si on sort de la boucle c'est qu'une borne depasse 1 *)
  68.         let x = bissec g borne 1. accuracy in
  69.         if x <> -1. then
  70.             printf "%.13g %.13g\n" x (sqrt(x))
  71.  
  72. let ()=
  73.     printf "%g\n" a;
  74.     let tmp = bissec df (1e-14) (get_bound_moins (floor (1./.2.))) (1e-14) in
  75.     printf "%g\n" tmp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement