Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Zagitov Asgar
- //17 in list
- // Print a table of a given function f, computed by taylor series
- // function to compute
- let f(x) = 0.5 * log(x)
- let a = 0.2
- let b = 0.7
- let n = 10
- // Define a function to compute f using naive taylor series method
- let taylor_nf x n = (1. / (2. * float(n) + 1.)) * ((x - 1.)/(x + 1.) ** (2. * float(n) + 1.))
- let rec taylor_naive_loop f x n iter =
- if (abs(f x n) < 0.00000001) then f x n;
- else (+) (f x n) (taylor_naive_loop f x (n + 1))
- let taylor_naive x = taylor_naive_loop taylor_nf x 0
- // Define a function to do the same in a more efficient way
- let rec taylor_loop prev x n =
- let current = (2.*float(n) - 1.)/(2.*float(n) + 1.) * (((x - 1.)/(x + 1.)) ** 2.) * prev
- if (abs(current) < 0.00000001) then current;
- else (+) prev (taylor_loop current x (n + 1))
- let taylor x =
- let current = (float(x) - 1.)/(float(x) + 1.)
- taylor_loop current x 1
- let main =
- for i = 0 to n do
- let x = a+(float i)/(float n)*(b-a)
- printfn "%5.2f %10.6f %10.6f %10.6f" x (f x) (taylor_naive x) (taylor x)
- main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement