Gauss Seidel Method with Relaxation

4 views (last 30 days)
Amanda
Amanda on 7 Nov 2017
Answered: resul demir on 27 Dec 2018
I am working on a matlab code using the Gauss Seidel method with relaxation to solve a system to a specified tolerance of es = 5%.
Something is wrong with my code, as I am not getting the correct output. Any help would be appreciated.
% function x=GaussSeidel_Relax_Example(lambda, es)
% Gauss-Seidel for a 3-by-3 linear system of equations
%
%--Input
% lambda: relaxation parameter
% es: error tolerance (approximate absolute percent relative error in solution)
%
%--Output
% x: Gauss-Seidel linear solution (3-by-1 vector)
% Note: 1. Use the Euclidian norm (i.e. 2-norm) to compute relative error.
% 2. You may need to rearrange the linear equations to achieve diagonal dominance.
% Write your code here.
lambda = 0.7;
es = 0.5;
Eqn1_LHS = [10 2 -1];
Eqn1_RHS = 27;
Eqn2_LHS = [-3 -6 2];
Eqn2_RHS = -61.5;
Eqn3_LHS = [1 1 5];
Eqn3_RHS = -21.5;
x = [10 10 10]; % Initial guess for the solution x
error = 1; % Initializing error
iter = 0; % number of iterations
while error > es % Running the code until actual error is greater than tolerance
x_old = x;
x(1,1) = (1-lambda) * x(1,1) + lambda * ((Eqn1_RHS - Eqn1_LHS(1,2) * x(1,2) - Eqn1_LHS(1,3) * x(1,3)) / (Eqn1_LHS(1,1)));
x(1,2) = (1-lambda) * x(1,2) + lambda * ((Eqn2_RHS - Eqn2_LHS(1,1) * x(1,1) - Eqn2_LHS(1,3) * x(1,3)) / (Eqn2_LHS(1,2)));
x(1,3) = (1-lambda) * x(1,3) + lambda * ((Eqn3_RHS - Eqn3_LHS(1,1) * x(1,1) - Eqn3_LHS(1,2) * x(1,2)) / (Eqn3_LHS(1,3)));
error = norm(x - x_old) / norm(x);
iter = iter + 1;
end
disp('Total number of iterations needed is:');
disp(iter)
end
The output I am given is 0.4685, 8.2208, -5.9833.
The correct output is 0.608679, 7.964641, -5.976428
I think I am messing up and not fully understanding making an initial guess or initializing error. Could someone please help review the code. I keep trying to work at it, but am clearly missing something. Thank you.

Answers (1)

resul demir
resul demir on 27 Dec 2018
Do you have any solutions for Gauss Seidel method with relaxation with making matrix diagonally dominant ?

Categories

Find more on Operating on Diagonal Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!