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

Lazarus Pascal Discussion :

Calcul du déterminant d'une matrice [Lazarus]


Sujet :

Lazarus Pascal

  1. #1
    Futur Membre du Club
    Femme Profil pro
    Étudiant
    Inscrit en
    Janvier 2015
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Tchèque Rep.

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2015
    Messages : 5
    Points : 7
    Points
    7
    Par défaut Calcul du déterminant d'une matrice
    Salut à tous !

    Je dois calculer le déterminant d'une matrice de rang n grâce à Lazarus et j'ai essayé de tirer quelque chose du programme du site. Mais mon programme me donne pas le bon déterminant... :/ Quelqu'un a une idée d'où je me suis plantée ? Surtout que le programme se lance 2 fois aussi...

    Merci de votre aide !

    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
    PROGRAM determ (input,output);
     
     
    TYPE
      tmat = array [1..10,1..10] of real;
     
    VAR
      N : integer; { dimension de la matrice }
      det : real;    { déterminant }
      mat : tmat;    { matrice à calculer }
       I,J : integer ;
     
    procedure sous_mat (A : tmat; var B : tmat; ind, N : integer);
    { on supprime la colonne 1 et la ligne ind pour avoir la s/mat de N-1 }
    //A matrice de debut B matrice de fin
    var J, I, L : integer;
     
    begin
      L := 0;
      for I := 1 to N do
      begin
        if I <> ind then
      begin
           L := L + 1;
           for J := 2 to N do
             B[L,J - 1] := A[I,J]
      end
      end
    end;                   
     
    function detn (M : tmat; Nm : integer) : real; // M : matrice Nm:ordre de la matrice
    { On cherche l'ordre Nm de la sous matrice en fonction de l'ordre Nm-1 & determinant detn}
     
    var 
    Res : real;   // determinant
        mprim : tmat; // matrice intermédiaire 
        I, S: integer;  // I lignes J colonnes S signe des lignes
    begin
      if Nm = 1
      then detn := M[1,1];
      if Nm = 2
      then detn:=M[1,1]*M[2,2]-M[2,1]*M[1,2];
      if Nm=3
      then detn:=M[1,1]*M[2,2]*M[3,3]+M[2,1]*M[3,2]*M[1,3]+M[1,2]*M[2,3]*M[3,1]
      -M[3,1]*M[2,2]*M[1,3]-M[3,2]*M[2,3]*M[1,1]-M[2,1]*M[1,2]*M[3,3]
      else
      begin
        Res := 0;
        S := -1;
        for I := 1 to Nm do
        begin
          sous_mat(M,mprim,I,Nm);
        S:= -S;    //change sign every lines
     
          Res := Res + (S * M[I,1] * detn(mprim,Nm - 1))
        end;
        detn := Res
      end
    end;
     
    BEGIN //principal program
     
            Write('Matrix order ');
            readln(N);
            for I:=1 to N do
            begin
                    writeln('line ',I:2);
                    for J:=1 to N do
                            readln(mat[I,J]);
            end;
     
            det:=detn(mat,N);//find the determinant
            Writeln('Determinant: ',det:2:1);
            readln;
    END.

  2. #2
    Expert confirmé
    Avatar de Ph. B.
    Homme Profil pro
    Freelance
    Inscrit en
    Avril 2002
    Messages
    1 784
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 57
    Localisation : France, Haute Garonne (Midi Pyrénées)

    Informations professionnelles :
    Activité : Freelance
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2002
    Messages : 1 784
    Points : 5 915
    Points
    5 915
    Par défaut
    Bonjour,
    Tout d'abord, n'oubliez pas la balise [ CODE ] [ /CODE ] quand vous attachez un extrait de code à votre message, cela facilite la compréhension...

    Je ne vois pas d'erreur dans le code (cf. les exemples fournis). Quant au double lancement, ce n'est pas lié au programme...

    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
    PROGRAM determ(input, output);
     
    type
      tmat = array [1..10, 1..10] of real;
     
    var
      N: integer; { dimension de la matrice }
      det: real; { déterminant }
      mat: tmat; { matrice à calculer }
      I, J: integer;
     
    procedure sous_mat(A: tmat; var B: tmat; ind, N: integer);
    { on supprime la colonne 1 et la ligne ind pour avoir la s/mat de N-1 }
    //A matrice de debut B matrice de fin
    var
      J, I, L: integer;
    begin
      L := 0;
      for I := 1 to N do
      begin
        if I <> ind then
        begin
          L := L + 1;
          for J := 2 to N do
            B[L, J - 1] := A[I, J];
        end;
      end;
    end;
     
    function detn(M: tmat; Nm: integer): real; // M : matrice Nm:ordre de la matrice
      { On cherche l'ordre Nm de la sous matrice en fonction de l'ordre Nm-1 & determinant detn}
    var
      Res: real; // determinant
      mprim: tmat; // matrice intermédiaire
      I, S: integer; // I lignes J colonnes S signe des lignes
    begin
      if Nm = 1 then
        detn := M[1, 1];
      if Nm = 2 then
        detn := M[1, 1] * M[2, 2] - M[2, 1] * M[1, 2];
      if Nm = 3 then
        detn := M[1, 1] * M[2, 2] * M[3, 3] + M[2, 1] * M[3, 2] * M[1, 3] +
          M[1, 2] * M[2, 3] * M[3, 1] - M[3, 1] * M[2, 2] * M[1, 3] -
          M[3, 2] * M[2, 3] * M[1, 1] - M[2, 1] * M[1, 2] * M[3, 3]
      else
      begin
        Res := 0;
        S := -1;
        for I := 1 to Nm do
        begin
          sous_mat(M, mprim, I, Nm);
          S := -S; //change sign every lines
     
          Res := Res + (S * M[I, 1] * detn(mprim, Nm - 1));
        end;
        detn := Res;
      end;
    end;
     
     
     
    begin
      mat[1, 1] := 4;  mat[1, 2] := 3;  mat[1, 3] := 1; // 4 3 1
      mat[2, 1] := 5;  mat[2, 2] := 2;  mat[2, 3] := 7; // 5 2 7
      mat[3, 1] := 8;  mat[3, 2] := 0;  mat[3, 3] := 2; // 8 0 2
     
      det := detn(mat, 3);//find the determinant
      Writeln('Determinant: ', det: 2: 1);      // 138
      readln;
     
      mat[1, 1] := -1;  mat[1, 2] :=  0;  mat[1, 3] :=  1;  mat[1, 4] :=  1; // -1  0  1  1
      mat[2, 1] :=  1;  mat[2, 2] := -2;  mat[2, 3] :=  1;  mat[2, 4] := -1; //  1 -2  1 -1
      mat[3, 1] :=  1;  mat[3, 2] :=  0;  mat[3, 3] := -1;  mat[3, 4] :=  1; //  1  0 -1  1
      mat[4, 1] :=  1;  mat[4, 2] :=  0;  mat[4, 3] :=  1;  mat[4, 4] := -1; //  1  0  1 -1
     
      det := detn(mat, 4);//find the determinant
      Writeln('Determinant: ', det: 2: 1);      //-8
      readln;
     
      mat[1, 1] :=  2;  mat[1, 2] :=  3;  mat[1, 3] := -1;  mat[1, 4] :=  3;  mat[1, 5] :=  1; //  2  3 -1  3  1
      mat[2, 1] :=  3;  mat[2, 2] :=  1;  mat[2, 3] := -4;  mat[2, 4] :=  3;  mat[2, 5] := -1; //  3  1 -4  3 -1
      mat[3, 1] :=  1;  mat[3, 2] := -2;  mat[3, 3] :=  3;  mat[3, 4] := -4;  mat[3, 5] :=  2; //  1 -2  3 -4  2
      mat[4, 1] :=  1;  mat[4, 2] :=  2;  mat[4, 3] := -3;  mat[4, 4] :=  2;  mat[4, 5] := -2; //  1  2 -3  2 -2
      mat[5, 1] :=  2;  mat[5, 2] :=  0;  mat[5, 3] :=  0;  mat[5, 4] :=  4;  mat[5, 5] := -5; //  2  0  0  4 -5
     
      det := detn(mat, 5);//find the determinant
      Writeln('Determinant: ', det: 2: 1);      //-580
      readln;
     
      mat[1, 1] :=  7;  mat[1, 2] :=  2;  mat[1, 3] := -8;  mat[1, 4] :=  4;  mat[1, 5] :=  6; //  7  2 -8  4  6
      mat[2, 1] := -3;  mat[2, 2] := -1;  mat[2, 3] :=  4;  mat[2, 4] := -2;  mat[2, 5] := -3; // -3 -1  4 -2 -3
      mat[3, 1] :=  6;  mat[3, 2] :=  2;  mat[3, 3] :=  2;  mat[3, 4] :=  4;  mat[3, 5] :=  7; //  6  2  2  4  7
      mat[4, 1] :=  1;  mat[4, 2] :=  3;  mat[4, 3] :=  7;  mat[4, 4] :=  5;  mat[4, 5] :=  1; //  1  3  7  5  1
      mat[5, 1] := -2;  mat[5, 2] :=  2;  mat[5, 3] :=  3;  mat[5, 4] :=  4;  mat[5, 5] :=  7; // -2  2  3  4  7
     
      det := detn(mat, 5);//find the determinant
      Writeln('Determinant: ', det: 2: 1);      //-1
      readln;
     
      mat[1, 1] :=  1;  mat[1, 2] :=  3;  mat[1, 3] :=  1;  mat[1, 4] := -1;  mat[1, 5] :=  7; //  1  3  1 -1  7
      mat[2, 1] :=  2;  mat[2, 2] :=  2;  mat[2, 3] :=  2;  mat[2, 4] :=  0;  mat[2, 5] :=  2; //  2  2  2  0  2
      mat[3, 1] :=  3;  mat[3, 2] :=  1;  mat[3, 3] :=  3;  mat[3, 4] := -8;  mat[3, 5] :=  1; //  3  1  3 -8  1
      mat[4, 1] :=  3;  mat[4, 2] :=  2;  mat[4, 3] :=  4;  mat[4, 4] :=  1;  mat[4, 5] :=  3; //  3  2  4  1  3
      mat[5, 1] :=  5;  mat[5, 2] :=  2;  mat[5, 3] :=  5;  mat[5, 4] :=  2;  mat[5, 5] :=  2; //  5  2  5  2  2
     
      det := detn(mat, 5);//find the determinant
      Writeln('Determinant: ', det: 2: 1);      //-224
      readln;
     
      Exit;
     
      Write('Matrix order ');
      readln(N);
      for I := 1 to N do
      begin
        writeln('line ', I: 2);
        for J := 1 to N do
          readln(mat[I, J]);
      end;
     
      det := detn(mat, N);//find the determinant
      Writeln('Determinant: ', det: 2: 1);
      readln;
    end.
    Philippe.

  3. #3
    Futur Membre du Club
    Femme Profil pro
    Étudiant
    Inscrit en
    Janvier 2015
    Messages
    5
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Tchèque Rep.

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Janvier 2015
    Messages : 5
    Points : 7
    Points
    7
    Par défaut
    Ah, merci pour la correction, effectivement c'est plus lisible d'un coup

    Bon, alors en fait j'ai réussi à trouver l'erreur dans mon code et visiblement ça aurait aussi fixé l'erreur du programme qui se lance 2 fois ! En fait, je sais pas pourquoi d'ailleurs, au lieu de mettre "else" ligne 46, je l'ai remplacé par "If Nm>3 then" parce qu'en fait ça me calculait pas le déterminant pour Nm = 2 j'avais toujours un résultat égal a 0

    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
     
       begin
         if Nm = 1
         then Detn := B[1,1];
     
         if Nm = 2
         then Detn:=B[1,1]*B[2,2]-B[2,1]*B[1,2];
     
         if Nm=3
         then Detn:=B[1,1]*B[2,2]*B[3,3]+B[2,1]*B[3,2]*B[1,3]+B[1,2]*B[2,3]*B[3,1]
         -B[3,1]*B[2,2]*B[1,3]-B[3,2]*B[2,3]*B[1,1]-B[2,1]*B[1,2]*B[3,3];
     
         if Nm>3
         then
                begin
                     Res := 0;
                     S := -1;
     
                     for I := 1 to Nm do
    Donc mon programme "final" serait

    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
     
    PROGRAM determinant (input,output);
     
     
    TYPE
    tmat = array [1..10,1..10] of real;
     
    VAR
    N : integer; { dimension de la matrice }
    det : real; { déterminant }
    mat : tmat; { matrice à calculer }
    I,J : integer ;
     
    procedure submatrix (A : tmat; var B : tmat; ind, N : integer);
                        //finding the submatrix
                        //A matrix of beginning and B end matrix
    var J, I, L : integer; // I lines J columns
     
        begin
             L := 0;
        for I := 1 to N do
                          begin
                          if I <> ind then
                                      begin
                                      L := L + 1;
                                       for J := 2 to N do
                                       B[L,J - 1] := A[I,J]
                                      end
                          end
        end;
     
    function Detn (B : tmat; Nm : integer) : real; // Nm:matrix order
                                                   //finding derminant of submatrix
    var
    Res : real; // result of determinant
    int: tmat; // intermediate matrix
    I, S: integer; // I lines J columns S sign
     
       begin
         if Nm = 1
         then Detn := B[1,1];
     
         if Nm = 2
         then Detn:=B[1,1]*B[2,2]-B[2,1]*B[1,2];
     
         if Nm=3
         then Detn:=B[1,1]*B[2,2]*B[3,3]+B[2,1]*B[3,2]*B[1,3]+B[1,2]*B[2,3]*B[3,1]
         -B[3,1]*B[2,2]*B[1,3]-B[3,2]*B[2,3]*B[1,1]-B[2,1]*B[1,2]*B[3,3];
     
         if Nm>3   then
                begin
                     Res := 0;
                     S := -1;
     
                     for I := 1 to Nm do
     
                           begin
                                submatrix(B,int,I,Nm);
                                S:= -S; //change sign every lines
     
                                Res := Res + (S * B[I,1] * detn(int,Nm - 1))
                           end;
                               detn := Res
                end
    end;
     
    BEGIN //principal program
     
    Writeln('Matrix order ');
    readln(N);
     
      for I:=1 to N do
     
              begin
              writeln('line ',I:2);
              for J:=1 to N do
              readln(mat[I,J]);
              end;
     
    det:=detn(mat,N); //find the determinant
    Writeln('Determinant: ',det:2:1);
    readln;
    END.
    Je sais pas ce que vous en pensez ^^ (oui les descriptions sont en anglais parce que mes cours sont en anglais )

  4. #4
    Expert éminent
    Avatar de jurassic pork
    Homme Profil pro
    Bidouilleur
    Inscrit en
    Décembre 2008
    Messages
    3 947
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Bidouilleur
    Secteur : Industrie

    Informations forums :
    Inscription : Décembre 2008
    Messages : 3 947
    Points : 9 275
    Points
    9 275
    Par défaut
    hello,
    ElodyE tu avais un problème à la ligne 46 car ton else s'appliquait à la condition if Nm = 3 donc en fait quand tu avais Nm = 2 tu exécutais le code de la condition if Nm = 2 et aussi le else du Nm =3 .
    Pour optimiser ton code je te conseille l'utilisation du case plutôt que des if pour tester Nm voir ici

    Ami calmant, J.P
    Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Calculer le determinant d'une matrice carrée
    Par NThierry dans le forum C
    Réponses: 15
    Dernier message: 27/08/2006, 11h31
  2. Inversion et déterminant d'une matrice
    Par coline dans le forum Algorithmes et structures de données
    Réponses: 16
    Dernier message: 23/06/2006, 09h01
  3. calcul du determinant d'une matrice
    Par gautret dans le forum Algorithmes et structures de données
    Réponses: 12
    Dernier message: 17/03/2006, 21h30
  4. [Matrices] Comment calculer le Déterminant d'une matrice 4x4
    Par cyber_N dans le forum Algorithmes et structures de données
    Réponses: 70
    Dernier message: 19/08/2005, 15h47
  5. [Débutant] Calculer le déterminant d'une matrice
    Par v4np13 dans le forum Mathématiques
    Réponses: 7
    Dernier message: 30/05/2005, 17h24

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