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 55 56 57
|
// Naive_Gaussian.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;
void NaiveGaussian(int n, float a[4][4], float b[4], float x[4]);
void main()
{
int nsize = 4;
float aa[4][4] = { { 3, -13, 9, 3 }, { -6, 4, 1, -18 }, { 6, -2, 2, 4 }, { 12, -8, 6, 10 } };
float bb[4] = { -19, -34, 16, 26 };
float xx[4];
NaiveGaussian(nsize, aa, bb, xx); // x is the solution
cout << xx[0] << endl;
cout << xx[1] << endl;
cout << xx[2] << endl;
cout << xx[3] << endl;
cin.ignore();
}
void NaiveGaussian(int n, float a[4][4], float b[4], float x[4]) //paranthesis is the arguments
{
int i, j, k;
float sum, xmult;
for (k = 0; k <= n - 2; k++)
{
for (i = k + 1; i <= n - 1; i++)
{
xmult = a[i][k] / a[k][k];
a[i][k] = xmult;
for (j = k + 1; j <= n - 1; j++)
{
a[i][j] = a[i][j] = -xmult*a[k][j];
}
b[i] = b[i] - xmult*b[k];
}
}
x[n - 1] = b[n - 1] / a[n - 1][n - 1];
for (i = n - 2; i >= 0; i--)
{
sum = b[i];
for (j = i + 1; j = n - 1; j++)
{
sum = sum - a[i][j] * x[j];
}
x[i] = sum / a[i][i];
}
}
|