(How do i make this work in the gaussseidel.m matlab function) 1. Edit the Gauss
ID: 3168330 • Letter: #
Question
(How do i make this work in the gaussseidel.m matlab function)
1. Edit the Gauss-Seidel .m file to illustrate the convergence of three unknown variables in a table.
Run your function for the following system with a default value of es.
6x1+7x3=50
8x1+3x2+x3=45.5
2x1+4x2-x3=4
Your function should print the following information:
Iteration x1 x2 x3 max error
Label the output Table 1 and save it to a word document. (Hint: You may have to make your system diagonally dominant.)
Please check your solution using the x=A commend to ensure you did not make a mistake.
2. Reconsider Problem 3 from HW 8. (Pasted below for your convenience). .
Edit the Gauss-Seidel .m file to illustrate the convergence of three unknown variables in a table.
Your function should print the following information:
Iteration u1 u2 u3 max error
Label the output Table 2 and save it to a word document.
(Matlab function)
function x = GaussSeidel(A,b,es,maxit)
% GaussSeidel: Gauss Seidel method
% x = GaussSeidel(A,b): Gauss Seidel without relaxation
% input:
% A = coefficient matrix
% b = right hand side vector
% es = stop criterion (default = 0.00001%)
% maxit = max iterations (default = 50)
% output:
% x = solution vector
if nargin<2,error('at least 2 input arguments required'),end
if nargin<4|isempty(maxit),maxit=50;end
if nargin<3|isempty(es),es=0.00001;end
[m,n] = size(A);
if m~=n, error('Matrix A must be square'); end
C = A;
for i = 1:n
C(i,i) = 0;
x(i) = 0;
end
x = x';
for i = 1:n
C(i,1:n) = C(i,1:n)/A(i,i);
end
for i = 1:n
d(i) = b(i)/A(i,i);
end
iter = 0;
while (1)
xold = x;
for i = 1:n
x(i) = d(i)-C(i,:)*x;
if x(i) ~= 0
ea(i) = abs((x(i) - xold(i))/x(i)) * 100;
end
end
iter = iter+1;
if max(ea)<=es | iter >= maxit, break, end
end
Explanation / Answer
clc;
clear all;
% 6x1+7x3=50
% 8x1+3x2+x3=45.5
% 2x1+4x2-x3=4
A = [ 8 3 1; 2 4 -1;6 0 7];
b = [45.5; 4; 50];
% error tolerance
tol = 0.0005;
%initial guess:
x0 = zeros(3,1);
maxiter=1000;
lambda=1;
n=length(x0);
x=x0;
error=1;
iter = 0;
while (error>tol & iter<maxiter)
xold=x;
for i=1:n
I = [1:i-1 i+1:n];
x(i) = (1-lambda)*x(i)+lambda/A(i,i)*( b(i)-A(i,I)*x(I) );
end
error = norm(x-xold)/norm(x);
iter = iter+1;
end
x_siedal=x
u=inv(A)*b
x_siedal =
6.0015
-1.5013
1.9987
u =
6.0000
-1.5000
2.0000
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.