Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //==========================PART 1(EXAMPLE 8)========================================
- let functionB (x : float) = 1. / (2. * x - 5.)
- let eps = 0.001
- let rec iter f init a b =
- if a <= b then f a (iter f init (a + 1) b)
- else init
- let power x n = iter (fun z acc -> float(acc) * x) 1. 1 n
- let functionTaylorSeries (x : float) =
- let realValue =
- functionB x
- let rec iterTillGotRealValue (acc : float) i =
- if (abs (realValue - acc) < eps)
- then (acc,i)
- else iterTillGotRealValue (acc - float(power 2. i) * float(power x i) / float(power 5. (i+1))) (i+1)
- iterTillGotRealValue 0. 0
- let functionTaylorSeriesFaster (x : float) =
- let realValue =
- functionB x
- let rec iterTillGotRealValue (acc : float) (acc4Component : float) i =
- //printf "%f\n" acc
- if (abs (realValue - acc) < eps)
- then (acc,i)
- else iterTillGotRealValue (acc - acc4Component) (acc4Component * 2. * x / 5.) (i+1)
- iterTillGotRealValue 0. 0.2 0
- printf "x\tПервый способ\tКол-во итераций\tВторой способ\tКол-во итераций\tЗначение функции\n"
- let n = 10
- for i = 0 to n do
- let point x =
- 2. * float(x) / float(n)
- let x =
- point(i)
- let res1 =
- functionTaylorSeries x
- let res2 =
- functionTaylorSeriesFaster x
- printf "%f\t%f\t%d\t%f\t%d\t%f\n" x (fst res1) (snd res1) (fst res2) (snd res2) (functionB x)
- //==========================PART 2(EXAMPLE 4)========================================
- let eps2 = 0.00001
- let myfun (x : float) =
- 3. * x - 14. + (exp x) - (exp (-x))
- let derivativeOfMyFun (x : float) =
- 3. + (exp x) + (exp (-x))
- let rec getRoot f derivative curX =
- if (abs (f curX) < eps2)
- then curX
- else getRoot f derivative (curX - (f curX) / (derivative curX))
- printf "%f\n" (getRoot myfun derivativeOfMyFun 1.)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement