Solution for a System of Linear Algebraic Equations
Objective : - To Solve the System of Linear Equations Using the Jacobi & the Gauss - Seidal Iteration Method
Theory:
Consider a system of linear equations as,
Which can be written in a compact form by using a Matrix – Vector Notation as
There are many ways to solve a system of Linear Equations:
The Jacobi Method
The Jacobi Method Named after, Carl Gustav Jacob Jacobi(1804 – 1851) makes two assumptions:
1. That the system is given by
has a unique solution.
1. The coefficient matrix A has no zeros on its main diagonal. If any of the diagonal entries a11, a22, ….. ann are Zero, then the rows or columns must be interchanged to obtain a coefficient matrix that has nonzero entries on the main diagonal.
To begin the Jacobi method, solve the first equation for x1, the second equation for x2 and so on as follows,
hen make an Initial Approximation of the solution,
(x1, x2, x3 …. xn) – Initial Approximation
and substitute these values (x1, x2, x3 …. xn) into the right-hand side of the rewritten equations to obtain the first approximation. After this procedure, One Iteration is performed, In the same way, the second approximation is formed by substituting the first approximation’s x values into the right-hand side of the rewritten equations. By repeated iterations, we get the sequence of approximation that often converges to the actual solution.
The Gauss-Seidel Method
The Modification of the Jacobi Method is called the Gauss-Seidel Method named after Carl Friedrich Gauss (1777 – 1855) and Philipp L. Seidel (1821 – 1896). This method requires lesser number of iterations to produce the same degree of accuracy.
With the Jacobi method, the values of xi obtained in the nth approximation remain unchanged until the entire (n + 1)th approximation has been calculated. With the Gauss-Seidel Method, you can use the new values of each xi from the first equation, then its value is then used in the second equation to obtain the new x2. Similarly, the new x1 and x2 are used in the third equation to obtain the new x3 and so on.
Convergence of the Jacobi & Gauss-Seidel Methods:
If the diagonal matrix is dominant, then the system of linear equations given by Ax = b, has a unique solution to which the Jacobi Method and the Gauss-Seidel Method will converge for any initial approximation.
Program - The Jacobi Method:
//Program to solve a system of linear equation using JACOBI Iteration Method
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
#include<math.h>
#define F1(x2,x3) ((17-20*x2+2*x3)/20)
#define F2(x1,x3) ((-18-3*x1+x3)/20)
#define F3(x1,x2) ((25-2*x1+3*x2)/20)
void main()
{
clrscr();
int i=0,INO=1;
double x1=0,x2=0,x3=0,y1=0,y2=0,y3=0,err;
cout<<"\n Enter the Initial Guess of x1,x2 & x3: "<<endl;
cin>>x1>>x2>>x3;
cout<<"\n Enter the Precision: ";
cin>>err;
cout<<setprecision(5);
cout<<"\n INO \t x1 \t x2 \t x3";
cout<<"\n "<<INO<<" \t "<<x1<<" \t "<<x2<<" \t "<<x3;
do
{
y1=F1(x2,x3);
y2=F2(x1,x3);
y3=F3(x1,x2);
if(fabs(y1-x1)<err&&fabs(y2-x2)<err&&fabs(y3-x3)<err)
{
cout<<"\n x1 = "<<y1;
cout<<"\n x2 = "<<y2;
cout<<"\n X3 = "<<y3;
i=1;
}
else
{
x1=y1;
x2=y2;
x3=y3;
cout<<"\n "<<INO<<" \t "<<x1<<" \t "<<x2<<" \t "<<x3;
INO++;
}
}while(i!=1);
getch( );
}
Program - The Gauss-Seidel Method:
//Program to solve a system of linear equation using JACOBI Iteration Method
#include<iostream.h>
#include<iomanip.h>
#include<conio.h>
#include<math.h>
#define F1(x2,x3) ((17-20*x2+2*x3)/20)
#define F2(x1,x3) ((-18-3*x1+x3)/20)
#define F3(x1,x2) ((25-2*x1+3*x2)/20)
void main()
{
clrscr();
int i=0,INO=1;
double x1=0,x2=0,x3=0,y1=0,y2=0,y3=0,err;
cout<<"\n Enter the Initial Guess of x1,x2 & x3: "<<endl;
cin>>x1>>x2>>x3;
cout<<"\n Enter the Precision: ";
cin>>err;
cout<<setprecision(5);
cout<<"\n INO \t x1 \t x2 \t x3";
cout<<"\n "<<INO<<" \t "<<x1<<" \t "<<x2<<" \t "<<x3;
do
{
y1=F1(x2,x3);
y2=F2(y1,x3);
y3=F3(y1,y2);
if(fabs(y1-x1)<err&&fabs(y2-x2)<err&&fabs(y3-x3)<err)
{
cout<<"\n x1 = "<<y1;
cout<<"\n x2 = "<<y2;
cout<<"\n X3 = "<<y3;
i=1;
}
else
{
x1=y1;
x2=y2;
x3=y3;
cout<<"\n "<<INO<<" \t "<<x1<<" \t "<<x2<<" \t "<<x3;
INO++;
}
}while(i!=1);
getch( );
}