For this problem, you don\'t really need to write a MATLAB generalized function
ID: 3863266 • Letter: F
Question
For this problem, you don't really need to write a MATLAB generalized function to calculate the solution for a system for equations using Gauss Seidel method (If you can that's even better). What you can alternatively do is solve the three equations in problem 1 for x1, x2, and x3 and put them within a for loop. You can create dummy variables to store the previous iteration values of x1, x2, and x3 to calculate the error. Write a user-defined MATLAB function that solves a system of n linear equations, [a][x] = [b], with the Gauss-Seidel method. For the function name and arguments use x = Gauss Seidel(a, b), where a is the matrix of coefficients, b is the right-hand-side column of constants, and x is the solution. Use Gauss Seidel to solve problems 1. This is problem 1 as a reference, but solve problem 4. Carry out the first three iterations of the solution of the following system of equations using the Gauss Seidel iterative method For the first guess of the solution, take the value of all the unknowns to be zero. 8x_1 + 2x_2 + 3x_3 = 51 2x_1 + 5x_2 + x_2 + 6x_3 = 20Explanation / Answer
(4).function x=GaussSeidel(A,b,y,N) %% method for gaussseidel
D=diag(A); %% diagnol
A=A-diag(D);
%% inverse
D=1./D;
%%length
n=length(y);
y=y(:);
x=zeros(n,NumIters);
for j=1:NumIters
for k=1:n
x(k)=(b(k)-A(k,:)*x)*D(k);
end
y(:,j)=y;
end
1Ans:Enter the matrix as 8 2 3 51
2 5 1 23
-3 1 6 20
follow the code
function x = gauss_siedel( A ,B )
disp ( 'Input linear equations in the form of AX=B')
%Input matrix A
A = input ( 'input matrix A : ')
% check if the entered matrix is a square matrix
[na , ma ] = size (A);
if na ~= ma
disp('ERROR: Matrix A must be square matrix') %% square matrix only
return
end
% Input matrix B
B = input ( 'Enter matrix B : ')
[nb , mb ] = size (B);
if nb ~= na || mb~=1
disp( 'ERROR: Matrix B must be column matrix') %% must be column matrix
return
end
% A = D + M + U
D = diag(diag(A));
L = tril(A)- D;
U = triu(A)- D
e= max(eig(-inv(D+M)*(U)));
if abs(e) >= 1
disp ('Since the modulus of the largest Eigen value of iterative matrix is not less than 1')
disp ('this process is not convergent.')
return
end
r = input ( 'Any initial guess for X? (y/n): ','s');
switch r
case '1'
X0 = input('Enter initial guess for X : ')
% 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
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+M)*(U)*X( : ,k) + inv(D+M)*B; %%its Gauss Seidel formula
err = X( :,k+1) - X( :, k);%%for find error
k = k + 1;
end
fprintf ('The final answer after %g iterations is ', k)
X( : ,k)
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.