Professor Diomar Cesar Lobao

Universidade Federal Fluminense-Volta Redonda, RJ, Brasil

Diomar Cesar


Dept. Ciências Exatas - Exact Science Dept.

Search

gssornl.m

% Illustration of Gauss-Seidel iteration with relaxation
%
% NON-LINEAR SYSTEM
%
% The goal is to determine a value of x1 and x2 that simultaneously
% satisfy the following equations...
%
%   f1(x1,x2) = x1^2 + x2^2 + exp(x1) - 7.7183*x1 = 0
%   f2(x1,x2) = x2 + exp(x2) + x1^3 - 10.389      = 0
%
% we will take an x1 out of f1(x1,x2) and an x2 out of f2(x1,x2)
% thus we will have
%
%   x1 = g1(x1,x2) = (x1^2 + x2^2 + exp(x1))/7.7183 and
%   x2 = g2(x1,x2) = 10.389 - exp(x2) - x1^3
%
% The g-functions are defined in files 'g1.m' and 'g2.m'
%

clear;

fprintf('\nSolution of Non-Linear Equations using Gauss-Seidel iteration\n');

      x1 = input('Enter x1 estimate         : ');
      x2 = input('Enter x2 extimate         : ');
       w = input('Enter relaxation factor   : ');
maxerror = input('Enter max approx error    : ');
   maxit = input('Enter max # of iterations : ');

count = 0;
error = 1;

fprintf('\niteration      x1           x2      max error\n');

while (error > maxerror) & (count < maxit)
    count = count + 1;

    temp = g1(x1,x2);
    x1new = x1 + w * (temp - x1);

    temp = g2(x1new,x2);
    x2new = x2 + w * (temp - x2);

    error1 = abs(x1new - x1);
    error2 = abs(x2new - x2);
    error = max([error1,error2]);
  
    fprintf('%6g    %10g %10g %10g\n',count,x1new,x2new,error);

    x1 = x1new;                           % assign new values
    x2 = x2new;
end
Skip to content