MATLAB PLEASE MATLAB PLEASE PLEASE SOLVE ONLY b a) Develop a program using a lib
ID: 3198759 • Letter: M
Question
MATLAB PLEASE
MATLAB PLEASE
PLEASE SOLVE ONLY b a) Develop a program using a library or a package of your choice for Gauss Seidel Method with x(0)-0 to approximate the solution to the given linear system with an error tolerance ?,-10-3 in the maximum magnitude norm (1X10) 4x +x2-3+x2 PLEASE SOL xi + 4x2-X3-X4 =-1 -xi-x2 +5x3 +x4=0 xi-x2 +x3 +3x4 =1 PLEASE SOLVE ONLY b 4x1 + x2-x3 + x4 =-2 b) Solve the same set of equations with the same initial conditions and error tolerance using SOR Method with w= 1.1.Explanation / Answer
%for SOR method
function x = SOR( A ,B )
disp ( ' Enter system of equations in the form of AX=B')
% Calling matrix A
A = input ( ' Enter matrix A : ')
% check for matrix A
% it should be a square matrix
[na , ma ] = size (A);
if na ~= ma
disp('ERROR:matrix A should be a square matrix')
return
end
% calling matrix B
B = input ( ' Enter matrix B : ')
% check for matrix B
[nb , mb ] = size (B);
if nb ~= na || mb~=1
disp( 'ERROR:input error..pls re-enter data')
return
end
w=input('Enter the relaxation parameter ');
% A = D + L + U
D = diag(diag(A));
L = tril(A)- D;
U = triu(A)- D;
% check for convergence of system
e= max(eig(inv( D+w*L) * ( D*(1-w) - w*U)));
if abs(e) >= 1
disp ('Since the modulus of largest eigen value of iterative matrix is not less than 1')
disp ('This process is not convergent. Please try some other process.')
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('pls enter initial guess for X : ')
% check for intial guess
[nx, mx] = size(X0);
if nx ~= na || mx ~= 1
disp( 'ERROR: pls check input')
return
end
otherwise
X0 = ones(na,1);
end
%allowed error in final answer
t = input ( 'enter the error allowed in final ans: ');
tol = t*ones(na,1);
k= 1;
X( : , 1 ) = X0;
err= 1000000000*rand(na,1); %intial error assumtion for looping
while sum(abs(err) >= tol) ~= zeros(na,1)
X ( : ,k+ 1 ) = inv(D+w*L) * ( D*(1-w) - w*U)*X( : ,k) + inv(D+ w*L)*B;% SOR formula
err = X( :,k+1) - X( :, k);% finding error
k = k + 1;
end
fprintf (' The final ans obtaibed after %d itaration which is ', k)
X( : ,k)
While giving input to the program, the standard MATLAB syntaxes must be obeyed. Here’s a sample output of the MATLAB program for SOR method:
Matrix A = [4 1-1 1;1 4 -1 -1;-1 -1 5 1; 1 -1 1 3]
Matrix B= [-2 -1 0 1]
Enter the relaxation paramter = 1.1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.