• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Sep 16th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. //Zagitov Asgar
2. //17 in list
3.
4. // Print a table of a given function f, computed by taylor series
5.
6. // function to compute
7. let f(x) = 0.5 * log(x)
8.
9. let a = 0.2
10. let b = 0.7
11. let n = 10
12.
13. // Define a function to compute f using naive taylor series method
14.
15. let taylor_nf x n = (1. / (2. * float(n) + 1.)) * ((x - 1.)/(x + 1.) ** (2. * float(n) + 1.))
16.
17. let rec taylor_naive_loop f x n iter =
18.     if (abs(f x n) < 0.00000001) then f x n;
19.     else (+) (f x n) (taylor_naive_loop f x (n + 1))
20.
21. let taylor_naive x = taylor_naive_loop taylor_nf x 0
22.
23. // Define a function to do the same in a more efficient way
24. let rec taylor_loop prev x n =
25.     let current = (2.*float(n) - 1.)/(2.*float(n) + 1.) * (((x - 1.)/(x + 1.)) ** 2.) * prev
26.     if (abs(current) < 0.00000001) then current;
27.     else (+) prev (taylor_loop current x (n + 1))
28.
29. let taylor x =
30.     let current = (float(x) - 1.)/(float(x) + 1.)
31.     taylor_loop current x 1
32.
33. let main =
34.    for i = 0 to n do
35.      let x = a+(float i)/(float n)*(b-a)
36.      printfn "%5.2f  %10.6f  %10.6f   %10.6f" x (f x) (taylor_naive x) (taylor x)
37.
38. main
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top