Question

In: Advanced Math

Matlab code for Gauss Siedel with solved example in Matlab

Matlab code for Gauss Siedel with solved example in Matlab

Solutions

Expert Solution

% Gauss-Seidel Method in MATLAB

function x = gauss_siedel( A ,B )
disp ( 'Enter the system of linear equations in the form of AX=B')

%Inputting matrix A

A = input ( 'Enter matrix A : \n')
% check if the entered matrix is a square matrix

[na , ma ] = size (A);
if na ~= ma
disp('ERROR: Matrix A must be a square matrix')
return
end

% Inputting matrix B

B = input ( 'Enter matrix B : ')
% check if B is a column matrix

[nb , mb ] = size (B);
if nb ~= na || mb~=1
disp( 'ERROR: Matrix B must be a column matrix')
return
end

% Separation of matrix A into lower triangular and upper triangular matrices

% A = D + L + U

D = diag(diag(A));
L = tril(A)- D;
U = triu(A)- D

% check for convergence condition for Gauss-Seidel method

e= max(eig(-inv(D+L)*(U)));
if abs(e) >= 1
disp ('Since the modulus of the largest Eigenvalue of iterative matrix is not less than 1')
disp ('this process is not convergent.')
return
end

% initial guess for X..?

% default guess is [ 1 1 .... 1]

r = input ( 'Any initial guess for X? (y/n): ','s');
switch r
case 'y'
% asking for initial guess
X0 = input('Enter initial guess for X :\n')
% check for initial guess
[nx, mx] = size(X0);
if nx ~= na || mx ~= 1
disp( 'ERROR: Check input')
return
end
otherwise
X0 = ones(na,1);
end

% allowable error in the final answer

t = input ( 'Enter the error allowed in final answer: ');
tol = t*ones(na,1);
k= 1;

X( : , 1 ) = X0;
err= 1000000000*rand(na,1);% initial error assumption for looping
while sum(abs(err) >= tol) ~= zeros(na,1)
X ( : ,k+ 1 ) = -inv(D+L)*(U)*X( : ,k) + inv(D+L)*B;% Gauss-Seidel formula
err = X( :,k+1) - X( :, k);% finding error
k = k + 1;
  
end

fprintf ('The final answer obtained after %g iterations is \n', k)
X( : ,k)


Related Solutions

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....
Show and explain Gauss-Jordan elimination using Matlab.
Show and explain Gauss-Jordan elimination using Matlab.
solve in MATLAB and screenshot code ?′′ −??′ +??= ???(????−?????)
solve in MATLAB and screenshot code ?′′ −??′ +??= ???(????−?????)
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
This is a Matlab Exercise problem. Please create the Matlab code and figure for the following...
This is a Matlab Exercise problem. Please create the Matlab code and figure for the following problem using problem specifications: Plot x vs y when y=sin(x), y=cos(x), y=sin (2*x), and y=2*sin(x) when x = 1:0.1:10. Use 2 by 2 subplot, sin(x) is in location 1, cos(x) is in location 2, sin(2*x) is in location 3 and 2*sin(x) is in location 4. The plot should have: (1) x label = ‘x value’, y label = ‘y value’, legend ‘y=sin(x)’,’ y=cos(x)’,’ y=sin...
Fill in the blanks in the MATLAB code below.
Fill in the blanks in the MATLAB code below. (Do not type unnecessary words or blank spaces in your response. The correct answers are case-sensitive.) % Consider a row vector v. % Complete the lines of code below to find the average and standard deviation of the elements of vector v such that these two values are assigned to variables M and S, respectively. E = G =
write the code in MATLAB with comments and show the inputs and results of the code...
write the code in MATLAB with comments and show the inputs and results of the code for the question below. Write an .m file in MATLAB, that records audio (you can record your own voice for 20 seconds that was recorded using your phone), then take Fourier transform of the signal (probably FFT).
show the MATLAB Code with comments and Write an .m file in MATLAB, that records audio...
show the MATLAB Code with comments and Write an .m file in MATLAB, that records audio (you can record your own voice for 20 seconds), takes Fourier transform of the signal (probably FFT).
write a matlab code to simulate fiber optics communication system on matlab simulink
write a matlab code to simulate fiber optics communication system on matlab simulink
Implement the steffenson method in matlab. The code must be in MATLAB DOnt answer if you...
Implement the steffenson method in matlab. The code must be in MATLAB DOnt answer if you cannot give correct MATLAB
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT