Info 1 période 4 : méthodes numériques

Le POLY (maj 14 mai 15) et ses sources TEX. Au programme IEEE 754, complexité d'algorithmes, suites et séries, approximations polynomiales, résolution approchée d'équations, courbes de Bézier et j'en passe et des meilleures...
Le Diaporama et ses sources TEX.

Un petit module pour obtenir et animer des développements limités. J'ai eu un petit conflit de librairie:

Haskell
 I found a duplicate definition for symbol    _hs_bytestring_long_long_uint_hex
This could be caused by:
   * Loading two different object files which export the same symbol
   * Specifying the same object file twice on the GHCi command line
   * An incorrect `package.conf' entry, causing some object to be
     loaded twice.
GHCi cannot safely continue in this situation.  Exiting now.  Sorry.

Il suffit de charger le module http:

Bash
$ cabal install http

Ensuite, c'est le rêve...

Haskell
import Graphics.EasyPlot
 
import Data.List
import System.Exit
import System.Process
 
-- D L - TAYLOR
 
{-
MÉTHODE À LA MANO :
 
taylorExpo x = 1 + x + x**2/2 + x**3/6 + x**4/24
 
compareDL :: Double -> Double -> (Double -> Double) -> (Double -> Double) -> IO Bool
compareDL a b f taylorF  = 
 plot X11 [ 
   Function2D [Title "Poly Taylor"   , Color Blue ] [Range a b, Step (2**(-8))] taylorF,
   Function2D [Title "Vraie fonction", Color Red  ] [Range a b, Step (2**(-8))] f
          ]
 
Dans ghci : compareDL (-2) 2 exp taylorExpo 
-}
 
-- Renvoie le vecteur [1, x, x^2/2!, x^3/3!, ..]
vecTaylor :: Double -> [Double]
vecTaylor    x      =  scanl (\ acc k -> acc * x / k) 1.0 [1 .. ]
 
{- Renvoie l'évaluation du poly de Taylor de degré n en x de la fonction
   dont on donne la liste des dérivées en 0 -}
polyTaylor :: [Double]   -> Int -> Double -> Double 
polyTaylor    listeDerivees n      x      =
    sum $ take (n + 1) $ zipWith (*) (vecTaylor x) listeDerivees 
 
-- Fonctions de base
-- fonction :: Double -> Double
invPlus    = (\ x -> 1 / (1 + x))
invMoins   = (\ x -> 1 / (1 - x))
logPlus    = (\ x -> log (1 + x))
racinePlus = (\ x -> sqrt (1 + x))
           
{- On renvoie le couple (fonctions, liste de ses dérivées successives en 0)
   On cherche le plus souvent une formule de récurrence et on utilise scanl
   Sinon, on répète des motifs
tTruc :: (Double -> Double, [Double]) -}
tExp      = (exp,        repeat 1.0)
tInvMoins = (invMoins,   scanl (\ acc k -> acc * k   )               1.0 [1 .. ])
tInvPlus  = (invPlus,    scanl (\ acc k -> acc * (-k))               1.0 [1 .. ])
tRacine   = (racinePlus, scanl (\ acc k -> acc * (-0.5) * (2*k - 1)) 1.0 [0 .. ])
tLogPlus  = (logPlus,    0.0 : (snd tInvPlus))
tSin      = (sin,        cycle [0.0,1.0,0.0,-1.0])
tCos      = (cos,        cycle [1.0,0.0,-1.0,0.0])
 
-- On trace sur un même graphique la fonction et son poly de Taylor sur [a,b] avec un pas de 1/2^8
compareDL :: Double -> Double -> ((Double -> Double) , [Double]) -> Int -> IO Bool
compareDL    a         b         tf                                 n   = 
  plot  X11 --(PNG ("exportLog.png")) 
     [ 
    Function2D [Title ("n = " ++ (show n))   , Color Blue ] [Range a b, Step (2**(-8))] (polyTaylor (snd tf) n),
    Function2D [Title "Vraie fonction", Color Red  ] [Range a b, Step (2**(-8))] (fst tf)
     ] 
       
 
animIniDL :: Double -> Double -> ((Double -> Double) , [Double]) -> Int -> String -> IO Bool
animIniDL    a         b         tf                                 n      nom    = 
  plot  (PNG (nom ++ (show (n + 10)) ++ ".png")) (
    [
     Function2D [Title " "   , Color White ] [Range a b, Step (2**(-8))] (polyTaylor (snd tf) k) | k <- [0..10]
      -- pour pallier l'abscence de contrôle sur l'échelle des y
    ]
    ++
    [ 
     Function2D [Title ("n = " ++ (show n))   , Color Blue ] [Range a b, Step (2**(-8))] (polyTaylor (snd tf) n),
     Function2D [Title "Vraie fonction", Color Red  ] [Range a b, Step (2**(-8))] (fst tf)
    ] 
                                                 )
 
animDL :: Double -> Double -> ((Double -> Double) , [Double]) -> Int -> String -> IO Bool
animDL a b tf n nom = [animIniDL a b tf k nom | k <- [0 .. n] ]
 
suite :: [IO Bool] -> IO ()
suite []     = return ()
suite (x:xs) = do x
                  suite xs
 
 -- crée l'animation dans un fichier anim[nom des fichiers].png et on visualise dans firefox
animPNG ::  Double -> Double -> ((Double -> Double) , [Double]) -> Int -> String -> IO ()
animPNG a b tf n nom =
        do
          suite (animDL a b tf n nom)
          ExitSuccess <- system
          ("apngasm anim" ++ nom ++ ".png " ++ nom ++ "*.png 1 1  && firefox anim" ++ nom ++ ".png")
          return ()

Par exemple, pour la racine carrée:

Haskell
λ> animPNG 0 1.2 tRacine 20 "racine"

et on obtient:

animation du DL de racine(1-x