Algorithme de Newton
19 novembre 2008
Programmer la m´ethode de Newton pour trouver les racines cubiques de 1.
Pour cela vous traduirez l’´equation z
3− 1 = 0 en un syst`eme de 2 ´equations et 2 inconnues, en posant z = x+iy, o` u i
2= − 1. Pour les 400 × 400 pixels d’un carr´e [ − c, +c]
2, vous calculerez vers quelle racine converge la m´ethode de Newton, et vous afficherez chaque pixel avec une couleur d´ependant de cette racine. Pour c = 2. (en haut ` a gauche), c = 1. (en haut ` a droite), c = 0.8 (en bas `a gauche), c = 0.1 (en bas ` a droite), vous devez obtenir des images comme celles ci :
1
1 Le programme
module L=L i s t ; ; module A=Array ; ; open Gr a p h i cs ; ;
l e t f x y = x∗. x ∗. x −. 3 . ∗. x ∗. y ∗. y −. 1 . ; ; l e t g x y = y ∗. y ∗. y −. 3 . ∗. x ∗. x ∗. y ; ; l e t f x x y = 3 . ∗. x ∗. x −. 3 . ∗. y ∗. y ; ; l e t f y x y = −. 6 . ∗. x ∗. y ; ;
l e t gx x y = −. 6 . ∗. x ∗. y ; ;
l e t gy x y = 3 . ∗. y ∗. y −. 3 . ∗. x ∗. x ; ;
l e t s t e p n e w t o n ( x , y ) =
l e t a = f x x y i n l e t c = f y x y i n l e t b = gx x y i n l e t d = gy x y i n l e t d et = a ∗. d −. b ∗. c i n
l e t a ’ = d / . d et i n l e t b ’ = −. b / . d et i n l e t c ’ = −. c / . d et i n l e t d ’ = a / . d et i n ( x −. ( f x y )∗. a ’ −. ( g x y ) ∗. c ’ ,
y −. ( f x y )∗. b ’ −. ( g x y ) ∗. d ’ ) ; ; l e t s q r x = x ∗. x ; ;
l e t e p s i l o n = 0 . 0 0 0 0 0 1 ; ;
l e t newton ( x , y ) =
l e t r e c b o u c l e ( x , y ) n = i f n=100 then ( x , y )
e l s e l e t ( x ’ , y ’)= s t e p n e w t o n ( x , y ) i n
i f ( s q r ( x−.x ’ ) +. s q r ( y−.y ’ ) <= e p s i l o n∗. e p s i l o n ) then ( x ’ , y ’ ) e l s e b o u c l e ( x ’ , y ’ ) ( n+1) i n b o u c l e ( x , y ) 0 ; ;
l e t d i s t a n c e ( x , y ) ( x ’ , y ’ ) = s q r t ( s q r ( x−.x ’ ) +. s q r ( y−.y ’ ) ) ; ;
l e t r e c c l o s e s t pt p t s =
l e t r e c s ca n i i c l o s e s t =
i f i = A. l e n g t h p t s then i c l o s e s t
e l s e s ca n ( i +1) (i f d i s t a n c e pt p t s . ( i ) < d i s t a n c e pt p t s . ( i c l o s e s t ) then i e l s e i c l o s e s t )
i n s ca n 1 0 ; ;
l e t f o i x = f l o a t o f i n t x ; ; l e t i o f x = i n t o f f l o a t x ; ;
l e t go c o t e =
o p en g r a p h ” 400 x400 ” ; c l e a r g r a p h ( ) ; l e t n= min ( s i z e x ( ) ) ( s i z e y ( ) ) i n
l e t r o o t s = [| −1. / . 2 . , ( ( s q r t 3 . ) / . 2 . ) ;
−1. / . 2 . , −. ( ( s q r t 3 . ) / . 2 . ) ; 1 . , 0 . |] i n f o r i= 0 to n−1 do f o r j= 0 to n−1 do
l e t x= c o t e ∗. ( 2 . ∗. ( f o i i ) / . ( f o i n ) −. 1 . ) i n l e t y= c o t e ∗. ( 2 . ∗. ( f o i j ) / . ( f o i n ) −. 1 . ) i n l e t ( x ’ , y ’ ) = newton ( x , y ) i n
s e t c o l o r (match c l o s e s t ( x ’ , y ’ ) r o o t s with
| 0 −> y e l l o w | 1 −> r ed | −> b l u e ) ; p l o t i j
done done;
l e t p i x e l x y = i o f ( ( x +. c o t e ) ∗. ( f o i n ) / . 2 . / . c o t e ) ,
i o f ( ( y +. c o t e ) ∗. ( f o i n ) / . 2 . / . c o t e ) i n A. i t e r (f u n c t i o n ( x , y)−> l e t ( i , j )= p i x e l x y i n
2
s e t c o l o r w h i te ; f i l l c i r c l e i j 5 ) r o o t s ; ;
l e t d e c o m p o s e c o l o r rgb= (∗rouge , v e r t , b l e u∗)
l e t r=rgb /256/256 i n l e t v=(rgb / 2 5 6 ) mod 256 i n l e t b=rgb mod 256 i n ( r , v , b ) ; ; module G=Gr a p h i cs ; ;
l e t s a v e i m g nom =
l e t tx= G. s i z e x ( ) and ty=G. s i z e y ( ) i n
l e t img= G. dump image (G. g e t i m a g e 0 0 tx ty ) i n l e t f i l e = o p en o u t nom i n
P r i n t f . f p r i n t f f i l e ”P6 %d %d 255\n” (A. l e n g t h img ) (A. l e n g t h img . ( 0 ) ) ; f o r l i g = A. l e n g t h img − 1 downto 0 do f o r c o l =0 to A. l e n g t h img . ( 0 ) − 1 do
l e t ( red , g r een , b l u e )= d e c o m p o s e c o l o r img . ( l i g ) . ( c o l ) i n P r i n t f . f p r i n t f f i l e ”%c%c%c ”
( Char . ch r r ed ) ( Char . ch r g r e e n ) ( Char . ch r b l u e ) done done; c l o s e o u t f i l e ; ;
go 2 . ; ;
s a v e i m g ” 2 . ppm” ; ; go 1 . ; ;
s a v e i m g ” 1 . ppm” ; ; go 0 . 8 ; ;
s a v e i m g ” 0 . 8 . ppm” ; ; go 0 . 1 ; ;
s a v e i m g ” 0 . 1 . ppm” ; ;