0% found this document useful (0 votes)
162 views8 pages

Jacobi Gauss

The document describes the Gauss-Seidel and Jacobi iterative methods for solving systems of linear equations Ax = b. It provides code in Octave to initialize matrices A and b, make an initial guess for x, then iteratively update x using either Gauss-Seidel or Jacobi methods until the solution converges within a specified tolerance or maximum number of iterations. The code examples demonstrate applying the methods to sample matrices and solving for x.

Uploaded by

Ryuji Fabre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
162 views8 pages

Jacobi Gauss

The document describes the Gauss-Seidel and Jacobi iterative methods for solving systems of linear equations Ax = b. It provides code in Octave to initialize matrices A and b, make an initial guess for x, then iteratively update x using either Gauss-Seidel or Jacobi methods until the solution converges within a specified tolerance or maximum number of iterations. The code examples demonstrate applying the methods to sample matrices and solving for x.

Uploaded by

Ryuji Fabre
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Gauss-Seidel Method

octave:1> %% Gauss Seidel Method

octave:1> %% Solution of x in Ax=b using Gauss Seidel Method

octave:1> % * _*Initailize 'A' 'b' & intial guess 'x'*_

octave:1> A=[5 -2 3 0;-3 9 1 -2;2 -1 -7 1; 4 3 -5 7]

A =

5 -2 3 0

-3 9 1 -2

2 -1 -7 1

4 3 -5 7

octave:2> b=[-1 2 3 0.5]'

b =

-1.00000

2.00000

3.00000

0.50000

octave:3> x=[0 0 0 0]'

x =

0
octave:4> n=size(x,1);

octave:5> normVal=Inf;

octave:6> tol=1e-5; itr=0;

octave:7> %% Algorithm: Gauss Seidel Method

octave:7> while normVal>tol

> > x_old=x;

> > for i=1:n

> > sigma=0;

> > for j=1:i-1

> > sigma=sigma+A(i,j)*x(j);

> > end

> > for j=i+1:n

> > sigma=sigma+A(i,j)*x_old(j);

> > end

> > x(i)=(1/A(i,i))*(b(i)-sigma);

> > end

> > itr=itr+1;

> > normVal=norm(x_old-x);

> > end

octave:8> fprintf('Solution of the system is : \n%f\n%f\n%f\n%f in %d

iterations',x,itr);

Solution of the system is :

0.178697

0.230293

-0.477635

-0.470549 in 8 iterations
octave:1> %% Gauss Seidel Method
octave:1> %% Solution of x in Ax=b using Gauss Seidel Method
octave:1> % * _*Initailize 'A' 'b' & intial guess 'x'*_
octave:1> A=[9 -9 9 9;-9 9 9 -9;9 -9 -9 9; 9 9 -9 9]
A =

9 -9 9 9
-9 9 9 -9
9 -9 -9 9
9 9 -9 9

octave:2> b=[-1 2 3 0.5]'


b =

-1.00000
2.00000
3.00000
0.50000

octave:3> x=[0 0 0 0]'


x =

0
0
0
0

octave:4> n=size(x,1);
octave:5> normVal=Inf;
octave:6> % * _*Tolerence for method*_
octave:6> tol=1e-5; itr=0;
octave:7> %% Algorithm: Gauss Seidel Method
octave:7> while normVal>tol
> > x_old=x;
> > for i=1:n
> > sigma=0;
> > for j=1:i-1
> > sigma=sigma+A(i,j)*x(j);
> > end
> > for j=i+1:n
> > sigma=sigma+A(i,j)*x_old(j);
> > end
> > x(i)=(1/A(i,i))*(b(i)-sigma);
> > end
> > itr=itr+1;
> > normVal=norm(x_old-x);
> > end
octave:7> fprintf('Solution of the system is : \n%f\n%f\n%f\n%f in
%d iterations',x,itr);
Solution of the system is :
398218333693.026062
58306571.611762
-5691.666667
-398276645956.248901 in 10245 iterations
Jacobi Method
octave:1> %% Jacobi Method
octave:1> %% Solution of x in Ax=b using Jacobi Method
octave:1> % * _*Initailize 'A' 'b' & intial guess 'x'*_
octave:1> A=[5 -2 3 0;-3 9 1 -2;2 -1 -7 1; 4 3 -5 7]
A =

5 -2 3 0
-3 9 1 -2
2 -1 -7 1
4 3 -5 7

octave:2> b=[-1 2 3 0.5]'


b =

-1.00000
2.00000
3.00000
0.50000

octave:3> x=[0 0 0 0]'


x =

0
0
0
0

octave:4> n=size(x,1);
octave:5> normVal=Inf;
octave:6> % * _*Tolerence for method*_
octave:6> tol=1e-5; itr=0;
octave:7> %% Algorithm: Jacobi Method
octave:7> while normVal>tol
> > xold=x;
> > for i=1:n
> > sigma=0;
> > for j=1:n
> > if j~=i
> > sigma=sigma+A(i,j)*x(j);
> > end
> > end
> > x(i)=(1/A(i,i))*(b(i)-sigma);
> > end
> > itr=itr+1;
> > normVal=abs(xold-x);
> > end
octave:8> fprintf('Solution of the system is : \n%f\n%f\n%f\n%f in %d
iterations',x,itr);
Solution of the system is :
0.178697
0.230293
-0.477635
-0.470549 in 8 iterations
octave:1> %% Jacobi Method
octave:1> %% Solution of x in Ax=b using Jacobi Method
octave:1> % * _*Initailize 'A' 'b' & intial guess 'x'*_
octave:1> A=[5 -1 2 1;-2 1 2 -1;2 -1 -2 1; 2 1 -2 1]
A =

5 -1 2 1
-2 1 2 -1
2 -1 -2 1
2 1 -2 1

octave:2> b=[-1 2 3 0.5]'


b =

-1.00000
2.00000
3.00000
0.50000

octave:3> x=[0 0 0 0]'


x =

0
0
0
0

octave:4> n=size(x,1);
octave:5> normVal=Inf;
octave:6> % * _*Tolerence for method*_
octave:6> tol=1e-5; itr=0;
octave:7> %% Algorithm: Jacobi Method
octave:7> while normVal>tol
> > xold=x;
> > for i=1:n
> > sigma=0;
> > for j=1:n
> > if j~=i
> > sigma=sigma+A(i,j)*x(j);
> > end
> > end
> > x(i)=(1/A(i,i))*(b(i)-sigma);
> > end
> > itr=itr+1;
> > normVal=abs(xold-x);
> > end
octave:8> fprintf('Solution of the system is : \n%f\n%f\n%f\n%f in %d
iterations',x,itr);
Solution of the system is :
97.000001
4.583338
-75.000000
-348.083341 in 30 iterations

You might also like