Sie sind auf Seite 1von 4

#include<stdio.

h>
#include<iostream>
using namespace std;

float coeff[10][10];
float Dinv[10][10];
float approx[10][1];
float R[10][10];
float matrixRes[10][1];
float b[10][1];
float temp[10][1];
int row, column, siz, navigate;
void multiply(float matrixA[][10], float matrixB[][1])
{
int ctr, ictr;
//function to perform multiplication
for (ctr = 0; ctr < siz; ctr++)
{
matrixRes[ctr][0] = 0;
for (navigate = 0; navigate < siz; navigate++)
matrixRes[ctr][0] = matrixRes[ctr][0] + matrixA[ctr][navigate] *
matrixB[navigate][0];
}
}
void jacobi()
{

cout << "Enter number of Equations = ";


cin >> siz;
cout << "[a].[x]=[b]" << endl;
cout << "Enter Matrix a:" << endl;
for (row = 0; row < siz; row++)
for (column = 0; column < siz; column++)
{
cout << "a[" << row << "," << column << "] = ";
cin >> coeff[row][column];
}

cout << "Enter Matrix b:" << endl;


for (row = 0; row < siz; row++)
{
cout << "b[" << row << "," << siz << "] = ";
cin >> b[row][0];
}
cout<<"Enter the first approximation\n";
for (row = 0; row < siz; row++)
{
cout << "x" << row << " =";

cin >> approx[row][0];


}

for (row = 0; row < siz; row++)//We calculate the diagonal inverse matrix
make all other entries as zero except Diagonal entries whose resciprocal we store
for (column = 0; column < siz; column++)
{
if (row == column)
Dinv[row][column] = 1 / coeff[row][column];
else
Dinv[row][column] = 0;
}
for (row = 0; row < siz; row++)
for (column = 0; column < siz; column++)
{
if (row == column)
R[row][column] = 0;
else
if (row != column)
R[row][column] = coeff[row][column];
}
int iter;
cout<<"Enter the number of iterations:";
cin >> iter;
int ctr = 1;
int octr;
while (ctr <= iter)
{
multiply(R, approx);
for (row = 0; row < siz; row++)
temp[row][0] = b[row][0] - matrixRes[row][0];
multiply(Dinv, temp);
for (octr = 0; octr < siz; octr++)
approx[octr][0] = matrixRes[octr][0];
//printf("%d is\n", ctr);
cout << "The Value after iteration " <<" "<< ctr<<endl;
for (row = 0; row < siz; row++)
{
/*cout << "x"<<row+1<<"=";2
2
printf("%.3f\n", approx[row][0]);
cout << " "; */
cout << "x"<<row+1<<"=" <<approx[row][0]<<" ";
}
cout<<endl;
ctr++;
}

void gaussian_seidel()
{

float a[10][10], b[10], x[10], y[10];


int n = 0, m = 0, i = 0, j = 0;
cout << "Enter number of Equations = : ";
cin >> n;
cout << "[a].[x]=[b]" << endl;
cout << "Enter Matrix a:" << endl;

for (i = 0; i < n; i++)


{
for (j = 0; j < n; j++)
{
cout << "a[" << i << "," << j << "] = ";
//cout << "Enter values number :=" <<"("<< i << ", " << j << ")";
cin >> a[i][j];
}
}
cout << "Enter Matrix b:" << endl;
for (i = 0; i < n; i++)
{
cout << "b[" << i << "," << j << "] = ";

//cout << "Enter values number :=" <<"("<< i << ", " << j << ")";
cin >> b[i];
}
cout << "Enter the first approximation\n";
for (i = 0; i < n; i++)
{
cout << "x" << i << " =";
cin >> x[i];
}
cout << "\nEnter the number of iteration : ";
cin >> m;
int v = m;
while (m > 0)
{
cout << "The Values after iteration" << " " << v - m + 1 << endl;

for (i = 0; i < n; i++)


{
y[i] = (b[i] / a[i][i]);

for (j = 0; j < n; j++)


{
if (j == i)
continue;
y[i] = y[i] - ((a[i][j] / a[i][i]) * x[j]);
x[i] = y[i];

printf("x%d = %f ", i + 1, y[i]);

cout << endl;


}
cout << "\n";
m--;

}
int main()
{

//jacobi();
gaussian_seidel();

Das könnte Ihnen auch gefallen