• Aucun résultat trouvé

sur un intervalle et pour le calcul approché de la solution d’une EDO.

N/A
N/A
Protected

Academic year: 2021

Partager "sur un intervalle et pour le calcul approché de la solution d’une EDO."

Copied!
8
0
0

Texte intégral

(1)

TP3 - M43 Analyse numérique

G…loŠrˆi€a‡ F€a€cca’n€o”nˆi‡

On s’intéresse dans ce TP aux diverses méthodes vues en cours et/ou en TD pour le calcul approché de l’intégration numérique d’une fonction réelle de variable réelle

sur un intervalle et pour le calcul approché de la solution d’une EDO.

1 Exercice

On rappelle que sinest un entier

1. la formule durectangleou dupoint milieuànsous-intervalles pour calculer l’intégrale d’une fonctionf sur l’intervalle [a,b] est

Zb

a

f(t)d t'Rn:=h

n1

X

i=0

f µ

a+2i+1

2 h

avech=ba n ;

x

a b

a+b 2

x x0 x1 x2 x3 x4

x0+x1 2

x1+x2 2

x2+x3 2

x3+x4 2

2. la formule destrapèzesànpoints pour calculer l’intégrale d’une fonctionf sur l’intervalle [a,b] est Z b

a

f(t)d t'Sn:=h Ã1

2f(a)+

n−1X

i=1

f(a+i h)+1 2f(b)

!

avech=ba n ;

x

a b x0 x1 x2 x3 x4 x5 x

3. la formule deCavalieri-Simpsonànpoints pour calculer l’intégrale d’une fonctionf sur l’intervalle [a,b] est Z b

a

f(t)d t'Sn:=h 6 Ã

f(a)+2

n−1X

r=1

f(a+r h)+4

n−1X

s=0

f µ

a+2s+1

2 h

¶ +f(b)

!

avech=ba n ; . On sait que sif est de classeC3([a,b]), on a

nlim→+∞

1 n

¯

¯

¯

¯ Z b

a

f(t)d t−In

¯

¯

¯

¯=0, pourIn=Rn,Tn,Sn.

1/8

(2)

L’observer numériquement pour les fonctions suivantes :

f:x7→sin(x) sur [0,π]; g:x7→ 1

1+x2 sur [0, 1];

h:x7→ex sur [−1, 1]; w:x7→xex sur [0, 2π];

avecn=1, 2, 4, 8, 16, 32, 64, 128.

2 Exercice

SoitI=[t0,t0+T] un intervalle fermé borné deR. On se donne une fonctionf définie et continue surI×Rà valeurs dans R. Pourη∈R, condition initiale, on cherche une fonctionydéfinie et dérivable surIà valeurs dansRtelle que

(y0(t)=f(t,y(t)),tI,

y(t0)=η. (1)

Il s’agit d’un problème de Cauchy avec une EDO du premier ordre. On sait que s’il existe un réelLtel que

tI,∀y,z∈R,|f(t,y)−f(t,z)| ≤L|yz|, (Condition de Lipschitz) alors le problème (1) admet une solution unique.

Rappelons ici trois schémas numériques : la méthode d’Euler explicite, la méthode de Heun (ou schéma de Runge-Kutta d’ordre 2) et la méthode de Runge-Kutta classique, d’ordre 4.

Soith=ti+1ti; alors on a Euler explicite

(y0=η,

yi+1=yi+h f(ti,yi), i=0, . . . ,n−1; (2)

Heun (

y0=η, yi+1=yi+h2¡

f(ti,yi)+f(ti+1,yi+h f(ti,yi))¢

, i=0, . . . ,n−1; (3)

RK4 (

y0=η, yi+1=yi+h6¡

f(ti,yi,1)+2f(ti+h/2,yi,2)+2f(ti+h/2,yi,3)+f(ti+1,yi,4

, i=0, . . . ,n−1; (4) avec

yi,1=yi yi,2=yi+h

2f(ti,yi,1) yi,3=yi+h

2f(ti+h/2,yi,2) yi,4=yi+h f(ti+h/2,yi,3) Programmer ces méthodes comme des fonctions informatiques.

Pour chaque problème de Cauchy ci-dessous calculer la solution exacte et les solutions approchées obtenues par les méthodes précédentes et afficher les courbes ainsi obtenues sur un même plan (par exemple avec gnuplot) :

(y0(t)= −y(t),

y(0)=1, (5)

(

y0(t)=y(t)+x

22 x+1 ,

y(0)=1, (6)

(y0(t)=cos2(y(t)),

y(0)=0, (7)

(y0(t)=x+y(t),

y(0)=1, (8)

(y0(t)=x y(t),

y(0)=1. (9)

Pour les calculs approchés on utilierah=0.1 etn=100.

(3)

3 Code

integration.c

1 /*

2 * F i l e : i n t e g r a t i o n . c

3 * A u t h o r : G l o r i a F a c c a n o n i

4 *

5 * C r e a t e d on 11 m a r s 2010 , 1 4 : 3 3

6 */

7

8 # i n c l u d e < s t d i o . h >

9 # i n c l u d e < s t d l i b . h >

10 # i n c l u d e < m a t h . h >

11

12 /*

13 * gcc - o i n t e g r a t i o n - lm i n t e g r a t i o n . c && ./ i n t e g r a t i o n

14 */

15 int m a i n ( int argc , c h a r** a r g v ) {

16

17 d o u b l e a , b ;

18 int n , exo ;

19

20 n = 30 ; // 1 2 8;

21 exo = 6 ;

22 23

24 // F o n c t i o n s p o u r les i n t e g r a t i o n s

25

26 d o u b l e r e c t a n g l e ( d o u b l e (* f )(d o u b l e) , d o u b l e a , d o u b l e b , int nb ) {

27 d o u b l e h , r e s u l t ;

28 int i ;

29 r e s u l t = 0 ;

30 h = ( b - a ) / nb ;

31 for ( i = 0 ; i < nb ; i ++ )

32 r e s u l t += f ( a + ( 2.0 * i + 1 ) / 2.0 * h ) ;

33 r e s u l t *= h ;

34 r e t u r n r e s u l t ;

35 }

36

37 d o u b l e t r a p e z e ( d o u b l e (* f )(d o u b l e) , d o u b l e a , d o u b l e b , int nb ) {

38 d o u b l e h , r e s u l t ;

39 int i ;

40 h = ( b - a ) / nb ;

41 r e s u l t = 0.0 ;

42 for ( i = 1 ; i < nb ; i ++ )

43 r e s u l t += f ( a + i * h ) ;

44 r e s u l t = ( f ( a ) + f ( b ) + r e s u l t * 2.0 ) * h / 2.0 ;

45 r e t u r n r e s u l t ;

46 }

47

48 d o u b l e S i m p s o n ( d o u b l e (* f )(d o u b l e) , d o u b l e a , d o u b l e b , int nb ) {

49 d o u b l e h , result , s , r ;

50 int i ;

51 h = ( b - a ) / nb ;

52 s = 0.0 ;

53 r = 0.0 ;

54 for ( i = 1 ; i < nb ; i ++ )

55 r += f ( a + i * h ) ;

56 for ( i = 0 ; i < nb ; i ++ )

57 s += f ( a + ( 2.0 * i + 1.0 ) * h / 2.0 ) ;

58 r e s u l t = ( f ( a ) + f ( b ) + 2.0 * r + 4.0 * s ) * h / 6.0 ;

59 r e t u r n r e s u l t ;

60 }

61 62

3/8

(4)

63 // c o m p a r a i s o n sur une f o n c t i o n d o n n e e

64

65 s w i t c h ( exo ) {

66 c a s e 1:

67 a = 0.0 ;

68 b = M _ P I ;

69 b r e a k ;

70 c a s e 2:

71 a = 0.0 ;

72 b = 1.0 ;

73 b r e a k ;

74 c a s e 3:

75 a = -1.0 ;

76 b = 1.0 ;

77 b r e a k ;

78 c a s e 4:

79 a = 0.0 ;

80 b = M _ P I + M _P I ;

81 b r e a k ;

82 c a s e 5:

83 a = 0.0 ;

84 b = 1.0 ;

85 b r e a k ;

86 c a s e 6:

87 a = 1.0 ;

88 b = 2.0 ;

89 b r e a k ;

90 d e f a u l t:

91 r e t u r n 0 ;

92 }

93

94 d o u b l e f ( d o u b l e x ) {

95 s w i t c h ( exo ) {

96 c a s e 1:

97 r e t u r n sin ( x ) ;

98 b r e a k ;

99 c a s e 2:

100 r e t u r n 1.0 / ( 1.0 + x * x ) ;

101 b r e a k ;

102 c a s e 3:

103 r e t u r n exp ( x ) ;

104 b r e a k ;

105 c a s e 4:

106 r e t u r n x * exp ( - x ) * cos ( 2.0 * x ) ;

107 b r e a k ;

108 c a s e 5:

109 r e t u r n x * x ;

110 b r e a k ;

111 c a s e 6:

112 r e t u r n 1.0 / x ;

113 b r e a k ;

114 d e f a u l t:

115 r e t u r n 0 ;

116 }// end s w i c h

117 }// end f

118

119 d o u b l e e x a c t e ( d o u b l e x ) {

120 s w i t c h ( exo ) {

121 c a s e 1:

122 r e t u r n - cos ( x ) ;

123 b r e a k ;

124 c a s e 2:

125 r e t u r n a t an ( x ) ;

126 b r e a k ;

127 c a s e 3:

128 r e t u r n exp ( x ) ;

(5)

129 b r e a k ;

130 c a s e 4:

131 r e t u r n ( - x / 0.5 e1 + 0.3 e1 / 0 . 2 5 e2 ) * exp ( - x ) * cos ( 0.2 e1 * x )

132 - ( -0.2 e1 / 0.5 e1 * x - 0.4 e1 / 0 .2 5 e2 ) * exp ( - x ) * sin ( 0.2 e1 * x ) ;

133 b r e a k ;

134 c a s e 5:

135 r e t u r n x * x * x / 3.0 ;

136 b r e a k ;

137 c a s e 6:

138 r e t u r n log ( x ) ;

139 b r e a k ;

140 d e f a u l t:

141 r e t u r n 0 ;

142 }// end s w i c h

143 }// end f

144 145

146 p r i n t f ( " \ n I n t e g r a t i o n ␣ e x a c t e ␣ e n t r e ␣ % lf ␣ et ␣ % lf ␣ : ", a , b ) ;

147 d o u b l e i n t e g r a l e = e x a c t e ( b ) - e x a c t e ( a ) ;

148 p r i n t f ( " \ n E X A C T E \ t \ t ␣ % 1 . 1 6 lf \ n \ n ", i n t e g r a l e ) ;

149

150 // A f f i c h e les s o l u t i o n s a p p r o c h e e s

151 p r i n t f ( " \ n I n t e g r a t i o n ␣ n u m e r i q u e ␣ a v e c ␣ % d ␣ p o i n t s ␣ : ", n ) ;

152 p r i n t f ( " \ n R E C T A N G L E \ t ␣ % 1 . 1 6 lf ", r e c t a n g l e ( f , a , b , n ) ) ;

153 p r i n t f ( " \ n T R A P E Z E \ t \ t ␣ % 1 . 1 6 lf ", t r a p e z e ( f , a , b , n ) ) ;

154 p r i n t f ( " \ n S I M P S O N \ t \ t ␣ % 1 . 1 6 lf \ n \ n ", S i m p s o n ( f , a , b , n ) ) ;

155

156 // A f f i c h e les e r r e u r s

157 p r i n t f ( " \ n E R R E U R S ␣ a v e c ␣ % d ␣ p o i n t s ␣ e n t r e ␣ % lf ␣ et ␣ % lf ␣ : ", n , a , b ) ;

158 p r i n t f ( " \ n R E C T A N G L E \ t ␣ % 1 . 1 6 lf ", f a b s ( i n t e g r a l e - r e c t a n g l e ( f , a , b , n ) ) ) ;

159 p r i n t f ( " \ n T R A P E Z E \ t \ t ␣ % 1 . 1 6 lf ", f a b s ( i n t e g r a l e - t r a p e z e ( f , a , b , n ) ) ) ;

160 p r i n t f ( " \ n S I M P S O N \ t \ t ␣ % 1 . 1 6 lf \ n \ n ", f a b s ( i n t e g r a l e - S i m p s o n ( f , a , b , n ) ) ) ;

161

162 r e t u r n ( E X I T _ S U C C E S S ) ;

163 }

5/8

(6)

edo.c

1 /*

2 * F i l e : edo . c

3 * A u t h o r : G l o r i a F a c c a n o n i

4 *

5 * C r e a t e d on 17 m a r s 2010 , 2 3 : 0 1

6 */

7 # i n c l u d e < s t d i o . h >

8 # i n c l u d e < s t d l i b . h >

9 # i n c l u d e < m a t h . h >

10

11 # d e f i n e NB 100

12 # d e f i n e N U M D E E E 5 // C h o i x du cas t e s t

13

14 d o u b l e * a l l o u e r _ t a b l e a u ( int n ) {

15 int i ;

16 d o u b l e * tab ;

17 tab = (d o u b l e *) m a l l o c ( n * s i z e o f (d o u b l e) ) ;

18 if ( tab == N U L L ) {

19 p r i n t f ( " A l l o c a t i o n ␣ i m p o s s i b l e ␣ ! " ) ;

20 e x i t ( 1 ) ;

21 }

22 r e t u r n tab ;

23 }

24

25 /*

26 * gcc - o edo - lm edo . c && ./ edo && g n u p l o t

27 * p l o t ’ edo . dat ’ u 1:2 w i t h line , ’ edo . dat ’ u 1:3 , ’ edo . dat ’ u 1:5 , ’ edo . dat ’ u 1:7

28 *

29 */

30 int m a i n ( int argc , c h a r** a r g v ) {

31

32 d o u b l e * x , * y_euler , * y_heun , * y_rk4 , h , y t a m ;

33 int i ;

34

35 F I L E * o u t p u t ; /* i n t e r n a l f i l e n a m e */

36 37

38 // D e c l a r a t i o n des c o n s t a n t e s de c a l c u l

39 h = 0.1 ; // pas p o u r le c a l c u l

40

41 // * A l l o c a t i o n de la m e m o i r e p o u r les t a b l e a u x de c a l c u l

42 x = a l l o u e r _ t a b l e a u ( NB + 2 ) ;

43 y _ e u l e r = a l l o u e r _ t a b l e a u ( NB + 2 ) ;

44 y _ h e u n = a l l o u e r _ t a b l e a u ( NB + 2 ) ;

45 y _ r k 4 = a l l o u e r _ t a b l e a u ( NB + 2 ) ;

46 47

48 // C . I .

49 s w i t c h ( N U M D E E E ) {

50 c a s e 1:

51 x [0] = 0 ;

52 y _ e u l e r [0] = 1 ;

53 b r e a k ;

54 c a s e 2:

55 x [0] = 0 ;

56 y _ e u l e r [0] = 1 ;

57 b r e a k ;

58 c a s e 3:

59 x [0] = 0 ;

60 y _ e u l e r [0] = 0 ;

61 b r e a k ;

62 c a s e 4:

63 x [0] = 0 ;

64 y _ e u l e r [0] = 1 ;

(7)

65 b r e a k ;

66 c a s e 5:

67 x [0] = 0 ;

68 y _ e u l e r [0] = 1 ;

69 b r e a k ;

70 }

71 y _ h e u n [0] = y _ e u l e r [0] ;

72 y _ r k 4 [0] = y _ e u l e r [0] ;

73

74 // T e r m e s o u r c e

75

76 d o u b l e f ( d o u b l e x , d o u b l e y ) {

77 s w i t c h ( N U M D E E E ) {

78 c a s e 1: r e t u r n - y ;

79 b r e a k ;

80 c a s e 2: r e t u r n ( y + x * x - 2.0 ) / ( x + 1.0 ) ;

81 b r e a k ;

82 c a s e 3: r e t u r n pow ( cos ( y ) , 2.0 ) ;

83 b r e a k ;

84 c a s e 4: r e t u r n x + y ;

85 b r e a k ;

86 c a s e 5: r e t u r n x * y ;

87 b r e a k ;

88 }

89 }

90

91 // S o l u t i o n e x a c t e

92

93 d o u b l e e x a c t e ( d o u b l e x ) {

94 s w i t c h ( N U M D E E E ) {

95 c a s e 1: r e t u r n exp ( - x ) ;

96 b r e a k ;

97 c a s e 2: r e t u r n ( x + 1.0 / ( x + 1 ) - 2.0 * log ( x + 1 ) ) * ( x + 1 ) ;

98 b r e a k ;

99 c a s e 3: r e t u r n a t a n ( x ) ;

100 b r e a k ;

101 c a s e 4: r e t u r n 2. * exp ( x ) - x - 1.0 ;

102 b r e a k ;

103 c a s e 5: r e t u r n exp ( x * x / 2 ) ;

104 b r e a k ;

105 }

106 }

107 108

109 // M e t h o d e d ’ E u l e r

110

111 d o u b l e e u l e r ( d o u b l e x [] , d o u b l e y [] , d o u b l e h ) {

112 int i ;

113 for ( i = 0 ; i <= NB ; i ++ ) {

114 x [ i + 1] = x [ i ] + h ;

115 y [ i + 1] = y [ i ] + h * f ( x [ i ] , y [ i ] ) ;

116 }

117 }

118 119

120 // M e t h o d e de H e u n

121

122 d o u b l e he u n ( d o u b l e x [] , d o u b l e y [] , d o u b l e h ) {

123 d o u b l e k1 , k2 ;

124 int i ;

125 for ( i = 0 ; i <= NB ; i ++ ) {

126 k1 = h * f ( x [ i ] , y [ i ] ) ;

127 k2 = h * f ( x [ i ] + h , y [ i ] + k1 ) ;

128 y [ i + 1] = y [ i ]+( k1 + k2 ) / 2.0 ;

129 }

130 }

7/8

(8)

131 132

133 // M e t h o d e de R u n g e K u t t a 4

134

135 d o u b l e r u n g e 4 ( d o u b l e x [] , d o u b l e y [] , d o u b l e h ) {

136 d o u b l e hh = h / 2.0 , k1 , k2 , k3 , k4 ;

137 int i ;

138 for ( i = 0 ; i <= NB ; i ++ ) {

139 k1 = h * f ( x [ i ] , y [ i ] ) ;

140 k2 = h * f ( x [ i ] + hh , y [ i ] + k1 / 2.0 ) ;

141 k3 = h * f ( x [ i ] + hh , y [ i ] + k2 / 2.0 ) ;

142 k4 = h * f ( x [ i ] + h , y [ i ] + k3 ) ;

143 y [ i + 1] = y [ i ]+( k1 + 2 * k2 + 2 * k3 + k4 ) / 6.0 ;

144 }

145 }

146 147

148 // C o e u r du p r o g r a m m e

149 e u l e r ( x , y_euler , h ) ;

150 h e u n ( x , y_heun , h ) ;

151 r u n g e 4 ( x , y_rk4 , h ) ;

152

153 // S a u v e g a r d e des r e s u l t a t s d a n s un f i c h i e r t e x t e p o u r t r a c e r par G n u P l o t

154 // Ce f i c h i e r se n o m m e edo . dat . Il est c r e e d a n s le r e p e r t o i r e c o u r a n t

155 // ( c e l u i d a n s l e q u e l est l a n c e le p r o g r a m m e )

156 // S ’ il n ’ e x i s t e pas , il est c r e e . S ’ il existe , son c o n t e n u est e c r a s e .

157 o u t p u t = f o p e n ( " edo . dat ", " w + " ) ;

158 f p r i n t f ( output ,

159 " \ n # ␣ x \ t \ t y _ e x a c t e \ t y _ e u l e r \ t \ t e r r e u r \ t \ t y _ h e u n \ t \ t e r r e u r \ t \ t y _ r u n g e \ t \ t e r r e u r \ n " ) ;

160 for ( i = 0 ; i <= NB ; i ++ ) {

161 y t a m = e x a c t e ( i * h ) ;

162 f p r i n t f ( output , " % lf \ t % lf \ t % lf \ t % lf \ t % lf \ t % lf \ t % lf \ t % lf \ n ",

163 x [ i ] , ytam ,

164 y _ e u l e r [ i ] , y t a m - y _ e u l e r [ i ] ,

165 y _ h e u n [ i ] , y t a m - y _ h e u n [ i ] ,

166 y _ r k 4 [ i ] , y t a m - y _ r k 4 [ i ] ) ;

167 }

168 f c l o s e ( o u t p u t ) ;

169

170 f r e e ( x ) ;

171 f r e e ( y _ e u l e r ) ;

172 f r e e ( y _ h e u n ) ;

173 f r e e ( y _ r k 4 ) ;

174

175 // o u t p u t = f o p e n (" p l o t . gnu " , " w + " ) ;

176 // f p r i n t f ( output ," p l o t ’ edo . dat ’ u 1:2 w i t h line , ’ edo . dat ’ u 1:3 , ’ edo . dat ’ u 1:5 , ’ edo . dat ’ u 1 : 7 " ) ;

177 // f c l o s e ( o u t p u t );

178 // s y s t e m (" g n u p l o t p l o t . gnu -") ;

179

180 // A F F I C H A G E G R A P H Q U E :

181 o u t p u t = p o p e n ( " g n u p l o t ", " w " ) ; // e x e c u t i o n de la c o m m a n d e g n u p l o t

182 f p r i n t f ( output , " ␣ p l o t ␣ \" edo . dat \" ␣ ␣ u ␣ 1:2 ␣ w i t h ␣ l i n e ␣ t i t l e ␣ ’ e x a c t ’ , ␣ ’ edo . dat ’ ␣ u ␣ 1:3 ␣ t i t l e ␣ ’ e u l e r ’ , ␣ ’ edo . dat ’ ␣ u ␣ 1:5 ␣ t i t l e ␣ ’ H e u n ’ , ␣ ’ edo . dat ’ ␣ u ␣ 1:7 ␣ t i t l e ␣ ’ RK4 ’\ n " ) ;

183 f f l u s h ( o u t p u t ) ;

184 s l e e p ( 10 ) ; // t e r m i n e r l ’ e n v o i de c o m m a n d e

185 p c l o s e ( o u t p u t ) ; // f e r m e r g n u p l o t

186

187 r e t u r n 0 ;

188 }

189 190

191 /*

192 ode := d i f f ( y ( x ) , x )=( y ( x ) + x * x - 2.0 ) / ( x + 1.0 );

193 d s o l v e ({% , y ( 0 ) = 1 } ) ;

194 C o d e G e n e r a t i o n [ C ]( rhs ( % ) ) ;

195 */

Références

Documents relatifs

Toute méthode d'approximation doit satisfaire à deux conditions essentielles 5 il faut d'abord qu'elle fasse con- naître le degré de l'approximation de chacune des valeurs

⇥ renvoie en sortie un paramètre T contenant l’aire obtenue par la méthode des rectangles appliquée à la fonction f sur l’intervalle [a,b] avec une subdivision de taille n+1..

Alors que, dans le cas général, on se « contente » de … l’inégalité de Cauchy-Schwarz … On en déduira donc l’existence d’une certaine dépendance linéaire. On va préciser

Nous supposerons que dans l’intervalle où l’on a enfermé la racine, la fonction y = f(X) possède.. une fonction inverse et admet des dérivées jusqu’au ~‘~

conditions de régularité des coefficients, la somme de la série est asymptotiquement équivalente à la somme d'un groupe de termes entourant le terme maximum, le nombre des termes de

En raison de sa vitesse de convergence et de sa simplicité, il nous paraît préférable d'utiliser le schéma MCRK (§ 4.3) pour la mise en oeuvre de la méthode décrite au paragraphe

Toute utilisation commerciale ou impression systématique est constitutive d’une infraction pénale. Toute copie ou impression de ce fichier doit contenir la présente men- tion

L’accès aux archives de la revue « Nouvelles annales de mathématiques » implique l’accord avec les conditions générales d’utilisation ( http://www.numdam.org/conditions )..