Advertisement
luizaspan

Escalonamento (Eliminação Gaussiana) + solução do sistema

Sep 9th, 2015
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.05 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. #define N 4
  5.  
  6. int main(void)
  7. {
  8.     double A[N][N]={{2,1,4,1},{3,4,-1,-1},{1,-4,1,5},{2,-2,1,3}}, b[N]={-4,3,9,7}, x[N]={0};
  9.     int i,j,k;
  10.  
  11.     printf("Antes do escalonamento: \n");
  12.     for (i=0;i<N;i++)
  13.     {
  14.         for (j=0;j<N;j++)
  15.         {
  16.             printf("%8.3lf",A[i][j]);
  17.  
  18.             if (j==(N-1))
  19.                 printf("\n");
  20.             else
  21.                 printf(" ");
  22.         }
  23.     }
  24.  
  25.     for (i=0;i<N;i++)
  26.     {
  27.         for (j=i+1;j<N;j++)
  28.         {
  29.             double c=A[j][i]/A[i][i];
  30.  
  31.             for (k=i;k<N;k++)
  32.             {
  33.                 A[j][k]=A[j][k] - c*A[i][k];
  34.             }
  35.  
  36.             b[j]=b[j]-c*b[i];
  37.         }
  38.     }
  39.  
  40.     printf("Após o escalonamento: \n");
  41.     for (i=0;i<N;i++)
  42.     {
  43.         for (j=0;j<N;j++)
  44.         {
  45.             printf("%8.3lf",A[i][j]);
  46.  
  47.             if (j==(N-1))
  48.                 printf("\n");
  49.             else
  50.                 printf(" ");
  51.         }
  52.     }
  53.  
  54.  
  55.     for (i=N-1;i>=0;i--)
  56.     {
  57.         double soma = b[i];
  58.  
  59.         for (j=i+1;j<N;j++)
  60.         {
  61.             soma=soma-A[i][j]*x[j];
  62.         }
  63.  
  64.         x[i]=soma/A[i][i];
  65.     }
  66.  
  67.     printf("Solução: \n");
  68.     for (i=0;i<N;i++)
  69.     {
  70.         printf("%8.3lf",x[i]);
  71.  
  72.         if (i==(N-1))
  73.             printf("\n");
  74.         else
  75.             printf(" ");   
  76.     }
  77.  
  78.  
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement