Answer To: Programming Assignment.pdf
Abr Writing answered on Feb 28 2021
tasks.m
% Clearing the workspace
close;
clear;
clc;
%% Task 1
A = [
-1 3 2 -3 -3 4 2 7
20000 20000 -100000 -50000 10000 0 20000 50000
100000 100000 9990 -40000 -30000 20000 10000 60000
-2 4 1 3 3 0 7 2
1 3 2 7 0 2 2 4
0.000001 0.000003 0.000002 -0.0000004 -0.0000001 0.000001 0.000002 0.000002
10 -5 5 -8 7 4 6 3
2 -5 -2 -14 6 7 2 9
];
b = [
1
-1
2
-2
3
100
-3
4
];
x = gauss_without_pivoting(A, b);
disp(A*x);
disp(A*x-b);
[x_soln,nrow,A_aug]= gauss_with_pivoting(A,b);
disp(A*x_soln);
disp(A*x_soln-b);
% Task 2
A_inv = matrixInverse(A);
disp(A_inv*b);
% Task 3
A = [
1 0 4 -1 2
4 -1 4 11 2
12 4 -3 -3 2
3 3 -1 1 12
-1 3 0 1 1
];
b = [
13
12
3
10
4
];
[x,GaussItr,plotGauss] = gauss_seidel(A, b);
fprintf('Solution of the system is : \n%f\n%f\n%f\n%f\n%f in %d iterations',x,GaussItr);
figure
hold on
plot(1:5:GaussItr,plotGauss(1:5:GaussItr),'LineWidth',2)
text(GaussItr,0.2,'\downarrow')
text(GaussItr,0.3,'Gauss Seidel')
ylabel('Error Value')
xlabel('Number of iterations')
title('Gauss Seidel Method')
hold off
TriangleBackwardSub.m
function C=TriangleBackwardSub(U,b)
% Triangle Matrix Backward Substitution
%
% Solves C = U \ B;
%
% |1| |2 2 1|
% With b = |2| and U = |0 1 4|
% |3| |0 0 3|
%
s=length(b);
C=zeros(s,1);
C(s)=b(s)/U(s,s);
for j=(s-1):-1:1
C(j)=(b(j) -sum(U(j,j+1:end)'.*C(j+1:end)))/U(j,j);
end
TriangleForwardSub.m
function C=TriangleForwardSub(L,b)
% Triangle Matrix Forward Substitution
%
% Solves C = L \ b
%
% |1| |1 0 0|
% With b = |2| and L = |2 1 0|
% |3| |3 4 1|
%
s=length(b);
C=zeros(s,1);
C(1)=b(1)/L(1,1);
for j=2:s
C(j)=(b(j) -sum(L(j,1:j-1)'.*C(1:j-1)))/L(j,j);
end
Linear-Equations.pdf
Linear-Equations
February 27, 2019
1 Task 1
Solving linear system Ax = b using Gaussian elimination without pivoting
function x = gauss_without_pivoting(A, b)
% A is an n by n matrix
% b is an n by k matrix (k copies of n-vectors)
% x is an n by k matrix (k copies of solution vectors)
[n, n] = size(A); % Find size of matrix A
[n, k] = size(b); % Find size of matrix b
x = zeros(n,k); % Initialize x
for i = 1:n-1
m = -A(i+1:n,i)/A(i,i); % multipliers
A(i+1:n,:) = A(i+1:n,:) + m*A(i,:);
b(i+1:n,:) = b(i+1:n,:) + m*b(i,:);
end;
% Use back substitution to find unknowns
x(n,:) = b(n,:)/A(n,n);
for i = n-1:-1:1
x(i,:) = (b(i,:) - A(i,i+1:n)*x(i+1:n,:))/A(i,i);
end
Solving linear system Ax = b using Gaussian elimination with pivoting
Note that this program does not directly change the rows of the augmented matrix. Instead it
uses the nrow vector to keep track of the row changes.
function [x_soln,nrow,A_aug]= gauss_with_pivoting(A,B);
%create the augmented matrix A|B
Aug=[A B];
n=rank(A);
%initialize the nrow vector
for i=1:n
nrow(i)=i;
end
nrow=nrow';
1
for k=1:n-1;
max=0;
index=0;
%find the maximum value in the column under the current checked element and
%return its row position
for j=k:n
if abs(Aug(nrow(j),k))>max
max=abs(Aug(nrow(j),k));
index=j;
end
end
%perform row exchange in the nrow vector
if nrow(k)~=nrow(index)
ncopy=nrow(k);
nrow(k)=nrow(index);
nrow(index)=ncopy;
disp(sprintf ('row changed '))
else
disp(sprintf ('no change '))
end
%Gaussian elimination
for i=(k+1):n
m(nrow(i),k)=Aug(nrow(i),k)/Aug(nrow(k),k);
for j=k:n+1
Aug(nrow(i),j)=Aug(nrow(i),j)-m(nrow(i),k)*Aug(nrow(k),j);
end
end
end
%backward...