IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C Discussion :

probleme methode de gauss resolution d un systeme Mx=b


Sujet :

C

  1. #1
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    52
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2006
    Messages : 52
    Points : 32
    Points
    32
    Par défaut probleme methode de gauss resolution d un systeme Mx=b
    bonjour, peut etre ici t a t il un "specialiste" en calcul numerique en C
    je cherche a resoudre Mx=b par l algorithme de gauss ; le programme s execute bien mais la solution est fausse... ou se trouve mon/mes erreurs svp ? merci

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
     
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <math.h>
    #include "main.h"   //contient les declarations des fonctions de ce fichier main.c
     
    const long n=4,MAX=100,MIN=0;
    int main(int argc, char *argv[])
    {
     
        double vect[n],mat[n][n],matb[n][n];
        double x[n],b[n];
        long c;
        srand(time(NULL));
     
    	printf("calcul sur les vecteurs et matrices\n");
     
        printf("calcul aleatoire du vecteur et de la matrice\n");
     
        aleatvect(vect,n);      //creation d un tableau de nb entiers aleatoires
        affichevect(vect,n);    //affichage du tableau
        copievect(vect,x,n);    //copie de ce tableau vect dans le tableau x
        affichevect(x,n);       //affichage du tableau x
     
        aleatmat(mat,n);        //creation d une matrice 4*4 de nb aleatoires
        affichemat(mat,n);      //affichage de la matrice
        copiemat(mat,matb,n);   //copie de la matrice mat dans la matrice matb
        affichemat(matb,n);     //affichage de matb
     
       // printf("calcul de la norme du vecteur\t\n");
       // printf("%lf\n",normvect(vect,n));
     
        printf("calcul du produit Matrice.Vecteur=Vecteurb\n");
        MX(mat,vect,b,n);   //calcul du vecteur b=mat*vect
        affichevect(b,n);   //affichage de b
     
        printf("resolution de M.x=b\n");
        resolution(mat,x,b,n);  //resolution de mat*x=b avec x inconnue
        affichevect(x,n); //normalement le vecteur x doit redonner le x initial
     
        printf("verification : \n");
        MX(matb,x,b,n);         //calcul de matb*x qui doit redonner b
        affichevect(b,n);       //le resultat est mis dans b qu'on affiche
     
        scanf("%ld",&c);
    	return 0;
    }
     
     
    void aleatvect(double *vect, long n)  //cree un vecteur de nb entiers aleatoires
    {
        long i;
        for(i=0;i<n;i++)
        {
            vect[i]=rand()%(MAX-MIN+1)+MIN;
        }
    }
     
    void affichevect(double *vect,long n) //affiche les composantes du vecteur
    {
        long i;
        printf("affichage du vecteur :\n");
        for(i=0;i<n;i++)
        {
            printf("vect[%ld]=%lf\n",i,vect[i]);
        }
    }
     
    void aleatmat(double (*mat)[n],long n) // cree une matrice carree de nb entiers aleatoires
    {
        long i,j;
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                mat[i][j]=rand()%(MAX-MIN+1)+MIN;
            }
        }
    }
     
    void affichemat(double (*mat)[n], long n)  //affiche la matrice
    {
        long i,j;
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                printf("mat[%ld][%ld]=%lf\t",i,j,mat[i][j]);
            }
            printf("\n");
        }
    }
     
     
    double normvect(double *vect, long n) //calcule la norme du vecteur
    {
        long i;
        double norm=0.0;
        for(i=0;i<n;i++)
        {
            norm +=pow(vect[i],2);
        }
        return sqrt(norm);
    }
     
      void MX(double (*mat)[n], double *vect,double *b,long n)
      {
        long i,k;
        double v;
        for(i=0;i<n;i++)
        {
            v=0.0;
            for(k=0;k<n;k++)
            {
                v +=mat[i][k]*vect[k];
            }
            b[i]=v;
        }
    }
     
    void copievect(double *vect,double *x,long n)
    {
        long i;
        for(i=0;i<n;i++)
        {
            x[i]=vect[i];
        }
    }
     
    void copiemat(double (*mat)[n],double (*matb)[n],long n)
    {
        long i,j;
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                matb[i][j]=mat[i][j];
            }
        }
    }
     
    void resolution(double (*mat)[n],double *x,double *b,long n)
    {
        long i,j,k;
        double sum;
        for(i=0;i<n-1;i++)
        {
            for(j=i+1;j<n;j++)
            {
                b[j]=mat[i][i]*b[j]-mat[j][i]*b[i];
                for(k=0;k<n;k++)   //k peut partir de j on s en fout que les zeros soient pas affichés
                {
                    mat[j][k]=mat[i][i]*mat[j][k]-mat[j][i]*mat[i][k];
                }
            }
        }
        affichemat(mat,n);
        //affichevect(b,n);
     
        x[n-1]=b[n-1]/mat[n-1][n-1];
        for(i=n-2;i>=0;i--) //on met le resultat dans x
        {
            sum=0.0;
            for(j=i+1;j<n;j++)
            {
                sum +=mat[i][j]*x[j];
            }
            x[i]=(b[i]-sum)/mat[i][i];
     
        }
        //affichevect(x,n);
     
    }

  2. #2
    Membre éclairé
    Profil pro
    Inscrit en
    Décembre 2004
    Messages
    1 298
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2004
    Messages : 1 298
    Points : 886
    Points
    886
    Par défaut
    Salut, si je ne me trompe pa, il faut plutot faire

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    srand((int) time(NULL));
    que

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    srand(time(NULL));
    Ensuite, est-ce que tu as vérifié toutes tes sous fonctions. T'es-tu assurré qu'elles retournent le bon résultat ?
    Ta fonction résolution me parait plus que douteuse : vérifie que ton pivot m[i][i] n'es pas nul auquel cas il faut que tu permutes des lignes. T'es-tu assuré que ta matrice n'est pas singulière ? Je ne vois pas de test d'erreur. Enfin, ton alog me parait un peu douteux... L'as-tu bien écrit à la main ?

    Je t"envoie mon algo. Vérifie qu'il passe tous les tests (matrice singulière, permutations des lignes, etc...)
    Enfin, pour éviter les erreurs de calculs il faudrait prendre comme pivot non pas m[i][i] mais le maximum sur les colonnes des m[i][j]. Auquel cas tu seras amené à aussi permuter les colonnes...

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
     
    #include<stdlib.h>
    #include<stdio.h>
    #include<assert.h>
     
    #define SWAP(a,b) {temp=a; a=b; b=temp;}
    #define PRINTERROR(msg) fprintf (stderr, " Error in file %s in function %s at line : %u :\n" #msg "\nExit program\n", __FILE__,__FUNCTION__, __LINE__)
    #define MAX(a,b) (a>b) ? a : b
     
    void Gauss(double **a,double *b,double *x,unsigned long N);
    void SwapLine(double **a,double *b,unsigned long i,unsigned long j,unsigned long
    N);
    void SwapCol(double **a,unsigned long i,unsigned long j,unsigned long N);
     
    void Pause(void)
    {
      printf("Pause : Press return to continue\n");
      getc(stdin);
    }
     
    void Printa(double **a,unsigned long N)
    {
    unsigned long i,j;
    printf("a = \n");
      for(i=0;i<N;++i)
      {
        for(j=0;j<N;++j)
          printf("%f\t",a[i][j]);
     
        printf("\n");
      }
      printf("\n");
    }
     
    void Printb(double * b,unsigned long N)
    {
    unsigned long i;
    printf("b = \n");
      for(i=0;i<N;++i) printf("%f\n",b[i]);
      printf("\n");
    }
     
    int main()
    {  
      unsigned int N=3,i;
      double **a, *b,*x;
      a=malloc(N*sizeof(*a));
      assert(a!=NULL);
      for(i=0;i<N;++i)
      {
        a[i]=malloc(N*sizeof(*a[i]));
        assert(a[i]!=NULL);
      }
     
      b=malloc(N*sizeof(*b)); assert(b!=NULL);
      x=malloc(N*sizeof(*x)); assert(x!=NULL);
     
      /*
      a[0][0]=1.; a[0][1]=1.; a[0][2]=1.; b[0]=1.;
      a[1][0]=0.; a[1][1]=1.; a[1][2]=0.; b[1]=2.;
      a[2][0]=1.; a[2][1]=0.; a[2][2]=1.; b[2]=3.;
      */
      /*
      a[0][0]=1.; a[0][1]=-1.; a[0][2]=2.; b[0]=5.;
      a[1][0]=3.; a[1][1]=2.; a[1][2]=1.; b[1]=10.;  
      a[2][0]=2.; a[2][1]=-3.; a[2][2]=-2.; b[2]=-10.;
      */
      /*
      a[0][0]=2.; a[0][1]=4.; a[0][2]=5.; b[0]=5.;
      a[1][0]=3.; a[1][1]=2.; a[1][2]=7.; b[1]=10.;  
      a[2][0]=5.; a[2][1]=2.; a[2][2]=8.; b[2]=-10.;
      */
     
      /*
      a[0][0]=1.; a[0][1]=1.; a[0][2]=1.; b[0]=1.;
      a[1][0]=1.; a[1][1]=1.; a[1][2]=0.; b[1]=2.;  
      a[2][0]=0.; a[2][1]=0.; a[2][2]=1.; b[2]=1.;
      */
     
      a[0][0]=1.; a[0][1]=1.; a[0][2]=1.; b[0]=1.;
      a[1][0]=1.; a[1][1]=1.; a[1][2]=0.; b[1]=2.;  
      a[2][0]=1.; a[2][1]=1.; a[2][2]=2.; b[2]=1.;
     
      Printa(a,N);
      Printb(b,N);
     
      printf("Dans la fonction\n");
      Gauss(a,b,x,N);
      for(i=0;i<N;++i) printf("%f\n",x[i]);
     
      for(i=0;i<N;++i)
      {
        free(a[i]);
        a[i]=NULL;
      }
     
      free(b); b=NULL;
      free(x); x=NULL;
      return 0;
    }
     
    void Gauss(double **a,double *b,double *x,unsigned long N)
    {
      unsigned long k,i,j,Nm1=N-1;
      double invakk,aikinvakk;
      long ii;
      unsigned short test;
     
      /* transformation of the matrix a in an upper triangular matrix */
      for(k=0;k<Nm1;++k)
      {
        printf("k = %u\n",k);
     
        if(a[k][k]==0.)
        {
          printf("je suis ici\n");
          test=1;
          for(ii=N-1;ii>k && test;--ii)
          {
            if(a[ii][k]!=0.)
    	{
    	  SwapLine(a,b,k,ii,N);
    	  test=0;
    	}
          }
          printf("test = %u\n",test);
          Printa(a,N);
          if(ii==k)
          {
            PRINTERROR("singular matrix (ii==k)");
    	exit(EXIT_FAILURE);
          }
        }
     
        invakk=1./a[k][k]; /* a[k][k] is the pivot */
        for(i=k+1;i<N;++i)
        {
          printf("i = %u\n",i);
          aikinvakk=a[i][k]*invakk;
          b[i]-=b[k]*aikinvakk;
          for(j=k;j<N;++j) a[i][j]-=a[k][j]*aikinvakk;
          Printa(a,N);
          //Pause();
        }   
      }
     
      Printa(a,N);
      Printb(b,N);
     
      if(a[Nm1][Nm1]==0.)
      {
        PRINTERROR("singular matrix (a[Nm1][Nm1])");
        exit(EXIT_FAILURE);
      }
     
      /* resolution of the linear system */
      double s;
      x[Nm1]=b[Nm1]/a[Nm1][Nm1];
      for(ii=N-2;ii>=0;--ii)
      {
        s=0.;
        for(j=ii+1;j<N;++j) s+=a[ii][j]*x[j];
        x[ii]=(b[ii]-s)/a[ii][ii];
      }
    }
     
    void SwapLine(double **a,double *b,unsigned long i,unsigned long j,unsigned long N)
    {
    /* swaps ith and jth line of the matrix a and the vector b */
      if(i>=N)
      {
        PRINTERROR("Third argument must be lower or equal than N-1");
        exit(EXIT_FAILURE);
      }
     
      if(j>=N)
      {
        PRINTERROR("Fourth argument must be lower or equal than N-1");
        exit(EXIT_FAILURE);
      }
     
      unsigned long k;
      double temp;
      for(k=0;k<N;++k) {temp=a[i][k]; a[i][k]=a[j][k]; a[j][k]=temp;}
      temp=b[i]; b[i]=b[j]; b[j]=temp;
    }
     
    void SwapCol(double **a,unsigned long i,unsigned long j,unsigned long N)
    {
    /* swaps ith and jth column of the matrix a */
      if(i>=N)
      {
        PRINTERROR("Third argument must be lower or equal than N-1");
        exit(EXIT_FAILURE);
      }
     
      if(j>=N)
      {
        PRINTERROR("Fourth argument must be lower or equal than N-1");
        exit(EXIT_FAILURE);
      }
     
      unsigned long k;
      double temp;
      for(k=0;k<N;++k) {temp=a[k][i]; a[k][i]=a[k][j]; a[k][j]=temp;}
    }

  3. #3
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    52
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2006
    Messages : 52
    Points : 32
    Points
    32
    Par défaut
    je pense que tu as raison, je vais tout refaire a la main avant
    la methode que tu precaunises est meilleure, je vais m en inspirer merci

Discussions similaires

  1. [FLASH 8] Probleme méthode send de LoadVars
    Par dom_dev dans le forum Flash
    Réponses: 11
    Dernier message: 26/09/2006, 12h07
  2. probleme methode="post"
    Par ardamus dans le forum Langage
    Réponses: 8
    Dernier message: 01/03/2006, 11h30
  3. Probleme Methode POST
    Par pidu dans le forum Général JavaScript
    Réponses: 6
    Dernier message: 06/02/2006, 17h33
  4. REsolution d'un systeme d'equation
    Par Dr_GonZO dans le forum Algorithmes et structures de données
    Réponses: 4
    Dernier message: 21/03/2005, 09h38
  5. Problème avec l'utilisation de la commande system awk
    Par vbcasimir dans le forum Linux
    Réponses: 3
    Dernier message: 05/10/2004, 16h18

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo