Question

In: Computer Science

% 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; end

if nargin < 3; maxIter = 500; end

k = 10; p = 1; omega = 1;

for numIter = 1:maxIter

xold = x;

x = feval(func,x,omega);

dx = sqrt(dot(x - xold,x - xold));

if dx < epsilon; return; end

if numIter == k; dx1 = dx; end

if numIter == k + p

omega = 2/(1 + sqrt(1 - (dx/dx1)/(1/p)));

end

end

error("Too many iterations")

Solutions

Expert Solution

ANSWER:

function [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;
    end
    
    if nargin < 3
        maxIter = 500; 
    end
    
    k = 10; p = 1; omega = 1;
    for numIter = 1:maxIter
        xOld = x;
        x = feval(func,x,omega);
        dx = sqrt(dot(x - xOld,x - xOld));
    
        if dx < epsilon
            return;
        end
        
        if numIter == k
           dx1 = dx; 
        end
        
        if numIter == k + p
            omega = 2/(1 + sqrt(1 - (dx/dx1)^(1/p)));
        end
    end
end

Related Solutions

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
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.
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....
A) Use Jacobi or Gauss-Seidel iteration and perform three iterations by hand. B) Use Jacobi or...
A) Use Jacobi or Gauss-Seidel iteration and perform three iterations by hand. B) Use Jacobi or Gauss-Siedel iteration for ten iterations with a MAT-LAB function. * A= [5, -1,0;-1,5,-1;0,-1,5] , B=[9;4;-6]
A) Use Jacobi or Gauss-Seidel iteration and perform three iterations by hand. B) Use Jacobi or...
A) Use Jacobi or Gauss-Seidel iteration and perform three iterations by hand. B) Use Jacobi or Gauss-Siedel iteration for ten iterations with a MAT-LAB function. * A= [10 -2 1;-2 10 -2;-2 -5 10] , B=[9;12;18]
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]
Given the differential equation (ax+b)2d2y/dx2+(ax+b)dy/dx+y=Q(x) show that the equations ax+b=et and t=ln(ax+b) reduces this equation to...
Given the differential equation (ax+b)2d2y/dx2+(ax+b)dy/dx+y=Q(x) show that the equations ax+b=et and t=ln(ax+b) reduces this equation to a linear equation with constant coefficients hence solve (1+x)2d2y/dx2+(1+x)dy /dx+ y=(2x+3)(2x+4)
(a) The n × n matrices A, B, C, and X satisfy the equation AX(B +...
(a) The n × n matrices A, B, C, and X satisfy the equation AX(B + CX) ?1 = C Write an expression for the matrix X in terms of A, B, and C. You may assume invertibility of any matrix when necessary. (b) Suppose D is a 3 × 5 matrix, E is a 5 × c matrix, and F is a 4 × d matrix. Find the values of c and d for which the statement “det(DEF) =...
factoring ax^2+bx+c with Grouping, Box Method, Star Method/Diamond Method/X-method, and Tic-Tac-Toe Method (with a>1)
factoring ax^2+bx+c with Grouping, Box Method, Star Method/Diamond Method/X-method, and Tic-Tac-Toe Method (with a>1)
dx dt =ax+by dy dt =−x − y, 2. As the values of a and b...
dx dt =ax+by dy dt =−x − y, 2. As the values of a and b are changed so that the point (a,b) moves from one region to another, the type of the linear system changes, that is, a bifurcation occurs. Which of these bifurcations is important for the long-term behavior of solutions? Which of these bifurcations corresponds to a dramatic change in the phase plane or the x(t)and y(t)-graphs?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT