Можешь перевести на любой язык.
| Код | ////////////////////////////////////////////////////////////////////////////// // // Solving system of linear equations (Zaidel method) // (c) Johna Smith, 1996 // // Method description: // Given: system of linear equations // a11 x1 + a12 x2 + ... + a1n xn = b1 // a21 x1 + a22 x2 + ... + a2n xn = b2 // ... // an1 x1 + an2 x2 + ... + ann xn = bn // // We use following iterative formula to solve this system: // // 1 i-1 N // xi(j+1)=xi(j)- --- ( SUM aik xk(j+1) + SUM aik xk(j) -bi) // aii k=1 k=i // // where xi(j+1) is j+1-th iteration, xi(j) is j-th iteration // //////////////////////////////////////////////////////////////////////////////
#include <stdio.h> #include <math.h>
#define N 3 // size of matrix #define N1 N+1
float matrix[N][N1]= {{10,1,1,12}, {2,10,1,13}, {2,2,10,14} }; float z[N]={0,0,0}; // first iteration
float epsilon=0.0001; // required accuracy
void ShowMatrix(void) { for (int i=0;i<N;i++) { for (int j=0;j<N;j++) printf("%+f*x%d",matrix[i][j],i+1); printf("=%f\n",matrix[i][N]); } }
void main(void) { // Variables declaration float x[N]; short int i,j; int iterations=0,finish=0;
// Printing given matrix ShowMatrix();
while (!finish) { finish=1; for (i=0;i<N;i++) { x[i]=-matrix[i][N]; for (j=0;j<N;j++) x[i]+=matrix[i][j]*z[j]; // don't stop iterations until required accuracy is reached if (fabs(x[i]/matrix[i][i])>=epsilon) finish=0; x[i]=z[i]-x[i]/matrix[i][i]; // next iteration z[i]=x[i]; } iterations++; }
// Printing solution printf("\nSolution:\n"); for (i=0;i<N;i++) printf("x%d=%f\n",i+1,x[i]); printf("%d iterations were made",iterations); }
| |