Posted By:
31 Mar, 2013 9:09 am PDT

Gauss-Seidel, Jacobi, and Successive over relaxation method to solve linear equations in matlab

I have a set of 3 linear equations
0.8x - 0.4y = 41
-0.4x + 0.8y - 0.4z = 25
-0.4y + 0.8z = 105

I want to obtain the solution using Gauss-Seidel, Jacobi, and Successive over relaxation method.
The value of lambda is 1.2 for the Successive over-relaxation method.


The answers should be :
a)For Jacobi result: x = 158.4375, y = 218.2812, z = 238.4375
b) For Gauss Seidel result, x = 161.9922, y = 233.2422, z = 247.8711
c) For successive over relaxation, x = 171.4230, y = 244.3887, z = 253.6222

Attachment:None


clc
clear all
%   0.8x - 0.4y = 41
%  -0.4x + 0.8y - 0.4z = 25
% -0.4y + 0.8z = 105

A = [0.8 -0.4 0; -0.4 0.8 -0.4; 0 -0.4 0.8];
b = [41; 25; 105];

% error tolerance

tol = 0.05;

%initial guess:
x0 = zeros(3,1);


%  Jacobi method
%---------------

xnew=x0;
error=1;
while error>tol
    xold=xnew;

    for i=1:length(xnew)
        off_diag = [1:i-1 i+1:length(xnew)];
        xnew(i) = 1/A(i,i)*( b(i)-sum(A(i,off_diag)*xold(off_diag)) );
    end
    error=norm(xnew-xold)/norm(xnew);
end
x_jacobian=xnew


%Gauss?Seidel:
%---------------

maxiter=1000;
lambda=1;
n=length(x0);
x=x0;
error=1;
iter = 0;
while (error>tol & iter<maxiter)
    xold=x;
    for i=1:n
        I = [1:i-1 i+1:n];
        x(i) = (1-lambda)*x(i)+lambda/A(i,i)*( b(i)-A(i,I)*x(I) );
    end
    error = norm(x-xold)/norm(x);
    iter = iter+1;
end

x_siedal=x


%SOR
%---------------

lambda=1.2;
n=length(x0);
x=x0;
error=1;
iter = 0;
while (error>tol & iter<maxiter)
    xold=x;
    for i=1:n
        I = [1:i-1 i+1:n];
        x(i) = (1-lambda)*x(i)+lambda/A(i,i)*( b(i)-A(i,I)*x(I) );
    end
    error = norm(x-xold)/norm(x);
    iter = iter+1;
end

x_SOR=x

Please watch the video for details**************

Attachment: Jacobi_Siedal_SOR.m
Was it useful?  




Please login to reply to this problem.