l
-
Méthode
Gauss Seidel
- # -*- coding: cp1252 -*-
-
- # Ce programme résout un système d'équations Ax=b, par la méthode de Gauss-Seidel
- # En cas de non convergence jouer sur w, ou sur tol ou sur itmax
-
- from math import*
-
- print "Résolution du système d'équations par la méthode Gauss
-Seidel"
-
- A=[[1,2,1],[8,-4,3],[3,2,-2]]
- # A est une matrice du système définie ligne par
ligne
- b=[3,4,-2]
- x=[0,0, 0]
- # x est un vecteur qui definie
les valeurs initiales de de x1 et x2(remarque xi est
réel
- tol=1e-16
- # tol définie la précision
seuil
- err=0
- itmax=300000
- # itmax est le nombre
d'itération maximal
- s=0
- w=0.1
- # 0 <w <1 , est un facteur
de relaxation qui modifie un peu l'algo, c'est pour
amener
- # le système à converger.En
effet il se peut que le système ne converge pas pour certaine matrice
- for k in range (itmax):
- err=0
- for i in
range(len(A)):
- s=0
- xprim=x[i]
- for j in range(len(A)):
- if i!=j:
- s=s+A[i][j]*x[j]
- x[i]=w*((b[i]-s)/A[i][i])+(1-w)*x[i]
- err=err+fabs((x[i]-xprim))
- if err<tol:
- print('Nombre d\' iterations',k,'Erreur',float(err))
- print " la solution de votre système est:",x
- break
- else:
- print "le sytème ne converge pas.
Essayer de jouer sur w, ou sur tol ou sur itmax ou sur x"
- print "voir en premier tol(penser à augmenter la
puissance si trop petit), puis itmax, puis w (ne pas
depasser 0.5)"
-
Méthode de Gauss Jordan
- Matrice inverse
· #include <iostream>
· #include <cmath>
· using namespace std;
· · void identite(double mat[],int dim);
· void copie_matriceR(double
mat1[],double mat2[],int dim);
· void gaussR(double mat1[],double mat2[],int dim);
· · int main()
· {
·
//exemple d'utilisation pour une matrice 2x2
· int dim=2;
· double *M=new
double[dim*dim];
· double *M_inv=new double[dim*dim];
· //remplissage
de la matrice M
· M[0+0*dim]=2.;
· M[0+1*dim]=4.8;
· M[1+0*dim]=6.2;
· M[1+1*dim]=5.1;
· //inversion
de la matrice M
· gaussR(M,M_inv,dim);
· //affichage
des elements de matrice inverse
· for(int i=0;i<dim;i++)
· {
· for(int j=0;j<dim;j++)
· {
· cout<<M_inv[i+j*dim]<<"
"<<;
· }
· cout<<endl;
· }
· · · }
· · //fonction
qui rempli une matrice
· //par la
matrice identité
· void identite(double mat[],int dim)
· {
· for(int i=0;i<dim;i++)
· {
· for(int j=0;j<dim;j++)
· {
· if(i==j)
· {
· mat[i+j*dim]=1.;
· }
· else
· {
· real(mat[i+j*dim])=0.;
· imag(mat[i+j*dim])=0.;
· }
· }
· }
· · //fonction
qui copie une matrice dans une autre
· void copie_matriceR(double
mat1[],double mat2[],int dim)
· {
· for(int i=0;i<dim;i++)
· {
· for(int j=0;j<dim;j++)
· {
· mat2[i+j*dim]=mat1[i+j*dim];
· }
· }
· }
· · //la matrice
mat1 est celle qui est à inverser
· //et
l'inverse est contenu dans mat2
· void gaussR(double mat1[],double mat2[],int dim)
· {
· double *temp=new double[dim*dim];
· copie_matriceR(mat1,temp,dim);
· double
a,b;
· a=0.;
· b=0.;
· int c=0;
· identite(mat2,dim);
· for(int k=0;k<dim;k++)
· {
· a=temp[k+k*dim];
· //verifie la condition "a" different
de zero
· //sinon on
copie une ligne pour resoudre le probleme
· c=0;
· while(abs(a)<0.000000001)
· {
· c++;
· for(int q=0;q<dim;q++)
· {
· temp[k+q*dim]=temp[k+q*dim]+temp[k+c+q*dim];
· mat2[k+q*dim]=mat2[k+q*dim]+mat2[k+c+q*dim];
· }
· a=temp[k+k*dim];
· }
· //normalisation la ligne k
· for(int l=0;l<dim;l++)
· {
· temp[k+l*dim]=temp[k+l*dim]/a;
· mat2[k+l*dim]=mat2[k+l*dim]/a;
· }
· //reduction
de gauss-jordan
· for(int i=0;i<dim;i++)
· {
· b=temp[i+k*dim];
· if(i!=k)
· {
· for(int j=0;j<dim;j++)
· {
· temp[i+j*dim]=temp[i+j*dim]-b*temp[k+j*dim];
· mat2[i+j*dim]=mat2[i+j*dim]-b*mat2[k+j*dim];
· }
· }
· }
· }
· delete(temp);
· }
-
Méthode
de Gauss-Jordan
- #include <stdio.h>
- #include <conio.h>
- #include <math.h>
- #include <string.h>
- #include <stdlib.h>
- #include <iostream.h>
- #include <process.h>
- #include <stdlib.h>
-
-
void
saisie_mat(float m[100][100],int,int);
-
void affich_mat(float
m[100][100],int
l,int c);
-
void gauss_jordan(float
m1[100][100],float m2[100][100],int
l,int c);
-
void affich_systeme(float
m1[100][100],float m2[100][100],int
l,int c);
-
void copie_mat(float
m[100][100],float m1[100][100],int
l,int c);
-
void
identite_mat(float m[100][100],int l);
-
-
-
-
void
main()
- {
-
int a,b;
-
int e,f;
-
float
mat[100][100],mat1[100][100];
-
- system
("cls");
- system
("color
3f");
-
-
printf(" ===> Saisissez votre
matrice carree A : \n\n");
-
printf("Combien de lignes :
");
-
scanf("%d",&a);
-
-
printf("Combien de colonnes :
");
-
scanf("%d",&b);
-
-
saisie_mat(mat,a,b);
-
-
printf("\n\n");
-
-
printf(" ===> Saisissez votre
matrice B : \n\n");
-
printf("Combien de lignes :
");
-
scanf("%d",&e);
-
printf("Comien de
colonnes : ");
-
scanf("%d",&f);
-
-
saisie_mat(mat1,e,f);
-
-
printf("\n\n");
-
-
affich_systeme(mat,mat1,a,b);
-
-
printf("\n\n");
-
-
gauss_jordan(mat,mat1,a,b);
-
-
printf("\n\n");
-
-
affich_systeme(mat,mat1,a,b);
-
-
-
- }
-
-
-
-
-
void
saisie_mat(float m[100][100],int l,int c)
-
{ int i,j;
-
-
for(i=0;i<l;i++)
- {
-
for(j=0;j<c;j++)
- {
-
printf(" A[%d][%d] :
",i+1,j+1);
-
scanf("%f",&m[i][j]);
- }
-
printf("\n");
- }
- }
-
-
-
-
void affich_systeme(float
m1[100][100],float m2[100][100],int l,
int c)
- {
-
int i,j;
-
printf(" ===>Affichage du
systeme lineaire :
\n\n\n");
-
-
for(i=0;i<l;i++)
- {
-
printf(" (");
-
for(j=0;j<c;j++)
- {
-
printf(" %.2f
",m1[i][j]);
- }
-
printf(" ) (X%d) =",i+1);
-
printf("\t%.2f",m2[i][0]);
-
printf("\n\n");
- }
- }
-
-
-
void gauss_jordan(float
m1[100][100],float m2[100][100],int
l,int c)
- {
-
int i;
-
int j;
-
float
tmp,tmp2;
-
float
m3[100][100];
-
float
m4[100][100];
-
float
m5[100][100];
-
float
m6[100][100];
-
int k=0;
-
identite_mat(m4,l);
-
affich_mat(m4,l,l);
-
-
for(i=0;i<l;i++)
- {
-
copie_mat(m1,m3,l,c);
-
copie_mat(m2,m5,l,1);
-
copie_mat(m4,m6,l,l);
-
tmp=m1[i][i];
-
for(j=0;j<c;j++)
- {
-
m1[i][j]=m1[i][j]/tmp;
-
m2[i][j]=m2[i][j]/tmp;
-
m4[i][j]=m4[i][j]/tmp;
- }
-
-
for(k=0;k<l;k++)
- {
-
if(k!=i)
- {
- tmp2=m1[k][i]/tmp;
-
for(j=0;j<c;j++)
- {
-
m1[k][j]=m1[k][j]-tmp2*m3[i][j];
-
m4[k][j]=m4[k][j]-tmp2*m4[i][j];
-
m2[k][j]=m2[k][j]-tmp2*m5[i][j];
- }
- }
-
- }
-
affich_systeme(m1,m2,l,c);
-
printf("\n\n\n");
-
printf(" ===> Matrice Inverse
:");
-
affich_mat(m4,l,c);
- }
- }
-
-
-
-
-
-
-
-
void affich_mat(float
m[100][100],int
l,int c)
- {
-
int i,j;
-
printf(" Affichage de la matrice
\n\n\n");
-
for(i=0;i<l;i++)
- {
-
for(j=0;j<c;j++)
- {
-
printf("\t%.2f",m[i][j]);
- }
-
printf("\n\n");
- }
- }
-
-
-
void copie_mat(float
m[100][100],float m1[100][100],int
l,int c)
- {
-
int i,j;
-
for(i=0;i<l;i++)
- {
-
for(j=0;j<c;j++)
- {
-
m1[i][j]=m[i][j];
- }
- }
- }
-
-
-
-
void
identite_mat(float m[100][100],int l)
- {
-
int i,j;
-
for(i=0;i<l;i++)
- {
-
for(j=0;j<l;j++)
- {
- m[i][j]=0;
- }
- m[i][i]=1;
- }
- }
-