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
|
#include <iostream>
#include <cmath>
using namespace std;
const double eps=0.000006;
const int n=3;
const int MAX=3;
bool converge(double *xk, double *xkp)
{
double norm = 0;
for (int i = 0; i < n; i++)
{
norm += (xk[i] - xkp[i])*(xk[i] - xkp[i]);
}
if(sqrt(norm) >= eps)
return false;
return true;
}
void Gaus(double a[][MAX],double x[],double b[])
{
double p[n];
//do
//{
for(int k=0;k<6;k++)
{
for (int i = 0; i < n; i++)
p[i] = x[i];
for (int i = 0; i <n; i++)
{
double var = 0;
for (int j = 0; j < i; j++)
var += (a[i][j] * x[j]);
for (int j = i; j < n; j++)
var += (a[i][j] * p[j]);
x[i] = (b[i] - var) / a[i][i];
cout<<"x["<<i<<"]= "<<x[i]<<'\t';
}
cout<<endl;
}
//}
//while (!converge(x, p));
}
int main()
{
double a[n][n]={{4,-1,0},{-1,4,-1},{0,-1,4}};
double x[n]={0,0,0};
double b[n]={2,6,2};
Gaus(a,x,b);
return 0;
}
|