CPGE : EL KHANSAA CASABLANCA Année scolaire : 2024/2025
Python : Correction TP matplotlib
Exercice 1 :
1 import numpy a s np
2 import m a t p l o t l i b . p y p l o t a s p l t
3
4
5 def f (x) :
6 r e t u r n ( np . exp ( x )−np . exp(−x ) ) /2
7
8 #Q u e s t i o n 1
9 X = np . l i n s p a c e ( −4 , 4 ,10 0)
10 Y = f (X)
11 #Q u e s t i o n 2+3+4
12 p l t . p l o t (X, Y, l a b e l=" f ( x ) " )
13 p l t . p l o t (Y, X, l a b e l=" f −1(x ) " )
14 plt . legend ()
15 plt . grid ()
16 p l t . show ( )
1
Le graphe de f −1 est le symétrique de celui de f par rapport à la droite d’équation y = x , il
suffit donc d’échanger les axes :
— Graphe de f :
plt.plot(x,y)
— Graphe de f −1
plt.plot(y,x)
Exercice 2 :
1 import numpy a s np
2 import m a t p l o t l i b . p y p l o t a s p l t
3
4 def f (x) :
5 r e t u r n x−np . l o g (1+np . abs ( x ) )
6
7 X = np . l i n s p a c e ( −1 , 1 ,10 0)
8 Y = f (X)
9 p l t . p l o t (X,Y)
10 plt . grid ()
11 p l t . show ( )
Le graphe présente deux demi tangentes en 0, l’une à gauche de coefficient directeur égal à -2,
l’autre à droite horizontale, donc fg′ (0) = 2 et fd′ (0) = 0, puisque fg′ (0) ̸= fd′ (0) alors f n’est pas
dérivable en 0.
2
Exercice 3 :
1 #Q u e s t i o n 1
2 import numpy a s np
3
4 n = e v a l ( i n p u t ( " E n t r e r un e n t i e r n : " ) )
5 V = 1 / np . a r a n g e ( 1 , n+1)
6 p r i n t (V)
1 #Q u e s t i o n 2
2 import numpy a s np
3 import m a t p l o t l i b . p y p l o t a s p l t
4
5 u = np . z e r o s ( 3 0 )
6 v = np . z e r o s ( 3 0 )
7 f o r i in range (30) :
8 u [ i ] = sum ( 1 / np . a r a n g e ( 1 , i +2) )−np . l o g ( i +1)
9 v [ i ] = sum ( 1 / np . a r a n g e ( 1 , i +1) )−np . l o g ( i +1)
10
11 x = np . a r a n g e ( 0 , 3 0 )
12 p l t . s c a t t e r ( x , u , l a b e l="Un" )
13 p l t . s c a t t e r ( x , v , l a b e l="Vn" )
14 plt . legend ()
15 plt . grid ()
16 p l t . show ( )
Le graphique montre les 30 premiers termes des suites un et vn . On constate sur le schéma que
3
ces suites sont monotones, de variations opposées et convergent vers une même limite qui vaut
environ 0,57.
Exercice 4 :
1 import numpy a s np
2 import m a t p l o t l i b . p y p l o t a s p l t
3
4 #Q u e s t i o n 1
5 d e f g a u s s (m, sigma , x ) :
6 r e t u r n np . exp (−(x−m) ∗ ∗ 2 / ( 2 ∗ sigma ∗ ∗ 2 ) ) / ( sigma ∗np . s q r t ( 2 ∗ np . p i ) )
7
8 #Q u e s t i o n 2
9 X = np . l i n s p a c e ( −4 , 4 ,10 0)
10 Y1 = g a u s s ( 0 , 1 ,X)
11 Y2 = g a u s s ( 0 , np . s q r t ( 2 ) ,X)
12 Y3 = g a u s s ( 0 , np . s q r t ( 3 ) ,X)
13 p l t . p l o t (X, Y1 , l a b e l="m=0 , sigma=1" )
14 p l t . p l o t (X, Y2 , l a b e l="m=0 , sigma =1.414 " )
15 p l t . p l o t (X, Y3 , l a b e l="m=0 , sigma =1.732 " )
16 p l t . t i t l e ( " La c l o c h e de g a u s s " )
17 plt . legend ()
18 p l t . show ( )