/******************************************************** * SOLVING A LINEAR MATRIX SYSTEM AX=B * * by Gauss-Jordan method * * ----------------------------------------------------- * * Ref.: Mathematiques et statistiques by H. Haut, PSI * * Editions, France, 1981 [BIBLI 13]. * * * * C++ Version By J-P Moreau, Paris * * (www.jpmoreau.fr) * * * * INPUTS from example file sysmat.dat, * * OUTPUTS to sysmat.lst. * * ----------------------------------------------------- * * SAMPLE RUN: * * Input file * * 3 * * 2.0 -1.0 1.0 * * 1.0 5.0 -2.0 * * 3.0 -2.0 3.0 * * 4 * * 5.0 -1.0 1.0 2.0 * * 1.0 2.0 3.0 4.0 * * 3.0 7.556 4.0 4.0 * * * * Output file * * * * ------------------------------------------------------* * SOLVING THE MATRIX LINEAR SYSTEM AX = B * * ------------------------------------------------------* * (BY GAUSS-JORDAN METHOD) * * * * N=3 * * * * MATRIX A: * * * * 2.000000 -1.000000 1.000000 * * 1.000000 5.000000 -2.000000 * * 3.000000 -2.000000 3.000000 * * * * M=4 * * * * MATRIX B: * * * * 5.000000 -1.000000 1.000000 2.000000 * * 1.000000 2.000000 3.000000 4.000000 * * 3.000000 7.556000 4.000000 4.000000 * * * * INVERSE OF MATRIX A: * * * * 0.785714 0.071429 -0.214286 * * -0.642857 0.214286 0.357143 * * -1.214286 0.071429 0.785714 * * * * SOLUTION MATRIX X: * * * * 3.357143 -2.262000 0.142857 1.000000 * * -1.928571 3.770000 1.428571 1.000000 * * -3.642857 7.294000 2.142857 1.000000 * * * * DETERMINANT= 14.000000 * * * * * * VERIFICATION OF AX = B: * * * * 5.000000 -1.000000 1.000000 2.000000 * * 1.000000 2.000000 3.000000 4.000000 * * 3.000000 7.556000 4.000000 4.000000 * * * * * * ------------------------------------------------------* * * ********************************************************/ #include "sysmat.h" // main program sysmat void main() { MAT A,A1,D; MAT1 B,C; REAL DETER; int I,J,M,N; char s[13],nom[9]; printf("\nInput data file name (without .dat): "); scanf("%s",nom); // READ MATRIX A[N][N] TO BE INVERTED // and COPY to OUTPUT FILE strcpy(s,nom); strcat(s,".dat"); fp1=fopen(s,"r"); strcpy(s,nom); strcat(s,".lst"); fp2=fopen(s,"w"); WriteHead(fp2," Solving a linear matrix system AX = B"); fprintf(fp2," (by GAUSS-JORDAN method)\n\n"); fscanf(fp1,"%d",&N); fprintf(fp2," N=%d\n",N); MATREAD(A,N); MATPRINT(" MATRIX A:",A,N); // READ RIGHT SIDE MATRIX B[N][M], IF M<>0 fscanf(fp1,"%d",&M); fprintf(fp2,"\n\n M=%d\n",M); if (M!=0) { MATREAD1(B,N,M); MATPRINT1(" MATRICE B :",B,N,M); } // END DATA SECTION fclose(fp1); // STORING A IN A1 (Original A is lost when calling MATINV) for (I=0; I PAV) { PV=AA[I][J]; PAV= ABS(PV); IK=I; JK=J; } } // Search terminated, the pivot is in location I=IK, J=JK. // Memorizing pivot location: PC[K]=JK; PL[K]=IK; // DETERMINANT DET is actualised // If DET=0, ERROR MESSAGE and STOP // Machine dependant EPSMACH equals here 1e-20 if (IK!=K) *DET=-*DET; if (JK!=K) *DET=-*DET; *DET=*DET*PV; temp= ABS(*DET); if (temp < MACH_EPS) { fprintf(fp2,"\n The determinant equals ZERO !!!\n"); return; } // POSITIONNING PIVOT IN K,K: if(IK!=K) for (I=0; I=0; I--) { IK=(int) PC[I]; if (IK==I) goto fin1; // EXCHANGE LINES I and PC(I) of matrix AA: for (J=0; J=0; J--) { JK=(int) PL[J]; if (JK==J) goto fin2; // EXCHANGE COLUMNS J ET PL(J) of matrix AA: for (I=0; I