You are on page 1of 7

// Analyse Numrique et Optimisation

// M. Bnallal
// http://benallal.free.fr
//
//Version 1999/2000 - Cours Analyse numrique et Optimisation - cole des Mines de D
ouai
//Version 2002/2003 - Cours Analyse numrique et Optimisation - Universit de Montral
//
//
//
//Implmentation Scilab de la mthode de Jacobi
// Rsolution de systme du type A.X=B
// par la mthode itrative de JACOBI
// Initialisation
A=[-16 6 -2 -5;
3 10 -5 1;
-4 1 18 2;
1 2 2 -14];
B=[-19;
1;
12;
1];
X0=[0.1;
0.1;
0.1;
0.1];
Xk=X0;
iter=0;
max_it=500;
tol = 0.0000000000001;
// Conditionnement des matrices D, L et U
n=4;
for i=1:n,
for j=1:n,
I(i,i)=1;
D(i,i)=A(i,i);
if i>j then, L(i,j)=-A(i,j);,
else L(i,j)=0;, end,
if i<j then, U(i,j)=-A(i,j);,
else U(i,j)=0;, end ;
end ; end;
// premire itration
// JACOBI
Xkplus1 = ( inv(D)*(L+U) * Xk + inv(D)* B);
// itrations suivantes
while norm(Xkplus1 - Xk)/norm(Xk) > tol,
iter=iter+1;
Xk = Xkplus1;
// JACOBI
Xkplus1 = ( inv(D)*(L+U) * Xk + inv(D)* B);
end;
// Solution obtenue
iter
Xkplus1
//
//

//Implmentation Scilab de la mthode de Gauss-Seidel


// Rsolution de systme du type A.X=B
// par la mthode itrative de GAUSS-SEIDEL
// Initialisation
A=[-16 6 -2 -5;
3 10 -5 1;
-4 1 18 2;
1 2 2 -14];
B=[-19;
1;
12;
1];
X0=[0.1;
0.1;
0.1;
0.1];
Xk=X0;
iter=0;
max_it=500;
tol = 0.0000000000001;
// Conditionnement des matrices D, L et U
n=4;
for i=1:n,
for j=1:n,
I(i,i)=1;
D(i,i)=A(i,i);
if i>j then, L(i,j)=-A(i,j);,
else L(i,j)=0;, end,
if i<j then, U(i,j)=-A(i,j);,
else U(i,j)=0;, end ;
end ; end;
// premire itration
// GAUSS-SEIDEL
Xkplus1 = ( inv(D-L)*U * Xk + inv(D-L)* B)
// itrations suivantes
while norm(Xkplus1 - Xk)/norm(Xk) > tol,
iter=iter+1;
Xk = Xkplus1;
// GAUSS-SEIDEL
Xkplus1 = ( inv(D-L) * U * Xk + inv(D-L)* B);
end;
// Solution obtenue
iter
Xkplus1
//
//
//Implmentation Scilab de la mthode de relaxation
// Rsolution de systme du type A.X=B
// par la mthode itrative de relaxation
// Initialisation
A=[-16 6 -2 -5;
3 10 -5 1;
-4 1 18 2;
1 2 2 -14];
B=[-19;

1;
12;
1];
X0=[0.1;
0.1;
0.1;
0.1];
Xk=X0;
iter=0;
max_it=500;
tol = 0.0000000000001;
// Conditionnement des matrices D, L et U
n=4;
for i=1:n,
for j=1:n,
I(i,i)=1;
D(i,i)=A(i,i);
if i>j then, L(i,j)=-A(i,j);,
else L(i,j)=0;, end,
if i<j then, U(i,j)=-A(i,j);,
else U(i,j)=0;, end ;
end ; end;
//
//
//
//
//
//

THEOREME
Pour toute matrice A, une condition
de convergence
est que 0 < w < 2.
premire itration
RELAXATION
w=1.1
Xkplus1 = ( inv(D-w*L) * ((1-w) * D
// itrations suivantes
while norm(Xkplus1 - Xk)/norm(Xk) >
iter=iter+1;
Xk = Xkplus1;
// RELAXATION
Xkplus1 = ( inv(D-w*L) * ((1-w) * D
end;
// Solution obtenue
iter
Xkplus1

necessaire

+ w*U) * Xk + inv(D-w*L)* w*B);


tol,

+ w*U) * Xk + inv(D-w*L)* w*B);

//Implmentation Scilab de la mthode de la dichotomie 1


// Recherche d'une racine d'un polynme
// du type a.x^5 + b.x^4 + c.x^3 + d.x^2 + e.x + f = 0
// par la mthode dichotomique 1
// D'abord lancer (getf('C:\scliab\fonction.sci'))
// le fichier fonction.sci contiendra la fonction
// polynomiale suivante :
// function [y]=f(x)
// y =x^5 - 2*x^4 + 2*x^3 - 26*x^2 + 19*x + 22
// Initialisation
tol = 0.001;
iter= 0;
max_iter=500;
s = 0.0000000001;
// Borne [1,2]
xa = 1; xb = 2; eps = 0.00000001;

ya=f(xa);
// itrations suivantes
while abs(xb - xa) > 2 * eps,
iter=iter+1;
xc = 0.5 * (xa + xb);
yc = f(xc);
if (yc*ya) < 0 then, xb = xc;,
else ya = yc; xa = xc;, end,
end;
// Solution obtenue
iter
xc

//Implmentation Scilab de la mthode de la dichotomie 2


// Recherche d'une racine d'un polynme
// du type a.x^5 + b.x^4 + c.x^3 + d.x^2 + e.x + f = 0
// par la mthode dichotomique 2
// d'abord lancer (getf('C:\scliab\fonction.sci'))
// pour la fonction polynomile suivante
// function [y]=f(x)
// y =x^5 - 2*x^4 + 2*x^3 - 26*x^2 + 19*x + 22
// Initialisation
tol = 0.001;
iter= 0;
max_iter=50;
s = 0.0000000001;
// Borne [1,2]
xa = 1; xb = 2; eps = 0.00000001;
ya=f(xa);
// itrations suivantes
while abs(xb - xa) > 2 * eps,
iter=iter+1;
xc = 0.5 * (xa + xb)- s;
xd = xc + 2 * s;
yc = f(xc);
yd = f(xd);
if yc <= yd then, xb = xd;,
else xa = xc;, end,
end;
// Solution obtenue
iter
x
//Implmentation Scilab du nombre d or
// Recherche d'une racine d'un polynme
// du type a.x^5 + b.x^4 + c.x^3 + d.x^2 + e.x + f = 0
// par la mthode dichotomique
// d'abord lancer (getf('C:\scliab\fonction.sci'))
// pour la fonction polynomile suivante
// function [y]=f(x)
// y =x^5 - 2*x^4 + 2*x^3 - 26*x^2 + 19*x + 22
// Initialisation
tol = 0.001;
iter= 0;

max_iter=50;
s = 0.0000000001;
nb_dor = 0.5*(sqrt(5) - 1)
// Borne [1,2]
xa = 1; xb = 2; eps = 0.00000001;
xc = xa + (1-nb_dor)*(xb-xa);
xd = xa + nb_dor*(xb-xa);
// itrations suivantes
while abs(xb - xa) > eps,
iter=iter+1;
yc = f(xc);
yd = f(xd);
if yc > yd then, xa = xc;
xc = xd;
xd = xa + nb_dor*(xb-xa);,
else xb = xd;
xd = xc;
xc = xa + (1-nb_dor)*(xb-xa);, end,
end;
// Solution obtenue
iter
x
//IImplmentation Scilab de la mthode de Newton
// Recherche d'une solution d'un systme
// d'quation non linaire
// par la mthode de Newton
// d'abord lancer (getf('C:\scliab\fonction.sci'))
// pour la fonction suivante
//function [y1,y2,y3] = f(x)
//y1 = 4*(x(1)^2 + x(2)^2) - 2*x(1)*x(2) - 6*(x(1) + x(2));
//y2 = x(1)^3 + 2*x(2)^2 - 1
//y3 = 5*x(2)^3 + x(1)^2 - 2*x(1)*x(2) - 4
//function [dy1_x1,dy1_x2,dy2_x1,dy2_x2,dy3_x1,dy3_x2] =df(x)
//dy1_x1 = 8*x(1) - 2*x(2) -6;
//dy1_x2 = 8*x(2) - 2*x(1) -6;
//dy2_x1 = 3*x(1)^2;
//dy2_x2 = 4*x(2);
//dy3_x1 = 2*x(1) - 2*x(2);
//dy3_x2 = 15*x(2)^2 - 2*x(1);
// Initialisation
iter= 0; max_iter=500; tol = 0.0000000001;
// vecteur initiale
Xk = [0.1 ; 0.1];
// premire itration
[y1,y2,y3] = f(Xk);
y =[y1;y2;y3]
[dy1_x1,dy1_x2,dy2_x1,dy2_x2,dy3_x1,dy3_x2]= df(Xk);
J = [dy1_x1,dy1_x2;
dy2_x1,dy2_x2;
dy3_x1,dy3_x2]
// resolution du systme J(x).delta(x) = -R(x)
deltax = - J \ y;
Xkplus1 = Xk + deltax;
// itrations suivantes
while norm(Xkplus1 - Xk)/norm(Xk) > tol,
iter=iter+1;
Xk = Xkplus1;

[y1,y2,y3] = f(Xk);
y =[y1;y2;y3];
// Calcul de la jacobienne
[dy1_x1,dy1_x2,dy2_x1,dy2_x2,dy3_x1,dy3_x2]= df(Xk);
J = [dy1_x1,dy1_x2;
dy2_x1,dy2_x2;
dy3_x1,dy3_x2];
// resolution du systme J(x).delta(x) = -R(x)
deltax = - J \ y;
Xkplus1 = Xk + deltax;
end;
// Solution obtenue
iter
Xkplus1

//Implmentation Scilab du Gradient


// Recherche d'une solution d'un systme
// d'quation non linaire
// par la mthode du Gradient
// d'abord lancer (getf('C:\scliab\fonction.sci'))
// pour la fonction suivante
//function [y] = f(x)
//y = 4*(x(1)^2 + x(2)^2) - 2*x(1)*x(2) - 6*(x(1) + x(2));
//function [dy_x1,dy_x2] = Gradf(x)
//dy_x1 = 8*x(1) - 2*x(2) -6;
//dy_x2 = 8*x(2) - 2*x(1) -6;
// Minimisation de f(X + t.gradf(X))
//f(X+t.gradf(X)) = 4(x1+t.dy_x1)^2 + 4(x2+t.dy_x2)^2
// -2(x1+t.dy_x1)(x2+t.dy_x2)
// -6(x1+t.dy_x1+x2+t.dy_x2)
//
// df/dt = 8(x1+t.dy_x1)+8(x2+t.dy_x2)dy_x2
// -2(x1+t.dy_x1)dy_x2-2(x2+t.dy_x2)dy_x1
// -6(dy_x1+dy_x2) =0
//
// 6(dy_x1+dy_x2)+2(x1.dy_x2+x2.dy_x1)-8(x1*dy_x1+x2*dy_x2)
//t= -------------------------------------------------------// 8(dy_x1^2+dy_x2^2) - 4.dy_x1.dy_x2
//
// Initialisation
iter= 0; max_iter=500; tol = 0.0000000001;
// drive seconde
d2f = [8, -2; -2 8 ];
// vecteur initiale
Xk = [3 ; 2];
// premire itration
[y] = f(Xk)
[dy_x1,dy_x2]= Gradf(Xk);Gf = -[dy_x1;dy_x2]
// Determination de t par minimisation de f(x + t Gradf(x))
x1 = Xk(1); x2 = Xk(2);
t = 6*(dy_x1+dy_x2)+2*(x1*dy_x2 + x2*dy_x1)-8*(x1*dy_x1 + x2*dy_x2);
t = t / ( 8*(dy_x1^2+dy_x2^2)-4*dy_x1*dy_x2)
// Estimation de Xk
Xkplus1 = Xk - t * Gf
// itrations suivantes

while norm(Xkplus1 - Xk)/norm(Xk) > tol,


iter=iter+1;
Xk = Xkplus1;
[y] = f(Xk);
[dy_x1,dy_x2]= Gradf(Xk);Gf = -[dy_x1;dy_x2];
x1 = Xk(1); x2 = Xk(2);
t = 6*(dy_x1+dy_x2)+2*(x1*dy_x2 + x2*dy_x1)-8*(x1*dy_x1 + x2*dy_x2);
t = t / ( 8*(dy_x1^2+dy_x2^2)-4*dy_x1*dy_x2);
// Estimation de Xk
Xkplus1 = Xk - t * Gf;
end;
// Solution obtenue
iter
Xkplus1
_____________________
function [x,i]=gausseidel(A,b,tol,N,x0)
n= size(b) ;
n=max(n);
x=x0 ; //Initialistations
for k=1:N //Itrations
for i=1:n
y(i)=x(i) ; //Stockage de x(i)(k-1) pour le test de sortie
for j=1:i-1 //Calcul de chaque terme de x pour une itration donne
somme1=A(i,j)*x(j) ;
end
for j=i+1:n
somme2=A(i,j)*x(j) ;
end
x(i)=1/A(i,i)*((-1)*somme1+(-1)*somme2+b(i)) ;
end
if (norm(x-y)/norm(y)<=tol) //Test si le vecteur trouv est bien dans l'intervalle
tolrence
return x ;
end
end
disp('Erreur, la srie n''a pas converg en N itrations') ;
// Si au bout de N itration on n'a rien retourn, message d'erreur
endfunction

You might also like