Advertisement
Guest User

Gaussina (RHZ)

a guest
Oct 27th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. /**
  2.  
  3. GAUSSIAN ELIMINATION
  4.  
  5. Definition:
  6.     ** for equation -> a1X + b1Y + c1Z = d1
  7.     ** for equation -> a2X + b2Y + c2Z = d2
  8.     ** for equation -> a3X + b3Y + c3Z = d3
  9.     ** for equation -> a4X + b4Y + c4Z = d4
  10.     we store the d in the B array
  11.     we store the a, b, c in the A array
  12.  
  13. **/
  14.  
  15.  
  16.  
  17. double A[101][101], B[101];
  18.  
  19. void gausian(int n) {
  20.     int i,j,k,l,e,mxIdx;
  21.     double x,y,cns,cnt,ans,mxV;
  22.     for (i=0,e=0; i<n; i++) {
  23.         mxV = A[e][e];
  24.         mxIdx = e;
  25.         for (j=e; j<n; j++) {
  26.             if (fabs(A[j][i]) > fabs(mxV)) {
  27.                 mxV = A[j][i];
  28.                 mxIdx = j;
  29.             }
  30.         }
  31.         if (mxV < EPS) {
  32.             e++;
  33.             continue;
  34.         }
  35.         for (j=0; j<n; j++) {
  36.             swap(A[mxIdx][j],A[e][j]);
  37.         }
  38.         swap(B[mxIdx],B[e]);
  39.         for (k=0; k<n; k++) A[e][k] /= mxV;
  40.         B[e] /= mxV;
  41.         for (j=0; j<n; j++) {
  42.             if (j != e) {
  43.                 x = A[j][e];
  44.                 if (fabs(x) >= EPS) {
  45.                     for (k=0; k<n; k++) {
  46.                         A[j][k] -= (A[e][k] * x);
  47.                     }
  48.                     B[j] -= (B[e] * x);
  49.                 }
  50.             }
  51.         }
  52.         e++;
  53.     }
  54.     for (i=99; i>=0; i--) {
  55.         for (j=99; j>=0; j--) {
  56.             if (fabs(A[i][j]) >= EPS && i != j) {
  57.                 B[i] -= (A[i][j] * B[j]);
  58.             }
  59.             if (i == j && fabs(A[i][j]) >= EPS) {
  60.                 B[i] /= (A[i][j]);
  61.             }
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement