Correction TP-Maths Appliquées GI-S3 Programmation Sous MATLAB
Correction TP-Maths Appliquées GI-S3 Programmation Sous MATLAB
IMANE DZAZI
IMANE AGUELIL
TP 1 : Résolution numérique des systèmes linéaires
TP1 EXERCICE 1
Solution :
Script.m
A[0 1 -3 ; 2 1 -1 ;4 5 -2] ;
B[-5 ; 7 ; 10] ;
X=A\B ;
X
EXERCICE 2
Solution
clear;clc
y(3) = v(3)/U(3,3);
y(2) = (v(2)-U(2,3)*y(3))/U(2,2);
y(1) = (v(1)-U(1,2)*y(2)-U(1,3)*y(3))/U(1,1);
y
TP 2 : Approximation numérique des éléments propres
(Valeurs et vecteurs)
EXERCICE 1
Solution
>>[VA DA] =eig(A)
VA valeurs propres
DA vecteurs propres
EXERCICE 2
Solution
clear;clc
B = [7 -2 0;-2 6 -2; 0 -2 5];
x0 = [1;0;0]; eps = 10^(-12); nmax = 100;
lambda = 0;
niter = 0;
err = eps+1;
x = x0/norm(x0);
lambda
x
niter
Il va afficher
lambda =8.1663
x=6.7223
= -39202
=2.4762
niter =60
TP 3 : Interpolation polynomiale de Lagrange
EXERCICE 1 :
Solution :
Qst 1 :
…….. ????
Qst 2 :
Function [P] = lagraneInter (x,y)
N=length(x) ;
B=Ones(n,m) ;
For i =1 : n
For j=1 : n-1
B(i,j) = x(i)^(n-j) ;
End
End
P=B/y’ ;
End
Qst 3 :
Clear ; clc
X=[-3 -2 -1 0 1 2 3 ] ;
Y = [-1 1 3 7 2 0 -2] ;
[Pcoeff] = lagraneInter(x,y) ;
Df = -4 : 0.2 :4 ;
PDF = polyval(Pcoeff,Df) ;
Plot(Df ,PDF ,’r-o’) ;
EXERCICE 2 :
Solution :
Clear ; clc
P=[1 0 1 ]
DG = -10 : 0.2 :10 ;
PDG = Polyval(P,DG) ;
Plot(DG , PDG ,’r-o’) ;
>>f = @(x) 1+x.^2
>>f(2)
Ans = 5
TP 4 : Intégration numérique, Méthodes du point milieu et du
trapèze
Exercice 1 :
Solution :
Qst 1 :
Clear ; clc
Format short
A=0 ;
B=2*pi ;
N=80 ;
intPTM = 0 ;
f=@(x) x.exp(-x).*cos(2.*x) ;
dx=(b-a)/n ;
x=a :dx :b ;
for i =1 : n
xbar =(x(i+1)+x(i))/2 ;
intPTM =intPTM + dx *f(xbar) ;
end
intPTM
la Sortie sera
intPTM = -0.1219
Qst 2 :
Clear ; clc
Format short
a=0 ;
b=2*pi ;
n=80 ;
intPTM = 0 ;
f=@(x) x.exp(-x).*cos(2.*x) ;
dx=(b-a)/n ;
x=a :dx :b ;
for i =1 : n
xbar =(x(i+1)+x(i))/2 ;
intPTM =intPTM + dx *f(xbar) ;
end
intExact = -0.122122604618968 ;
errPTM = abs(intExact – intPTM)
la Sortie sera
errPTM = 2.599777
Qst 3 :
Clear ; clc
Format short
na=10 ;
nb=1000 ;
a=0 ;
b=2*pi ;
intExact=-12.183319110226909
b=2*pi ;
intPTM = 0 ;
f=@(x) x.exp(-x).*cos(2.*x) ;
errPTML =[] ;
for ni =na : nb
dx=(b-a)/ni ;
x=a :dx :b ;
for i=1 : ni
xbar =(x(i+1)+x(i))/2 ;
intPTM =intPTM + dx *f(xbar) ;
end
errPTM = abs(intExact – intPTM)
errPTML = [errPTML errPTM] ;
intPTM=0 ;
end
x=na :10 :nb
plot(x,errPTML)
Exercice 2 :
Solution :
Clear ; clc
Format short
Xbar = x(nj) ;
intTrpz = intTrpz + dx*fun(xbar)
end
intTrpz = inti + intTrpz ;
errTr=abs(intExact – intTrpz) ;
errTrpz = [ errTrpz errTr] ;
intTrpz=0 ;
end
X=Na : 10 : Nb ;
y=Na : 10 : Nb ;
figure (3) ;
plot(x,errPTM,’r ‘,y,errTrpz,’b’)
legend (‘Point Milieu’,’TrapA’)
xLabel (‘nbr de Point ‘)
ylabel(‘Erreur’)
title (‘Erreur par la méthode des Trapèzes ‘)