0% found this document useful (0 votes)
115 views11 pages

Correction TP-Maths Appliquées GI-S3 Programmation Sous MATLAB

This document contains solutions to exercises in a MATLAB programming tutorial. It includes solutions to exercises on: 1) Numerically solving linear systems using MATLAB's backslash operator and Gaussian elimination. 2) Approximating eigenvalues and eigenvectors numerically using MATLAB's eig function and the power method. 3) Polynomial interpolation using Lagrange interpolation. 4) Numerical integration using the midpoint rule and trapezoid rule. Graphs of errors are shown as the number of intervals increases.

Uploaded by

Emene Ek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
115 views11 pages

Correction TP-Maths Appliquées GI-S3 Programmation Sous MATLAB

This document contains solutions to exercises in a MATLAB programming tutorial. It includes solutions to exercises on: 1) Numerically solving linear systems using MATLAB's backslash operator and Gaussian elimination. 2) Approximating eigenvalues and eigenvectors numerically using MATLAB's eig function and the power method. 3) Polynomial interpolation using Lagrange interpolation. 4) Numerical integration using the midpoint rule and trapezoid rule. Graphs of errors are shown as the number of intervals increases.

Uploaded by

Emene Ek
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

CORRECTION

TP-Maths appliquées GI-S3


Programmation sous MATLAB

Préparé & présenté par :

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

A = [3 2 1;1 3 2;2 4 6]; b = [1;2;3];


n = size(A);
U = A; v = b;
for j = 1:n-1
for i = j+1:n
v(i) = v(i)-(U(i,j)/U(j,j))*v(j);
for k = j+1:n
U(i,k) = U(i,k) - (U(i,j)/U(j,j))*U(j,k);
end
U(i,j) = 0;
end
end

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);

while err >eps & niter<=nmax


y = x/norm(x);
x = B*y;
y = x/norm(x);
lambdanew = y'*x;
err = abs(lambda - lambdanew);
lambda = lambdanew;
niter = niter + 1;
end

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 ‘)

You might also like