Numerical Method in C++ | Gaussian Elimination implemented in C++ programming using Code:: Blocks

in #utopian-io8 years ago (edited)

What is Gaussian Elimination?

Gauss Elimination is a methods used to solve values of system of linear equation using a specific algorithm that implement a sequential operation for a matrix of coefficient. In my tutorial involving Numeral Method implemented in Open Office spreadsheet, I stated that " the objective of Gaussian elimination are to make the matrix element (row:column) [1:1] to a value of 1 by performing basic row operations to get zero's in all positions underneath matrix element [1:1]. And, get 1's for leading coefficients in every row diagonally from [1:1] to [3:3], and get 0's beneath all leading coefficients".

What Will I Learn?

At the end of this tutorial, you should be able to implement Gaussian Elimination in C++ programming using Code :: Blocks.

Requirements

To be able to follow this tutorial, you need to have the following tools:

  • A desktop PC or laptop with Windows (7, 8, 10) operating system; and,
  • An installed GCC Compiler and Code:: Blocks Open Source cross-platform IDE software.

If you do not have the following software, you can download the Code:: Blocks software from codeblocks.org. For the GCC Compiler, you can download a MinGW compiler from mingw.org.

Difficulty

Advance

Gaussian Elimination implemented in C++ programming using Code :: Blocks


Part 1: Setting up an New Project in Code :: Blocks



1 | Open Code :: Blocks from your desktop or browse from the start menu. Click image.png to start a new project.

image.png

2 | Select image.png from "New from Template" wizard, and click image.png.

image.png

3 | Click image.png from Console Application wizard.

4 | Set language to C++ and click image.png.

image.png

5 | Type in the project title in the Console application wizard then click image.png.

image.png

6 | Click image.png to finalize the creation of new project.

image.png

Part 2: Implementing the Gaussian Elimination algorithm using C++ programming


1 | To start coding the program for Gaussian Elimination, locate the main.h of the recently created Gaussian Elimination project. Go to Workspace > Gaussian Elimination > Sources > main.cpp .

image.png

Double click on main.cpp.

image.png

2 | Since you have already save the new project setting, you can now start coding the Gaussian Elimination algorithm in C++ programming language. First is to define directives and identifiers to be used in the algorithm.

SyntaxDescription
#include <iostream>C++ input/output standard stream
#include <cmath>(math.h) Declares a set of functions to compute standard mathematical operation
#include <vector>sequence containers representing arrays
using namespace std;allow you to omit using std:: in your codes



Encoding these directives in Code::Blocks

image.png

Code 1 : coding augment matrix A


3 | Initialize the code by void print( vector<vector<double>>A) which set the array A.

4 | Set int n= A.size(); which n determines the size of array A .

5 | Do a loop that will allow to ask repeatedly to users the coefficient of each variables in an equation using a FOR loop until all values for array A[i][j] is filled and IF statement to terminate the loop.

for (int i=0; i<n; i++) {
for (int j=0; j<n+1; j++) {
cout << A[i][j] << "\t";
if (j == n-1) {
cout << "| ";
}
}
cout << "\n";
}
cout << endl;
}

Encoded the codes in the Code::Block

image.png

Code 2: Implementing Gaussian algorithm

6 | Using a FOR loop and IF statement, we search the maximum in columns by establishing IF condition to abs(A[k][i]) > maxEl.

for (int i=0; i<n; i++) {
double maxEl = abs(A[i][i]);
int maxRow = i;
for (int k=i+1; k<n; k++) {
if (abs(A[k][i]) > maxEl) {
maxEl = abs(A[k][i]);
maxRow = k;
}
}

7 | Column by column swapping of current row by maximum row using the operation double tmp = A[maxRow][k]; A[maxRow][k] = A[i][k]; A[i][k] = tmp; . To perform this operation for all columns and rows in the A array use a FOR loop to do repetition until it reach n counts. the complete code is shown below.

for (int k=i; k<n+1;k++) {
double tmp = A[maxRow][k];
A[maxRow][k] = A[i][k];
A[i][k] = tmp;
}

8 | To make all rows below the current row equal to 0, we need to do a mathematical operation set as c which is equal to the determinants -A[k][i]/A[i][i] of the augmented matrix represented by array A. Using and IF-ELSE conditional loop, we apply the formula for determinants repeatedly until n .

for (int k=i+1; k<n; k++) {
double c = -A[k][i]/A[i][i];
for (int j=i; j<n+1; j++) {
if (i==j) {
A[k][j] = 0;
} else {
A[k][j] += c * A[i][j];
}
}
}

9 | Solve for the upper triangle matrix of array A using FOR loop to do the mathematical operation x[i] = A[i][n]/A[i][i];, for row, and A[k][n] -= A[k][i] * x[i];, for column.

vector x(n);
for (int i=n-1; i>=0; i--) {
x[i] = A[i][n]/A[i][i];
for (int k=i-1;k>=0; k--) {
A[k][n] -= A[k][i] * x[i];
}
}
return x;
}

10| To complete the code for Gaussian algorithm, we enclosed the code inside vector<double> gauss(vector< vector<double> > A) { int n = A.size(); } .

Here is the complete code 2:
vector gauss(vector< vector > A) {
int n = A.size();
for (int i=0; i<n; i++) {
double maxEl = abs(A[i][i]);
int maxRow = i;
for (int k=i+1; k<n; k++) {
if (abs(A[k][i]) > maxEl) {
maxEl = abs(A[k][i]);
maxRow = k;
}
}
for (int k=i; k<n+1;k++) {
double tmp = A[maxRow][k];
A[maxRow][k] = A[i][k];
A[i][k] = tmp;
}
for (int k=i+1; k<n; k++) {
double c = -A[k][i]/A[i][i];
for (int j=i; j<n+1; j++) {
if (i==j) {
A[k][j] = 0;
} else {
A[k][j] += c * A[i][j];
}
}
}
}
vector x(n);
for (int i=n-1; i>=0; i--) {
x[i] = A[i][n]/A[i][i];
for (int k=i-1;k>=0; k--) {
A[k][n] -= A[k][i] * x[i];
}
}
return x;
}

image.png

Code 3: Scanning input and printing output

11 | To scan the input data from the user and storing the value of n and array A. Using FOR loop function to repeatedly ask the values of the array A until n. You need to type in:

int main() {
int n;
cin >> n;
vector line(n+1,0);
vector< vector > A(n,line);
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
cin >> A[i][j];
}
}
for (int i=0; i<n; i++) {
cin >> A[i][n];
}

12 | To print the values of array A in a form of a matrix. Type in :

print(A);
vector x(n);
x = gauss(A);

13 | To print the result of Gaussian algorithm (code 2), use cout<< x[i]. Since we need to print all values of n variables, we use a FOR loop to do so.

cout << "Result:\t";
for (int i=0; i<n; i++) {
cout << x[i] << " ";
}
cout << endl;
}

The complete code

#include
#include
#include
using namespace std;
void print(vector< vector > A) {
int n = A.size();
for (int i=0; i<n; i++) {
for (int j=0; j<n+1; j++) {
cout << A[i][j] << "\t";
if (j == n-1) {
cout << "| ";
}
}
cout << "\n";
}
cout << endl;
}
vector gauss(vector< vector > A) {
int n = A.size();
for (int i=0; i<n; i++) {
double maxEl = abs(A[i][i]);
int maxRow = i;
for (int k=i+1; k<n; k++) {
if (abs(A[k][i]) > maxEl) {
maxEl = abs(A[k][i]);
maxRow = k;
}
}
for (int k=i; k<n+1;k++) {
double tmp = A[maxRow][k];
A[maxRow][k] = A[i][k];
A[i][k] = tmp;
}
for (int k=i+1; k<n; k++) {
double c = -A[k][i]/A[i][i];
for (int j=i; j<n+1; j++) {
if (i==j) {
A[k][j] = 0;
} else {
A[k][j] += c * A[i][j];
}
}
}
}
vector x(n);
for (int i=n-1; i>=0; i--) {
x[i] = A[i][n]/A[i][i];
for (int k=i-1;k>=0; k--) {
A[k][n] -= A[k][i] * x[i];
}
}
return x;
}
int main() {
int n;
cin >> n;
vector line(n+1,0);
vector< vector > A(n,line);
for (int i=0; i<n; i++) {
for (int j=0; j<n; j++) {
cin >> A[i][j];
}
}
for (int i=0; i<n; i++) {
cin >> A[i][n];
}
Print input
print(A);
Calculate solution
vector x(n);
x = gauss(A);
cout << "Result:\t";
for (int i=0; i<n; i++) {
cout << x[i] << " ";
}
cout << endl;
}

Snapshots from the code:: block.

image.png
image.png
image.png

Part 3: Result and Simulation

1 | Build the program by clicking image.png; and a debug message will appear showing confirmation that the program is successfully build.

image.png

2 | Run the program by clicking image.png .

image.png

3 | Input value for n variables /unkowns and n coefficient.

image.png

4 | Press enter to show the result.

image.png

image.png

This is the screenshot for the code and simulation.

image.png

Curriculum

This is the first tutorial for the Numerical Method on Code::Blocks series.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

This post has received a 0.56 % upvote from @booster thanks to: @juecoree.

Hey @juecoree I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.04
TRX 0.32
JST 0.077
BTC 62558.26
ETH 1693.86
USDT 1.00
SBD 0.40