Vous êtes sur la page 1sur 21

Chapitre 43

Exemples avec Sage

Ce chapitre est un foure-tout de choses que l’on peut faire avec Sage.

43.0.1 Graphiques
Pour afficher le graphe d’une fonction, vous pouvez faire

+--------------------------------------------------------------------+
| SageMath version 8.1, Release Date: 2017-12-07 |
| Type "notebook()" for the browser-based notebook interface. |
| Type "help()" for help. |
+--------------------------------------------------------------------+
sage: plot(cos(x),0,5)
Launched png viewer for Graphics object consisting of 1 graphics primitive
sage: f(x)=sin(x)
sage: f.plot(-pi,pi)
Launched png viewer for Graphics object consisting of 1 graphics primitive

Un programme externe se lance automatiquement pour afficher le graphique que vous avez de-
mandé.
Il se peut qu’aucun programme ne se lance et vous ayez, au lieu de Launched png viewer for
Graphics object ... uniquement Created graphics object .... Disons pour faire court que
Sage a produit un png et qu’il ne sait pas quel programme externe utiliser pour l’afficher.
La solution est à l’adresse http://doc.sagemath.org/html/en/reference/misc/sage/misc/
viewer.html

43.0.2 Autres
Dans le but d’automatiser certaines tâches, j’ai écrit ce module, nomé outilsINGE.sage, dans
le cadre d’un cours de première année donné à des ingénieurs. Certaines des fonctions définies ici
sont utilisée dans les exemples qui suivent.

1 # -* - c o d i n g : u t f 8 -* -
2 f r o m sage . a l l i m p o r t *
3

4 """
5 This module provides _pragmatic_ tools for solving exercise for
6 a first year in g e n e r a l m a t h e m a t i c s .
7 """
8

9 # TODO : t r o u v e r une bonne t r a d u c t i o n pour " point de selle ."


10

2023
2024 CHAPITRE 43. EXEMPLES AVEC SAGE

11 d e f automatedVar ( symbol , n ) :
12 " " " I f s y m b o l = " x " a n d n =4 , r e t u r n t h e s t r i n g ’ x1 , x2 , x3 , x 4 ’ –â
"""
13 s = " , " . join ([ symbol + s t r ( i ) f o r i i n r a n g e (1 , n +1) ])
14 return s
15

16 c l a s s Solv eLine arSyst em ( o b j e c t ) :


17 """
18 Solve Ax = v and print it in a nice way
19

20 Example :
21

22 A = matrix ([ [1 , -2 ,3 , -2 ,0] ,[3 , -7 , -2 ,4 ,0] ,[4 ,3 ,5 ,2 ,0] ])


23 v = vector ((0 ,0 ,0 ,0 ,0) )
24 print SolveLinearSystem (A ,v)
25 """
26 d e f __init__ ( self ,A , v ) :
27 self . matrix = A
28 self . vector = v
29 self . nvars = A . ncols ()
30 s = automatedVar ( " x " , self . nvars )
31 self . xx = var ( s )
32 d e f equations ( self ) :
33 """ Return the e q u a t i o n s c o r r e s p o n d i n g to the
34 s e l f . m a t r i x a n d s e l f . v e c t o r a s a l i s t o f e q u a t i o n s –â
"""
35 X = matrix ( [ self . xx [ i ] f o r i i n r a n g e (0 , self . nvars ) ]) .–â
transpose ()
36 eqs =[]
37 f o r i i n r a n g e (0 , self . matrix . nrows () ) :
38 exp = ( self . matrix * X ) [ i ][0]== self . vector [ i ]
39 eqs . append ( exp )
40 r e t u r n eqs
41 d e f solutions ( self ) :
42 r e t u r n solve ( self . equations () , self . xx )
43 d e f latex ( self ) :
44 """ " Return the LaTeX ’s code of the system . """
45 a =[]
46 a . append ( r " " "
47

48 \ item
49 $
50 \ left \{
51 \ begin { array }{ ll }
52 """ )
53 f o r eq i n self . equations () :
54 a . append ( " " + s t r ( eq ) . replace ( " x " , " x _ " ) . replace ( " * "–â
, " " ) . replace ( " = = " , " = " ) + " \ \ \ \ \ n " )
55 a . append ( r " " "
56 \ end { array }
57 \ right .
58 $
59 """ )
2025

60 r e t u r n " " . join ( a )


61 d e f __str__ ( self ) :
62 a = []
63 a . append ( " T h e g i v e n m a t r i x c o r r e s p o n d s t o t h e s y s t e m " )
64 f o r eq i n self . equations () :
65 a . append ( s t r ( eq ) )
66 a . append ( " A n d t h e s o l u t i o n s a r e " )
67 a . append ( s t r ( self . solutions () ) )
68 r e t u r n " \ n " . join ( a )
69

70 d e f QuadraticMap (A , v ) :
71 """
72 Return the result of the q u a d r a t i c form a s s o c i a t e d
73 with A a p p l i e d on the vector v , that is the number
74 A_ij v ^ iv ^ j
75 using the summation convention .
76 """
77 n = A . nrows ()
78 i f n o t A . is_symmetric () :
79 print " W a r n i n g : Given matrix is not s y m m e t r i c "
80 i f n o t A . is_square () :
81 r a i s e TypeError , " E r r o r : T h e m a t r i x A i s n o t s q u a r e "
82 i f n o t v . degree () == n :
83 r a i s e TypeError , " T h e s i z e d o n o t a g r e e "
84 r e t u r n s u m ([ A [i , j ]* v [ i ]* v [ j ] f o r i i n r a n g e ( n ) f o r j i n r a n g e (–â
n) ]) . simplify_full ()
85

86 c l a s s SymmetricMatrix ( o b j e c t ) :
87 """
88 P r o v i d e i n f o r m a t i o n s a b o u t t h e m a t r i x A a s s u m i n g i t i s s y m m e t r i c –â
.
89 """
90 d e f __init__ ( self , A ) :
91 i f n o t A . is_square () :
92 print " Error : A s y m m e t r i c matrix must be square "
93 r a i s e TypeError
94 self . matrix = A
95 self . degree = A . nrows ()
96 self . matrix . set_immutable ()
97 d e f p r i m a r y _ p r i n c i p a l _ s u b m a t r i x ( self , n ) :
98 """
99 R e t u r n t h e p r i m a r y p r i n c i p a l s u b m a t r i x o f o r d e r n , t h a t i s t h e –â
matrix obtained
100 b y r e m o v i n g t h e n l a s t l i n e s a n d c o l u m n s f r o m s e l f –â
.
101 """
102 taille = self . degree - n
103 v =[]
104 f o r i i n r a n g e (0 , taille ) :
105 v . append ( self . matrix [ i ][0: taille ])
106 r e t u r n matrix ( v )
107 d e f principal_minors ( self ) :
108 """
2026 CHAPITRE 43. EXEMPLES AVEC SAGE

109 R e t u r n t h e l i s t o f p r i n c i p a l m i n o r s . T h e p r i n c i p a l m i n o r o f –â
order k is
110 t h e d e t e r m i n a n t o f t h e p r i m a r y p r i n c i p a l m a t r i x o f –â
order k.
111 """
112 a =[]
113 f o r i i n r a n g e ( self . degree ) :
114 a . append ( self . p r i m a r y _ p r i n c i p a l _ s u b m a t r i x ( i ) . determinant () )
115 return a
116 d e f genre_list ( self ) :
117 """
118 R e t u r n t h e g e n i u s o f t h e m a t r i x a s a l i s t o f b o o l e a n s i n t h e –â
order
119 positive defined , negative defined ;
120 s e m i d e f i n i t e p o s i t i v e , s e m i d e f i n i t e n e g a t i v e , –â
indefinite .
121

122 """
123 defpos = True
124 defneg = True
125 semidefpos = True
126 semidefneg = True
127 indefinie = True
128 mineurs = self . principal_minors ()
129 f o r i i n r a n g e ( l e n ( mineurs ) ) :
130 m = mineurs [ i ]
131 i f m == 0:
132 defneg = False
133 defpos = False
134 i f m < 0:
135 defpos = False
136 semidefpos = False
137 i f i %2==0:
138 defneg = False
139 i f m > 0:
140 semidefneg = False
141 i f i %2==1:
142 defneg = False
143 i f 0 n o t i n mineurs :
144 semidefneg = False
145 semidefpos = False
146 i f ( defpos == True ) o r ( defneg == True ) o r ( semidefpos == True ) o r (–â
semidefneg == True ) : indefinie = False
147 r e t u r n [ defpos , defneg , semidefpos , semidefneg , indefinie ]
148 d e f __str__ ( self ) :
149 r e t u r n s t r ( self . matrix )
150

151 c l a s s QuadraticForm ( SymmetricMatrix ) :


152 """
153 F r o m a s y m m e t r i c m a t r i x A , p r o v i d e i n f o r m a t i o n s c o n c e r n i n g t h e –â
associated quadratic form .
154 """
155 d e f __init__ ( self , A ) :
2027

156 SymmetricMatrix . __init__ ( self , A )


157 i f n o t A . is_symmetric () :
158 print " W a r n i n g : matrix is not s y m m e t r i c "
159 d e f evaluate ( self , v ) :
160 """
161 Return the value of the q u a d r a t i c form on the vector v .
162 """
163 r e t u r n QuadraticMap ( self . matrix , v )
164 d e f di a g o n a li z i n g _ m a r tr i x ( self ) :
165 """
166 Return the matrix B such that B ^ tAB is d i a g o n a l .
167 """
168 # T h e t r a n s p o s i t i o n i s b e c a u s e , i n t h e m a t r i x B , t h e –â
eigenvectors have
169 # t o b e r e a d a s c o l u m n w h i l e S a g e ’ s m a t r i x c o n s t r u c t o r t a k e s –â
rows .
170 r e t u r n matrix ( self . or thonormal_basis () ) . transpose ()
171 d e f new_variables ( self ) :
172 """
173 G i v e t h e c h a n g e o f v a r i a b l e s n e e d e d t o p u t t h e q u a d r a t i c f o r m –â
under its normal form
174 X = BY
175 where X are the " old " variables
176 """
177 variables = var ( automatedVar ( " y " , self . degree ) )
178 Y = vector ( variables )
179 r e t u r n self . d i ag o n a l i z ing_ martr ix () * Y
180 d e f eigenmatrix_left ( self ) :
181 r e t u r n self . matrix . eigenmatrix_left ()
182 d e f eigenvectors ( self ) :
183 """
184 Return a list of e i g e n v e c t o r s of the matrix .
185

186 As the matrix is symmetric , that list has to be a basis .


187 """
188 D , P = self . eigenmatrix_left ()
189 r e t u r n [ P [ i ] f o r i i n r a n g e ( P . nrows () ) ]
190 d e f eigenvalues ( self ) :
191 """
192 R e t u r n a l i s t o f e i g e n v a l u e s o f t h e m a t r i x i n t h e s a m e o r d e r –â
as the list of e i g e n v e c t o r s given in
193 self . e i g e n v e c t o r s ()
194 """
195 D , P = self . eigenmatrix_left ()
196 r e t u r n [ D [i , i ] f o r i i n r a n g e ( D . nrows () ) ]
197 d e f orth onorm al_bas is ( self ) :
198 """
199 Return a basis of e i g e n v e c t o r s n o r m a l i s e d to 1 as a list .
200 """
201 M , mu = matrix ( self . eigenvectors () ) . gram_schmidt ()
202 r e t u r n [ v / v . norm () f o r v i n M ]
203 d e f verification ( self ) :
204 """
2028 CHAPITRE 43. EXEMPLES AVEC SAGE

205 r e t u r n t h e v a l u e o f t h e q u a d r a t i c f o r m o n t h e v e c t o r –â
n e w _ v a r i a b l e s ()
206 """
207 r e t u r n self . evaluate ( self . new_variables () )
208

209 d e f __str__ ( self ) :


210 a = []
211 a . append ( " H i g u y ; I ’ m t h e q u a d r a t i c f o r m a s s o c i a t e d w i t h t h e –â
matix ")
212 a . append ( s t r ( self . matrix ) )
213 a . append ( " M y e i g e n v a l u e s a n d e i g e n v e c t o r s a r e : " )
214 veps = self . eigenvectors ()
215 vaps = self . eigenvalues ()
216 f o r i i n r a n g e ( l e n ( veps ) ) :
217 a . append ( " % s - > % s " %( s t r ( vaps [ i ]) , s t r ( veps [ i ]) ) )
218 a . append ( " I ’ v e t h e f o l l o w i n g o r t h o n o r m a l b a s i s o f e i g e n v e c t o r s –â
:")
219 f o r v i n self . or thonormal_basis () :
220 a . append ( s t r ( v ) )
221 a . append ( " A m a t r i x B s u c h t h a t B ^ t A B i s d i a g o n a l i s " )
222 a . append ( s t r ( self . d i a g o n al i zi n g _ m a r t r i x () ) )
223 a . append ( " I ’ m q u i t e p r e t t y i n t h e f o l l o w i n g v a r i a b l e s . . . " )
224 f o r i i n r a n g e ( self . degree ) :
225 a . append ( " x % s = % s " %( s t r ( i +1) , s t r ( self . new_variables () [ i ]) )–â
)
226 a . append ( " L o o k a t m e w h e n I w e a r m y c o o l v a r i a b l e s " )
227 a . append ( s t r ( self . verification () ) )
228 r e t u r n " \ n " . join ( a )
229

230 c l a s s Extrema ( o b j e c t ) :
231 """
232 F r o m a f u n c t i o n f , p r o v i d e s t h e i n f o r m a t i o n s f o r t h e s t u d y o f –â
the extrema :
233 partial derivative
234 critical points
235 H e s s i a n matrix at the c r i t i c a l points
236 Genius of the H e s s i a n and c o n c l u s i o n as local min / max
237

238 D e a r s t u d e n t : r e m e m b e r t h a t t h i s c l a s s d o e s n o t f u r n i s h a n y –â
informations
239 c o n c e r n i n g * global * e x t r e m a . The latter have to be found
240 among the c r i t i c a l points OR on the border of the domain .
241 """
242 d e f __init__ ( self , f ) :
243 var ( ’ x , y ’ )
244 self . fun = f
245 self . gx = self . fun . diff ( x ) . full_simplify ()
246 self . gy = self . fun . diff ( y ) . full_simplify ()
247 self . gxx = self . gx . diff ( x ) . simplify_full ()
248 self . gxy = self . gx . diff ( y ) . full_simplify ()
249 self . gyy = self . gy . diff ( y ) . full_simplify ()
250 self . cp = solve ( [ self . gx (x , y ) ==0 , self . gy (x , y ) ==0] ,[ x , y ] )
251 d e f critical_points ( self ) :
2029

252 """
253 Return the c r i t i c a l points as a list of tuples (x , y )
254 """
255 a = []
256 f o r pt i n self . cp :
257 try :
258 px = SR ( pt [0]. rhs () )
259 py = SR ( pt [1]. rhs () )
260 a . append (( px , py ) )
261 e x c e p t TypeError :
262 a . append ( " I ’ m n o t a b l e t o s o l v e t h e s e e q u a t i o n s . " )
263 return a
264 d e f hessienne ( self ,a , b ) :
265 r e t u r n matrix ( SR ,2 ,2 ,[ self . gxx (a , b ) , self . gxy (a , b ) , self . gxy (a ,–â
b ) , self . gyy (a , b ) ])
266 d e f __str__ ( self ) :
267 a = []
268 a . append ( " T h e f u n c t i o n : " )
269 a . append ( s t r ( self . fun ) )
270 a . append ( " D e r i v a t i v e x a n d y : " )
271 a . append ( s t r ( self . gx ) )
272 a . append ( s t r ( self . gy ) )
273 a . append ( " H e s s i a n m a t r i x : " )
274 a . append ( s t r ( self . hessienne (x , y ) ) )
275 a . append ( " C r i t i c a l p o i n t s : " )
276 f o r pt i n self . critical_points () :
277 a . append ( s t r ( pt ) )
278 f o r pt i n self . critical_points () :
279 try :
280 px = pt [0]
281 py = pt [1]
282 a . append ( " A t ( % s , % s ) , t h e H e s s i a n i s " %( s t r ( px ) , s t r ( py ) ) )
283 try :
284 Hess = SymmetricMatrix ( self . hessienne ( px , py ) )
285 f o r l i n Hess . matrix :
286 a . append ( " " + s t r ( l ) )
287 a . append ( " P r i m a r y p r i n c i p a l m i n o r s a r e % s " % s t r ( Hess .–â
principal_minors () ) )
288 l = Hess . genre_list ()
289 i f l [0]== True :
290 a . append ( " H e s s i a n p o s i t i v e d e f i n e d " )
291 a . append ( " l o c a l m i n i m u m " )
292 i f l [1]== True :
293 a . append ( " H e s s i a n n e g a t i v e d e f i n e d " )
294 a . append ( " l o c a l m a x i m u m " )
295 i f l [2]== True :
296 a . append ( " H e s s i a n p o s i t i v e s e m i d é f i n i t e " )
297 a . append ( " I d o n ’ t c o n c l u d e " )
298 i f l [3]== True :
299 a . append ( " H e s s i a n n e g a t i v e s e m i d e f i n i t e " )
300 a . append ( " I d o n ’ t c o n c l u d e " )
301 i f l [4]== True :
302 a . append ( " U n d e f i n i t e H e s s i a n " )
2030 CHAPITRE 43. EXEMPLES AVEC SAGE

303 a . append ( " « s e l l e » p o i n t " )


304 e x c e p t RuntimeError , data :
305 a . append ( " " + s t r ( data ) )
306 e x c e p t TypeError :
307 a . append ( " I ’ m n o t a b l e t o s o l v e t h e s e e q u a t i o n s . " )
308 r e t u r n " \ n " . join ( a )

tex/sage/outilsINGE.sage

Exemple 43.1
Calculer la limite
sinpxq cospxq
lim (43.1)
xÑ8 x

var(’x’)
f(x)=sin(x)*cos(x)/x
limit(f(x),x=oo)

La première ligne déclare que la lettre x désignera une variable. Pour la troisième ligne, notez que
l’infini est écrit par deux petits « o ». —

Exemple 43.2
Quelques limites et graphes avec Sage.
(1) limxÑ0 sinp–xq
sinp—xq .
Pour effectuer cet exercice avec Sage, il faut taper les lignes suivantes :

sage: var(’x,a,b’)
(x, a, b)
sage: f(x)=sin(a*x)/sin(b*x)
sage: limit( f(x),x=0 )
a/b

Noter qu’il faut déclarer les variables x, a et b.


?
x2 `1´x
(2) limxÑ˘8 x´2

sage: f(x)=(sqrt(x**2+1))/(x-2)
sage: limit(f(x),x=oo)
1
sage: limit(f(x),x=-oo)
-1

Noter la commande pour la racine carré : sqrt. Étant donné que cette fonction diverge en
x “ 2, si nous voulons la tracer, il faut procéder en deux fois :

sage: plot(f,(-100,1.9))
Launched png viewer for Graphics object consisting of 1 graphics primitive
sage: plot(f,(2.1,100))
Launched png viewer for Graphics object consisting of 1 graphics primitive

La première ligne trace de ´100 à 1.9 et la seconde de 2.1 à 100. Ces graphiques vous
permettent déjà de voir les limites. Attention : ils ne sont pas des preuves ! Mais ils sont de
sérieux indices qui peuvent vous inspirer dans vos calculs.
2031

Exemple 43.3

Calculer les dérivées partielles Bx f , By f , Bx2 f , Bxy


2 f , B 2 f et B 2 f des fonctions suivantes.
yx y

(1) 2x3 ` 3x2 y ´ 2y 2 (3) tanpx{yq


xy 2
(2) lnpxy 2 q (4) x`y

Le script Sage suivant (exoDV002.sage) résout l’exercice :

1 # -* - c o d i n g : u t f 8 -* -
2

3 d e f LesCalculs ( f ) :
4 print " Pour la f o n c t i o n % s "% str (f)
5 p r i n t " d _ x " ,f . diff ( x ) . simplify_full ()
6 p r i n t " d _ y " ,f . diff ( y ) . simplify_full ()
7 p r i n t " d ^ 2 _ x " ,f . diff ( x ) . diff ( x ) . simplify_full ()
8 p r i n t " d _ x d _ y " ,f . diff ( x ) . diff ( y ) . simplify_full ()
9 p r i n t " d _ y d _ x " ,f . diff ( y ) . diff ( x ) . simplify_full ()
10 p r i n t " d ^ 2 _ y " ,f . diff ( y ) . diff ( y ) . simplify_full ()
11 print ""
12

13 d e f exercise_DV002 () :
14 var ( ’ x , y ’ )
15 fa (x , y ) =2* x **3+3* x **2* y -2* y **2
16 fb (x , y ) = ln ( x * y **2)
17 fc (x , y ) = tan ( x / y )
18 fd (x , y ) = x * y **2/( x + y )
19 LesCalculs ( fa )
20 LesCalculs ( fb )
21 LesCalculs ( fc )
22 LesCalculs ( fd )
tex/sage/exoDV002.sage

La sortie est :

Pour la fonction (x, y) |--> 2*x^3 + 3*x^2*y - 2*y^2


d_x (x, y) |--> 6*x^2 + 6*x*y
d_y (x, y) |--> 3*x^2 - 4*y
d^2_x (x, y) |--> 12*x + 6*y
d_xd_y (x, y) |--> 6*x
d_yd_x (x, y) |--> 6*x
d^2_y (x, y) |--> -4

Pour la fonction (x, y) |--> log(x*y^2)


d_x (x, y) |--> 1/x
d_y (x, y) |--> 2/y
d^2_x (x, y) |--> -1/x^2
d_xd_y (x, y) |--> 0
d_yd_x (x, y) |--> 0
d^2_y (x, y) |--> -2/y^2
2032 CHAPITRE 43. EXEMPLES AVEC SAGE

Pour la fonction (x, y) |--> tan(x/y)


d_x (x, y) |--> 1/(y*cos(x/y)^2)
d_y (x, y) |--> -x/(y^2*cos(x/y)^2)
d^2_x (x, y) |--> 2*sin(x/y)/(y^2*cos(x/y)^3)
d_xd_y (x, y) |--> -(2*x*sin(x/y) + y*cos(x/y))/(y^3*cos(x/y)^3)
d_yd_x (x, y) |--> -(2*x*sin(x/y) + y*cos(x/y))/(y^3*cos(x/y)^3)
d^2_y (x, y) |--> 2*(x^2*sin(x/y) + x*y*cos(x/y))/(y^4*cos(x/y)^3)

Pour la fonction (x, y) |--> x*y^2/(x + y)


d_x (x, y) |--> y^3/(x^2 + 2*x*y + y^2)
d_y (x, y) |--> (2*x^2*y + x*y^2)/(x^2 + 2*x*y + y^2)
d^2_x (x, y) |--> -2*y^3/(x^3 + 3*x^2*y + 3*x*y^2 + y^3)
d_xd_y (x, y) |--> (3*x*y^2 + y^3)/(x^3 + 3*x^2*y + 3*x*y^2 + y^3)
d_yd_x (x, y) |--> (3*x*y^2 + y^3)/(x^3 + 3*x^2*y + 3*x*y^2 + y^3)
d^2_y (x, y) |--> 2*x^3/(x^3 + 3*x^2*y + 3*x*y^2 + y^3)

Exemple 43.4

Résoudre les systèmes suivants.


$ $
& x1 ´ 2x2 ` 3x3 ´ 2x4 “ 0 & x1 ´ 2x2 ` 3x3 ´ 2x4 “ 0
(1) 3x1 ´ 7x2 ´ 2x3 ` 4x4 “ 0 (8) 3x1 ´ 7x2 ´ 2x3 ` 4x4 “ 0
% %
4x1 ` 3x2 ` 5x3 ` 2x4 “ 0 4x1 ` 3x2 ` 5x3 ` 2x4 “ 0
$ $
& 2x1 ` x2 ´ 2x3 ` 3x4 “ 0 & 2x1 ` x2 ´ 2x3 ` 3x4 “ 0
(2) 3x1 ` 2x2 ´ x3 ` 3x4 “ 4 (9) 3x1 ` 2x2 ´ x3 ` 3x4 “ 4
% %
3x1 ` 3x2 ` 3x3 ´ 3x4 “ 9 3x1 ` 3x2 ` 3x3 ´ 3x4 “ 9
$ $
& x1 ` 2x2 ´ 3x3 “ 0 & x1 ` 2x2 ´ 3x3 “ 0
(3) 2x1 ` 5x2 ` 2x3 “ 0 (10) 2x1 ` 5x2 ` 2x3 “ 0
% %
3x1 ´ x2 ´ 4x3 “ 0 3x1 ´ x2 ´ 4x3 “ 0
$ $

’ x1 ` 2x2 ´ x3 “ 0 ’
’ x1 ` 2x2 ´ x3 “ 0
& &
2x1 ` 5x2 ` 2x3 “ 0 2x1 ` 5x2 ` 2x3 “ 0
(4) (11)
’ x ` 4x2 ` 7x3 “ 0 ’ x ` 4x2 ` 7x3 “ 0
% 1
’ % 1

x1 ` 3x2 ` 3x3 “ 0 x1 ` 3x2 ` 3x3 “ 0
$ $

’ x1 ` x2 ` x3 ` x4 “ 0 ’
’ x1 ` x2 ` x3 ` x4 “ 0
& &
x1 ` x2 ` x3 ´ x4 “ 4 x1 ` x2 ` x3 ´ x4 “ 4
(5) (12)
’ x ` x2 ´ x3 ` x4 “ ´4 ’ x ` x2 ´ x3 ` x4 “ ´4
% 1
’ % 1

x1 ´ x2 ` x3 ` x4 “ 2 x1 ´ x2 ` x3 ` x4 “ 2
$ $
& x1 ` 3x2 ` 3x3 “ 1 & x1 ` 3x2 ` 3x3 “ 1
(6) x1 ` 3x2 ` 4x3 “ 0 (13) x1 ` 3x2 ` 4x3 “ 0
% %
x1 ` 4x2 ` 3x3 “ 3 x1 ` 4x2 ` 3x3 “ 3
$ $
& x1 ´ 3x2 ` 2x3 “ ´6 & x1 ´ 3x2 ` 2x3 “ ´6
(7) ´3x1 ` 3x2 ´ x3 “ 17 (14) ´3x1 ` 3x2 ´ x3 “ 17
% %
2x1 ´ x2 “ 3 2x1 ´ x2 “ 3

Nous résolvons les systèmes en utilisant Sage avec le script suivant.

1 # -* - c o d i n g : u t f 8 -* -
2 """
2033

3 Ce script Sage résout un c e r t a i n nombre


4 de s y s t è m e s d ’ é q u a t i o n s l i n é a i r e s du cours I N G E 1 1 2 1
5 """
6

7 i m p o r t outilsINGE
8

9 d e f e xe rc i s e _ 1_ 1 _ b c d e fh i () :
10 # Exercice 1.1. b ( INGE1121 )
11 A = matrix ([ [1 , -2 ,3 , -2 ,0] ,[3 , -7 , -2 ,4 ,0] ,[4 ,3 ,5 ,2 ,0] ])
12 v = vector ((0 ,0 ,0 ,0 ,0) )
13 p r i n t outilsINGE . Solve Linear Syste m (A , v )
14 # Exercice 1.1. c ( INGE1121 )
15 A = matrix ([ [2 ,1 , -2 ,3] ,[3 ,2 , -1 ,3] ,[3 ,3 ,3 , -3] ])
16 v = vector ((0 ,4 ,9) )
17 p r i n t outilsINGE . Solve Linear Syste m (A , v )
18 # Exercice 1.1. d ( INGE1121 )
19 A = matrix ([ [1 ,2 , -3] ,[2 ,5 ,2] ,[3 , -1 , -4] ])
20 v = vector ((0 ,0 ,0) )
21 p r i n t outilsINGE . Solve Linear Syste m (A , v )
22 # Exercice 1.1. e ( INGE1121 )
23 A = matrix ([ [1 ,2 , -1] ,[2 ,5 ,2] ,[1 ,4 ,7] ,[1 ,3 ,3] ])
24 v = vector ((0 ,0 ,0 ,0) )
25 p r i n t outilsINGE . Solve Linear Syste m (A , v )
26 # Exercice 1.1. f ( INGE1121 )
27 A = matrix ([ [1 ,1 ,1 ,1] ,[1 ,1 ,1 , -1] ,[1 ,1 , -1 ,1] ,[1 , -1 ,1 ,1] ])
28 v = vector ((0 ,4 , -4 ,2) )
29 p r i n t outilsINGE . Solve Linear Syste m (A , v )
30 # Exercice 1.1. h ( INGE1121 )
31 A = matrix ([ [1 ,3 ,3] ,[1 ,3 ,4] ,[1 ,4 ,3] ])
32 v = vector ((1 ,0 ,3) )
33 p r i n t outilsINGE . Solve Linear Syste m (A , v )
34 # Exercice 1.1. i ( INGE1121 )
35 A = matrix ([ [1 , -3 ,2] ,[ -3 ,3 , -1] ,[2 , -1 ,0] ])
36 v = vector (( -6 ,17 ,3) )
37 p r i n t outilsINGE . Solve Linear Syste m (A , v )

tex/sage/exo11.sage

Le résultat est le suivant :

The given matrix corresponds to the system


x1 - 2*x2 + 3*x3 - 2*x4 == 0
3*x1 - 7*x2 - 2*x3 + 4*x4 == 0
4*x1 + 3*x2 + 5*x3 + 2*x4 == 0
And the solutions are
[
[x1 == -23/16*r19, x2 == -5/16*r19, x3 == 15/16*r19, x4 == r19, x5 == r18]
]
The given matrix corresponds to the system
2*x1 + x2 - 2*x3 + 3*x4 == 0
3*x1 + 2*x2 - x3 + 3*x4 == 4
3*x1 + 3*x2 + 3*x3 - 3*x4 == 9
And the solutions are
[
[x1 == 3*r20 - 7, x2 == -4*r20 + 11, x3 == r20, x4 == 1]
2034 CHAPITRE 43. EXEMPLES AVEC SAGE

]
The given matrix corresponds to the system
x1 + 2*x2 - 3*x3 == 0
2*x1 + 5*x2 + 2*x3 == 0
3*x1 - x2 - 4*x3 == 0
And the solutions are
[
[x1 == 0, x2 == 0, x3 == 0]
]
The given matrix corresponds to the system
x1 + 2*x2 - x3 == 0
2*x1 + 5*x2 + 2*x3 == 0
x1 + 4*x2 + 7*x3 == 0
x1 + 3*x2 + 3*x3 == 0
And the solutions are
[
[x1 == 9*r21, x2 == -4*r21, x3 == r21]
]
The given matrix corresponds to the system
x1 + x2 + x3 + x4 == 0
x1 + x2 + x3 - x4 == 4
x1 + x2 - x3 + x4 == -4
x1 - x2 + x3 + x4 == 2
And the solutions are
[
[x1 == 1, x2 == -1, x3 == 2, x4 == -2]
]
The given matrix corresponds to the system
x1 + 3*x2 + 3*x3 == 1
x1 + 3*x2 + 4*x3 == 0
x1 + 4*x2 + 3*x3 == 3
And the solutions are
[
[x1 == -2, x2 == 2, x3 == -1]
]
The given matrix corresponds to the system
x1 - 3*x2 + 2*x3 == -6
-3*x1 + 3*x2 - x3 == 17
2*x1 - x2 == 3
And the solutions are
[
[x1 == 37, x2 == 71, x3 == 85]
]

Exemple 43.5

Pour chacun des systèmes suivants A · X “ B,


(1) Résoudre le système par échelonnement,
(2) Calculer A´1 ,
(3) Vérifier votre réponse en calculant A´1 B. Qu’êtes-vous censé obtenir ?
2035

Les énoncés sont


(1) ¨ ˛ ¨ ˛
2 1 ´2 10
A “ ˝3 2 2 ‚, B “ ˝ 1 ‚ (43.2)
5 4 3 4
Nous utilisons Sage pour fournir la réponse. Le code suivant résout le système et donne l’inverse
de la matrice :

1 # -* - c o d i n g : u t f 8 -* -
2

3 i m p o r t outilsINGE
4

5 d e f exercise_1_3 () :
6 A = matrix ([[2 ,1 , -2] ,[3 ,2 ,2] ,[5 ,4 ,3]])
7 v = vector ((10 ,1 ,4) )
8 p r i n t outilsINGE . Solve Linear Syste m (A , v )
9 print " Matrice inverse :"
10 p r i n t A . inverse ()
tex/sage/exo13.sage
La sortie est ici :
The given matrix corresponds to the system
2*x1 + x2 - 2*x3 == 10
3*x1 + 2*x2 + 2*x3 == 1
5*x1 + 4*x2 + 3*x3 == 4
And the solutions are
[
[x1 == 1, x2 == 2, x3 == -3]
]
Matrice inverse :
[ 2/7 11/7 -6/7]
[ -1/7 -16/7 10/7]
[ -2/7 3/7 -1/7]

Exemple 43.6

Sachant que p´1, 0, 1, 0q est un vecteur propre de la matrice


¨ ˛
2 1 ´1 1
˚1 0 1 1‹
A“˚ ˝´1 1 2
‹ (43.3)
1‚
1 1 1 0
(1) Diagonaliser A au moyen d’une matrice orthogonale
(2) Écrire la forme quadratique X t AX sous forme d’une somme pondérée de carrés.
Calculons Av afin de savoir la valeur propre associée au vecteur donné :
¨ ˛¨ ˛ ¨ ˛
2 1 ´1 1 ´1 ´3
˚ 1 0 1 1‹ ˚ 0 ‹ ˚ 0 ‹
˚ ‹˚ ‹ ˚ ‹
˝´1 1 2 1‚˝ 1 ‚ “ ˝ 3 ‚. (43.4)
1 1 1 0 0 0
2036 CHAPITRE 43. EXEMPLES AVEC SAGE

La valeur propre est donc 3. Nous savons donc que p⁄ ´ 3q pourra être factorisé dans le polynôme
caractéristique.
Pour le reste de l’exercice c’est standard et c’est résolu de la façon suivante :

1 # -* - c o d i n g : u t f 8 -* -
2

3 i m p o r t outilsINGE
4

5 d e f exercise_6_5 () :
6 A = matrix ( QQ ,4 ,4 ,[2 ,1 , -1 ,1 ,1 ,0 ,1 ,1 , -1 ,1 ,2 ,1 ,1 ,1 ,1 ,0])
7 x = outilsINGE . QuadraticForm ( A )
8 print x

tex/sage/exo65.sage

qui retourne

Hi guy; I’m the quadratic form associated with the matix


[ 2 1 -1 1]
[ 1 0 1 1]
[-1 1 2 1]
[ 1 1 1 0]
My eigenvalues and eigenvectors are :
3 -> (1, 0, -1, 0)
3 -> (0, 1, 2, 1)
-1 -> (1, 0, 1, -2)
-1 -> (0, 1, 0, -1)
I’ve the following orthonormal basis of eigenvectors :
(1/2*sqrt(2), 0, -1/2*sqrt(2), 0)
(1/2, 1/2, 1/2, 1/2)
(1/6*sqrt(6), 0, 1/6*sqrt(6), -1/3*sqrt(6))
(-1/2*sqrt(1/3), 3/2*sqrt(1/3), -1/2*sqrt(1/3), -1/2*sqrt(1/3))
A matrix B such that B^tAB is diagonal is
[ 1/2*sqrt(2) 1/2 1/6*sqrt(6) -1/2*sqrt(1/3)]
[ 0 1/2 0 3/2*sqrt(1/3)]
[ -1/2*sqrt(2) 1/2 1/6*sqrt(6) -1/2*sqrt(1/3)]
[ 0 1/2 -1/3*sqrt(6) -1/2*sqrt(1/3)]
I’m quite pretty in the following variables ...
x1 = -1/2*sqrt(1/3)*y4 + 1/2*sqrt(2)*y1 + 1/6*sqrt(6)*y3 + 1/2*y2
x2 = 3/2*sqrt(1/3)*y4 + 1/2*y2
x3 = -1/2*sqrt(1/3)*y4 - 1/2*sqrt(2)*y1 + 1/6*sqrt(6)*y3 + 1/2*y2
x4 = -1/2*sqrt(1/3)*y4 - 1/3*sqrt(6)*y3 + 1/2*y2
Look at me when I wear my cool variables
3*y1^2 + 3*y2^2 - y3^2 - y4^2

Exemple 43.7

Rechercher les extrema des fonctions suivantes


a
(1) f px, yq “ 2 ´ x2 ` y 2
(2) f px, yq “ x3 ` 3xy 2 ´ 15x ´ 12y
2037

x3 4y 3
(3) f px, yq “ 3 ` 3 ´ x2 ´ 3x ´ 4y ´ 3
Les corrigés sont créés par le script Sage exo101.sage

# -*- coding: utf8 -*-

import outilsINGE

def exercise_10_1_A():
var(’x,y’)
f(x,y)=2-sqrt(x**2+y**2)
print outilsINGE.Extrema(f)
def exercise_10_1_B():
var(’x,y’)
f(x,y)=x**3+3*x*y**2-15*x-12*y
print outilsINGE.Extrema(f)
def exercise_10_1_C():
var(’x,y’)
f(x,y)=x**3/3+4*y**3/3-x**2-3*x-4*y-3
print outilsINGE.Extrema(f)

Des réponses :
(1) The function :
(x, y) |--> -sqrt(x^2 + y^2) + 2
Derivative x and y :
(x, y) |--> -x/sqrt(x^2 + y^2)
(x, y) |--> -y/sqrt(x^2 + y^2)
Hessian matrix :
[-sqrt(x^2 + y^2)*y^2/(x^4 + 2*x^2*y^2 + y^4) x*y/(x^2 + y^2)^(3
[ x*y/(x^2 + y^2)^(3/2) -sqrt(x^2 + y^2)*x^2/(x^4 + 2*x^2*y^2 + y
Critical points :
(0, 0)
At (0,0), the Hessian is
power::eval(): division by zero
Ici nous voyons que Sage a du mal à calculer la matrice hessienne en p0, 0q. En effet, nous
tombons sur une division
a par zéro. Pour résoudre l’exercice, il faut se rendre compte que
la fonction px, yq ބ x2 ` y 2 est toujours positive et est nulle seulement au point p0, 0q.
Donc f est toujours plus petite ou égale à deux tandis que f p0, 0q “ 2. Le point est donc
un maximum global.
(2) The function :
(x, y) |--> x^3 + 3*x*y^2 - 15*x - 12*y
Derivative x and y :
(x, y) |--> 3*x^2 + 3*y^2 - 15
(x, y) |--> 6*x*y - 12
Hessian matrix :
[6*x 6*y]
[6*y 6*x]
Critical points :
(2, 1)
(1, 2)
(-1, -2)
(-2, -1)
At (2,1), the Hessian is
(12, 6)
2038 CHAPITRE 43. EXEMPLES AVEC SAGE

(6, 12)
Primary principal minors are [108, 12]
Hessian positive defined
local minimum
At (1,2), the Hessian is
(6, 12)
(12, 6)
Primary principal minors are [-108, 6]
Undefinite Hessian
«selle» point
At (-1,-2), the Hessian is
(-6, -12)
(-12, -6)
Primary principal minors are [-108, -6]
Undefinite Hessian
«selle» point
At (-2,-1), the Hessian is
(-12, -6)
(-6, -12)
Primary principal minors are [108, -12]
Hessian negative defined
local maximum

(3) The function :


(x, y) |--> 1/3*x^3 - x^2 + 4/3*y^3 - 3*x - 4*y - 3
Derivative x and y :
(x, y) |--> x^2 - 2*x - 3
(x, y) |--> 4*y^2 - 4
Hessian matrix :
[2*x - 2 0]
[ 0 8*y]
Critical points :
(3, 1)
(-1, 1)
(3, -1)
(-1, -1)
At (3,1), the Hessian is
(4, 0)
(0, 8)
Primary principal minors are [32, 4]
Hessian positive defined
local minimum
At (-1,1), the Hessian is
(-4, 0)
(0, 8)
Primary principal minors are [-32, -4]
Undefinite Hessian
«selle» point
At (3,-1), the Hessian is
(4, 0)
(0, -8)
Primary principal minors are [-32, 4]
Undefinite Hessian
«selle» point
2039

At (-1,-1), the Hessian is


(-4, 0)
(0, -8)
Primary principal minors are [32, -4]
Hessian negative defined
local maximum

Exemple 43.8

Déterminer les valeurs extrêmes et les points de selle des fonctions suivantes.

(1) f px, yq “ x2 ` 4x ` y 2 ´ 2y. (3) f px, yq “ ex sinpyq.


2
(2) f px, yq “ ex `xy .

Certains corrigés de cet exercice ont étés réalisés par Sage. Le script utilisé est exo103.sage

1 # -* - c o d i n g : u t f 8 -* -
2

3 i m p o r t outilsINGE
4

5 d e f exercise_10_3_A () :
6 var ( ’ x , y ’ )
7 f (x , y ) = x **2+4* x + y **2 -2* y
8 p r i n t outilsINGE . Extrema ( f )
9

10 d e f exercise_10_3_H () :
11 var ( ’ x , y ’ )
12 f (x , y ) = exp ( x **2+ x * y )
13 p r i n t outilsINGE . Extrema ( f )
14

15 d e f exercise_10_3_Q () :
16 var ( ’ x , y ’ )
17 f (x , y ) = exp ( x ) * sin ( y )
18 p r i n t outilsINGE . Extrema ( f )

tex/sage/exo103.sage

Des réponses :
(1) The function :
(x, y) |--> x^2 + y^2 + 4*x - 2*y
Derivative x and y :
(x, y) |--> 2*x + 4
(x, y) |--> 2*y - 2
Hessian matrix :
[2 0]
[0 2]
Critical points :
(-2, 1)
At (-2,1), the Hessian is
(2, 0)
(0, 2)
2040 CHAPITRE 43. EXEMPLES AVEC SAGE

Primary principal minors are [4, 2]


Hessian positive defined
local minimum
(2) The function :
(x, y) |--> e^(x^2 + x*y)
Derivative x and y :
(x, y) |--> (2*x*e^(x^2) + y*e^(x^2))*e^(x*y)
(x, y) |--> x*e^(x^2 + x*y)
Hessian matrix :
[(4*x*y*e^(x^2) + y^2*e^(x^2) + 2*(2*x^2 + 1)*e^(x^2))*e^(x*y) (x*y*e^(x
[ (x*y*e^(x^2) + (2*x^2 + 1)*e^(x^2))*e^(x*y)
Critical points :
(0, 0)
At (0,0), the Hessian is
(2, 1)
(1, 0)
Primary principal minors are [-1, 2]
Undefinite Hessian
«selle» point
(3) The function :
(x, y) |--> e^x*sin(y)
Derivative x and y :
(x, y) |--> e^x*sin(y)
(x, y) |--> e^x*cos(y)
Hessian matrix :
[ e^x*sin(y) e^x*cos(y)]
[ e^x*cos(y) -e^x*sin(y)]
Critical points :
I’m not able to solve these equations.
I’m not able to solve these equations.
At ( ,I), the Hessian is
I’m not able to solve these equations.
At ( ,I), the Hessian is
I’m not able to solve these equations.
Ici, Sage n’est pas capable de résoudre les équations qui annulent le jacobien. Les équations
à résoudre sont pourtant faciles :
" x
e cospyq “ 0 (43.5a)
ex sinpyq “ 0 (43.5b)

Étant donné que l’exponentielle ne s’annule jamais, il faudrait avoir en même temps cospyq “
0 et sinpyq “ 0, ce qui est impossible. La fonction n’a donc aucun extrema local.

Exemple 43.9

Considérons la fonction
2 `y 2 q{4
f px, yq “ xy 2 e´px . (43.6)
(1) Montrer qu’il y a une infinité de points critiques.
(2) Déterminer leur nature.
2041

Voici la fonction Sage qui fournit les informations :

1 # -* - c o d i n g : u t f 8 -* -
2

3 i m p o r t outilsINGE
4

5 d e f exercise_10_4 () :
6 var ( ’ x , y ’ )
7 f (x , y ) = x * y **2* exp ( -( x **2+ y **2) /4)
8 p r i n t outilsINGE . Extrema ( f )

tex/sage/exo104.sage

La sortie est

The function :
(x, y) |--> x*y^2*e^(-1/4*x^2 - 1/4*y^2)
Derivative x and y :
(x, y) |--> -1/2*(x^2 - 2)*y^2*e^(-1/4*x^2 - 1/4*y^2)
(x, y) |--> -1/2*(x*y^3 - 4*x*y)*e^(-1/4*x^2 - 1/4*y^2)
Hessian matrix :
[ 1/4*(x^3 - 6*x)*y^2*e^(-1/4*x^2 - 1/4*y^2) 1/4*((x^2 - 2)*y^3 - 4*(x^2 - 2)*
[1/4*((x^2 - 2)*y^3 - 4*(x^2 - 2)*y)*e^(-1/4*x^2 - 1/4*y^2) 1/4*(x*y^4 - 10*x*y^2 + 8*
Critical points :
(r17, 0)
(-sqrt(2), -2)
(sqrt(2), -2)
(-sqrt(2), 2)
(sqrt(2), 2)
At (r17,0), the Hessian is
(0, 0)
(0, 2*r17*e^(-1/4*r17^2))
Primary principal minors are [0, 0]
Hessian positive semidéfinite
I don’t conclude
Hessian negative semidefinite
I don’t conclude
At (-sqrt(2),-2), the Hessian is
(4*sqrt(2)*e^(-3/2), 0)
(0, 4*sqrt(2)*e^(-3/2))
Primary principal minors are [32*e^(-3), 4*sqrt(2)*e^(-3/2)]
Hessian positive defined
local minimum
At (sqrt(2),-2), the Hessian is
(-4*sqrt(2)*e^(-3/2), 0)
(0, -4*sqrt(2)*e^(-3/2))
Primary principal minors are [32*e^(-3), -4*sqrt(2)*e^(-3/2)]
Hessian negative defined
local maximum
At (-sqrt(2),2), the Hessian is
(4*sqrt(2)*e^(-3/2), 0)
(0, 4*sqrt(2)*e^(-3/2))
Primary principal minors are [32*e^(-3), 4*sqrt(2)*e^(-3/2)]
Hessian positive defined
2042 CHAPITRE 43. EXEMPLES AVEC SAGE

local minimum
At (sqrt(2),2), the Hessian is
(-4*sqrt(2)*e^(-3/2), 0)
(0, -4*sqrt(2)*e^(-3/2))
Primary principal minors are [32*e^(-3), -4*sqrt(2)*e^(-3/2)]
Hessian negative defined
local maximum

Notez la présence de r1 comme paramètres dans les solutions. Tous les points avec y “ 0 sont
des points critiques. Cependant, Sage 1 ne parvient pas à conclure la nature de ces points px, 0q.
Notons que le nombre f px, yq a toujours le signe de x parce que y 2 et l’exponentielle sont
positives. Toujours ? En tout cas lorsque x ‰ 0. Prenons un point pa, 0q avec a ° 0. Dans un
voisinage de ce point, nous avons f px, yq ° 0 parce que si a ° 0, alors x ° 0 dans un voisinage de
a. Le point pa, 0q est un minimum local parce que 0 “ f pa, 0q § f px, yq pour tout px, yq dans un
voisinage de pa, 0q.
De la même façon, les points pa, 0q avec a † 0 sont des maxima locaux parce que dans un
voisinage, la fonction est négative.
Le point p0, 0q n’est ni maximum ni minimum local. C’est un point de selle.

Exemple 43.10

Dériver les fonctions suivantes.


` ˘
(1) sin lnpxq
sin x
(2) ;
x
2
(3) ex
(4) cospxqsinpxq
Le programme suivant par Sage résout l’exercice :

1 # ! / usr / bin / sage - python


2 # -* - c o d i n g : u t f 8 -* -
3

4 f r o m sage . a l l i m p o r t *
5

6 var ( ’ x ’ )
7 f = sin ( ln ( x ) )
8 p r i n t f . diff ( x )
9 f = sin ( x ) / x
10 p r i n t f . diff ( x )
11 f = exp ( x **2)
12 p r i n t f . diff ( x )
13 f = cos ( x ) **( sin ( x ) )
14 p r i n t f . diff ( x )

tex/sage/corrDerive_0002.sage

Le résultat est :

cos(log(x))/x
cos(x)/x - sin(x)/x^2
1. ou, plus précisément, le programme que j’ai écrit avec Sage.
2043

2*x*e^(x^2)
(log(cos(x))*cos(x) - sin(x)^2/cos(x))*cos(x)^sin(x)

Exemple 43.11

Donner une approximation de lnp1.0001q.

----------------------------------------------------------------------
| Sage Version 4.5.3, Release Date: 2010-09-04 |
| Type notebook() for the GUI, and license() for information. |
----------------------------------------------------------------------
sage: numerical_approx(ln(1.0001))
0.0000999950003332973

Vous aimerez peut-être aussi