Question

In: Advanced Math

how can I change the Gauss-Seidel method to SOR method code in Matlab? The question has...

how can I change the Gauss-Seidel method to SOR method code in Matlab?

The question has shows that In implementing SOR method in MATLAB, one should not calculate Tw and cw by formulas Tw = (D -wL)^(-1)[(1-w)D+wU)] and Cw = w(D-wL)^(-1)b , where w stands for omega and using MATLAB's built-in inv function, since this function requires O(n^3) flops and therefore the
whole matter loses its point.

I have tried for many times but I can't get the correct answers. Please help Thanks :)

Execute code:

function x = GaussSeidel(A,b,x0,tol,kmax,output)
% This function returns an approximate solution of a
% linear system A*x=b, obtained by the Gauss-Seidel method.
%
% x0 is an initial approximation,
% tol is tolerance,
% kmax is the maximum number of iterations,
% output is a parameter which regulates displays:
% 0 - no display,
% 1 - some display,
% 2 - detailed display.
%
% Input: A, n by n matrix,
% b, n by 1 column,
% x0, initial approximation,
% tol, tolerance,
% kmax, maximum number of iterations,
% output, a parameter which regulates displays.
%
% Output: x, a solution.

[m,n]=size(A);
if m~=n,
error('A is not square');
end
m=length(b);
if m~=n,
error('Dimensions of A and B do not agree');
end
% Write A as D-L-U
[D,L,U] = DLU_decomposition(A);
% Calculate the matrix T and the vector c
T = L+U;
c=b;
for i=1:n,
T(i,:) = T(i,:)/D(i,i); % T = D^(-1)*(L+U)
c(i) = b(i)/D(i,i); % c = D^(-1)*b
end
% Calculate x
x=x0; % initial approximation
if output >= 2
disp(' k TOL x1 x2');
end
for k=1:kmax,
xprev=x;
for i=1:n
x(i)=c(i);
for j=1:n
if j~=i
x(i)=x(i)+T(i,j)*x(j);
end
end
end
TOL=norm(x-xprev,inf);
if output >= 2
out=[k, TOL, x(1), x(2)];
disp(out);
end
if TOL<tol,
if output >= 1
disp('The Gauss-Seidel method has converged.');
end
break
end
end
if output >= 1
if TOL>=tol
disp('The Gauss-Seidel method did not converge.');
end
s=sprintf('The norm of residual vector A*x-b is %e.',norm(A*x-b));
disp(s);
end
end

Solutions

Expert Solution


%%Matlab code for Gauss Siedel Method
clear all
close all

%A_matrix is the Coefficient Marix A,b_matrix is the Result Matrix b,
A_matrix=[3 -1 0 0 0 1/2; -1 3 -1 0 1/2 0 ;0 -1 3 -1 0 0; 0 0 -1 3 -1 0; 0 1/2 0 -1 3 -1; 1/2 0 0 0 -1 3];
b_matrix=[5/2;3/2;1;1;3/2;5/2];
conv_es=10^-12;
w=1.1;
%exact solution
x_exact=[1;1;1;1;1;1];
x0=[0 0 0 0 0 0];
%displaying the matrix
fprintf('The A matrix is \n')
disp(A_matrix)

fprintf('The b matrix is \n')
disp(b_matrix)


%result for Gauss Siedel method
[x_g,error_gauss]=Gauss_method(A_matrix,b_matrix,x0,conv_es);

%result for Gauss Jordan method
[x_sor,error_sor]=SOR(A_matrix,b_matrix,x0,w,conv_es);

%Printing the result
fprintf('\n\tThe result using Gauss Siedel Method after 7 iterations is\n');
disp(x_g)


%Printing the result
fprintf('\n\tThe result using SOR Method for w=1.1 after 7 iterations is\n');
disp(x_sor')

fprintf('Printing the error for each method\n')
fprintf('\tItr \t Gauss \t SOR\n')
%Printing the error

for i=1:length(error_sor)
     fprintf('\t%d\t%f\t%f\n',i,error_gauss(i),error_sor(i))
end

%Function for Gauss Siedel method
function [x,error]=Gauss_method(A,b,x0,conv)
%Jacobi method
ea=1;it=0;xx=ones(length(b),1);
while ea>=conv
    it=it+1;
    for i=1:length(b)
        s=0;
        for j=1:length(b)
            if i~=j
                s=s+A(i,j)*x0(j);
            end
          
        end
         x0(i)=(b(i)-s)/A(i,i);
    end
     if it==8
        break
     end
    if it==1
        ea=10;
    else
        ea = norm(x1 - x0', 'inf');
    end

    error(it) = abs(norm(xx - x0'));
    x1=x0;
end
x=x0';
end


%%Matlab function for Gauss Siedel SOR Method
function [x,error]=SOR(A,b,x0,lambda,conv)
%inputs are A=matrix A; b=result vector, x0=initial guess
%maxit=maximum iteration;conv=error convergence
%outputs are x=solution matrix, it=maximum iteration; lambda=w for sor
%Gauss Siedel method
ea=10;k=0; xx=ones(length(b),1);

while ea>=conv
    k=k+1;
    for i=1:length(b)
        s=0;
        for j=1:length(b)
            if i~=j
                s=s+A(i,j)*x0(j);
            end         
        end
        x11=((lambda*(b(i)-s))/A(i,i))+(1-lambda)*x0(i);
        x0(i)=x11;   
    end
    if k==1
        ea=10;
    else
        ea = norm(x1 - x0', 'inf');
    end
    if k==8
        break
    end
  
    x1=x0';
    error(k)=norm(xx - x1, 'inf');
end
x=x0;
end

%%%%%%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%%%%%%%%


Related Solutions

QUESTION: USING MATLAB, Carry out three iterations of the Gauss-Seidel method, starting from the initial vector...
QUESTION: USING MATLAB, Carry out three iterations of the Gauss-Seidel method, starting from the initial vector Use the  ,  and  norm to calculate the residual error after each iteration, until all errors are below 0.0001. [Use 5 decimal place accuracy in you calculations]
Matlab code for Gauss Siedel with solved example in Matlab
Matlab code for Gauss Siedel with solved example in Matlab
The Gauss-Seidel method as an iterative technique often refers to an improved version of the Jacobi...
The Gauss-Seidel method as an iterative technique often refers to an improved version of the Jacobi method, since the Gauss-Seidel method generally achieves a faster convergence. Describe the difference between the Gauss-Seidel and Jacobi methods.
Use the Gauss-Seidel method (a) without relaxation and (b) with relaxation (l 5 0.95) to solve...
Use the Gauss-Seidel method (a) without relaxation and (b) with relaxation (l 5 0.95) to solve the following system to a tolerance of es 5 5%. If necessary, rearrange the equations to achieve convergence. 23x1 1 x2 1 12x3 5 50 6x1 2 x2 2 x3 5 3 6x1 1 9x2 1 x3 5 40
design a program that solves matrices by the method of gauss-seidel use the object-oriented language c...
design a program that solves matrices by the method of gauss-seidel use the object-oriented language c ++
how to write coding in matlab using gauss elimination method for equation 10a + 50b +...
how to write coding in matlab using gauss elimination method for equation 10a + 50b + 20c + 10d = 100 5a + 15b + 75c - 25d=200 25a -15c - 5d = 300 10a + 20b - 30c + 100d = 400
How can I code this problem in MATLAB: a) Find the approximations to within 10-4 to...
How can I code this problem in MATLAB: a) Find the approximations to within 10-4 to all real zeros of the following polynomials using Newton's method.? f(x)=x3 - 2*x2- 5. b) Find approximations to within 10-5 to all the zeros of each of the following polynomials by first finding the real zeros using Newton’s method and then reducing to polynomials of lower degree to determine any complex zeros. f(x)=x4 + 5x3 - 9*x2 - 85*x - 136.
This is a code for a bouncing ball on an 8X8 LED. How can i change...
This is a code for a bouncing ball on an 8X8 LED. How can i change the code to make it a ping pong game against AI by adding 1 potentionmeter to control it? #include <TimerOne.h>//this is a library that uses timer 1 of the arduino to trigger interrupts in certain time intervals //This defines a matrix defining a smiley face for the 8x8 LED matrix display #define BALL { \ {1, 0, 0, 0, 0, 0, 0, 0}, \...
% Solves Ax = b by Gauss-Seidel method with relaxation. % USAGE: [x,numIter,omega] = gaussSeidel(func,x,maxIter,epsilon) %...
% Solves Ax = b by Gauss-Seidel method with relaxation. % USAGE: [x,numIter,omega] = gaussSeidel(func,x,maxIter,epsilon) % INPUT: % func = handle of function that returns improved x using % x = starting solution vector % maxIter = allowable number of iterations (default is 500) % epsilon = error tolerance (default is 1.0e-9) % OUTPUT: % x = solution vector % numIter = number of iterations carried out % omega = computed relaxation factor if nargin < 4; epsilon = 1.0e-9;...
matlab code that performs overlap add method.
matlab code that performs overlap add method.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT