N selon epsilon

sawcce a.k.a Alex=

Written on: 5/28/2024 Last edited: 5/28/2024

1
1termesMilieu :: (Enum a1, RealFloat a1, Enum n, Integral n) => (a1 -> a1) -> n -> a1 -> a1 -> a1
2termesMilieu f n a b = sum $ map g [1 .. n - 1]
3  where
4    g k = f (a + fromIntegral k * ((b - a) / fromIntegral n))
5
6riemann :: (Enum b, RealFloat b, Enum n, Integral n) => (b -> b) -> n -> b -> b -> (b, b)
7riemann f n a b
8  | n == 0 = (0, 0)
9  | a - b == 0 = (0, 0)
10  | otherwise = do
11      let g = f a
12          d = f b
13          m = termesMilieu f n a b
14
15      (((b - a) / fromIntegral n) * (m + g), ((b - a) / fromIntegral n) * (m + d))
16
17nSelonEpsilon f a b epsilon
18  | a < b = ceiling (((b - a) / epsilon) * abs (f a - f b)) + 1
19  | a > b = floor (((b - a) / epsilon) * abs (f a - f b)) - 1
20
21delta x y = abs (x - y)
22
23methodeSimple epsilon max = filter (\(_, x, _, _) -> x) $ map g [1 .. max]
24  where
25    g k = do
26      let (g, d) = riemann f k 0 1
27      (k, delta g d <= epsilon, g, d)
28
29f :: (RealFloat a) => a -> a
30f x = 1 / (1 + x ^ 2)
31
32main = do
33  let epsilon = 0.01
34  let n = nSelonEpsilon f 0 1 epsilon
35  let (g, d) = riemann f n 0 1
36  print (n, delta g d <= epsilon, g, d)
37  print $ methodeSimple epsilon 100
38

Comments

Loading comments...