Need a javascript or similar program for a Naïve Gauss Elimination and Gauss-Jor
ID: 3811721 • Letter: N
Question
Need a javascript or similar program for a Naïve Gauss Elimination and Gauss-Jordan problem. Please use the following instructions:
As a personal preference I don't like the idea of Gauss elimination with back substitution. So I'm not going to make any of you go through that pain either. basically I want you to write a program that takes a set of linear equations like this: 5w+2x+2y+3z = 15 -11w-3x+5y-7z = -2 2w+4x-6y = -21 w+2y+2z = 4 Although I will probably not give you any more than ten equations in ten unknowns your program has to be able to solve any simultaneous set of linear equations. start the program out by giving it the coefficient matrix c=[[5,2,2,3,15],[-11,-3,5,-7,-2],[2,4,-6,0,-21],[1,0,2,2,4]] and have it print out the solution vector in the form: [5, -2.5, 3.5, -4] remember to try testing the program with various input. for example, what if you wanted to solve: 3x=12 input [[3,12]] output [4]
Explanation / Answer
.cpp file for Gauss Jordan :
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int i, j, k, n;
float a[10][10] = { 0 }, d;
cout << "No of equations ? ";
cin >> n;
cout << "Read all coefficients of matrix with b matrix too " << endl;
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
cin >> a[i][j];
for (i = 1; i <= n; i++)
for (j = 1; j <= 2 * n; j++)
if (j == (i + n))
a[i][j] = 1;
/************** partial pivoting **************/
for (i = n; i > 1; i--)
{
if (a[i - 1][1] < a[i][1])
for (j = 1; j <= n * 2; j++)
{
d = a[i][j];
a[i][j] = a[i - 1][j];
a[i - 1][j] = d;
}
}
cout << "pivoted output: " << endl;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n * 2; j++)
cout << a[i][j] << " ";
cout << endl;
}
/********** reducing to diagonal matrix ***********/
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n * 2; j++)
if (j != i)
{
d = a[j][i] / a[i][i];
for (k = 1; k <= n * 2; k++)
a[j][k] -= a[i][k] * d;
}
}
/************** reducing to unit matrix *************/
for (i = 1; i <= n; i++)
{
d = a[i][i];
for (j = 1; j <= n * 2; j++)
a[i][j] = a[i][j] / d;
}
cout << "your solutions: " << endl;
for (i = 1; i <= n; i++)
{
for (j = n + 1; j <= n * 2; j++)
cout << a[i][j] << " ";
cout << endl;
}
getch();
return 0;
}
Output :
No of equations ? 3
Read all coefficients of matrix with b matrix too
2 3 5
1 4 6
2 7 8
pivoted output:
2 3 5 1 0 0
2 7 8 0 0 1
1 4 6 0 1 0
your solutions:
0.769231 -0.846154 0.153846
-0.307692 -0.461538 0.538462
0.0769231 0.615385 -0.384615
--------------------------------
Process exited after 52.83 seconds with return value 0
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.