APM1513
ASSIGNMENT 2
SOLUTIONS 2023
Unique number: *******
DUE DATE: 22 AUGUST 2023
APPLIED LINEAR
ALGEBRA
Page 1 of 17
,SOLUTION
octave:1> rand("state",429373610915)
octave:2> rand(1)
ans = 0.63536
SOLUTION
% (a)
A = [2 -1 3; 4 2 -5; 6 3 1];
B =[8; -9; 12];
X = A\B;
printf("\nSolution using A\\B Construct is : \n")
printf("X1=%f, \tX2=%f, \tX3=%f\n\n", X(1,1), X(2,1), X(3,1));
% (b)
A = [10 1 2; 1 10 -3; 2 1 10];
B =[3; 1.5; -9];
X = A\B;
Page 2 of 17
, printf("\nSolution using A\\B Construct is : \n")
printf("X1=%f, \tX2=%f, \tX3=%f\n\n", X(1,1), X(2,1), X(3,1));
%--------------------------------------------------------------------------------------------------------------
--
function GuassSeidel(A,B, convergeVal, maxIteration)
N = length(B);
X = zeros(N,1);
err = ones(N,1);
for i = 1:N
j = 1:N;
j(i) = [];
C = abs(A(i,j));
Check(i,1) = abs(A(i,i)) - sum(B);
if Check(i) < 0
fprintf('The matrix is not strictly diagonally dominant at row %2i\n\n',i)
end
end
iter = 0;
while max(err) > convergeVal
iter = iter + 1;
Z = X;
for i = 1:N
j = 1:N;
j(i) = [];
Xtemp = X;
Xtemp(i) = [];
X(i,1) = (B(i,1) - sum(A(i,j) * Xtemp)) / A(i,i);
end
Xs = X;
err = sqrt((X - Z).^2);
if iter >= maxIteration
break;
end
end
% Printing the ouput
printf("\nSolution using Guass Seidel Model : \n");
printf("X1=%f, \tX2=%f, \tX3=%f, ", Xs(1), Xs(2), Xs(3));
printf("Number of iterations %d\n\n", iter);
end
% part(a) using Guass Seidel
A = [10 1 2; 1 10 -3; 2 1 10];
Page 3 of 17