1. PRACTICAL
Name- Saloni Singhal
M.Sc. (Statistics) II-Sem.
Roll No: 2046398
Course- MATH-409 L
Numerical Analysis Lab
Submitted To: Dr. S.C. Pandey
2. OBJECTIVE
• To write an m-file to implement Gauss
elimination method without pivoting (Naïve
Gauss Elimination)
for solving system of linear equations.
• To write both script and function files for the same
program.
3. Theory
• Gaussian elimination (row reduction) is an algorithm
for solving systems of linear equations. It consists of
a sequence of operations performed on the
corresponding matrix of coefficients.
• It does not use pivot element hence, also called
Naïve Gauss Elimination (Naïve means
inexperienced)
• It performs forward elimination and then back
substitution.
• This method can also be used to compute the rank
of a matrix, the determinant of a square matrix, and
the inverse of an invertible matrix.
4. Script File
% defines matrix A
A = [2 1 5; 3 5 2;2 1 4]
% defines vector b
b = [8;10;7]
[m n]=size(A);
if m~=n
%method applies on sq matrix
fprintf("incorrect size")
exit
end
%forward elimination
aug=[A b];
for i=1:m-1
for j=i+1:m
a=aug(j,i)/aug(i,i);
aug(j,:)=aug(j,:)-a*aug(i,:);
end
5. Script File
Contd.
end
if aug(n,n)==0
fprintf("0 div err")
exit
end
x(n)=aug(n,n+1)/aug(n,n);
for i=n-1:-1:1
if aug(i,i)==0
fprintf("0 div err")
exit
end %backward substitution
x(i)=(aug(i,n+1)-dot(aug(i,i+1:n),x(i+1:n)))/aug(i,i);
end
6. Function File
function x =guasselmn(A,b)
[m n]=size(A);
if m~=n
fprintf("incorrect size")
exit
aug=[A b]
for i=1:m-1 %forward elimination
for j=i+1:m
a=aug(j,i)/aug(i,i)
aug(j,:)=aug(j,:)-a*aug(i,:)
end
end
7. Program Contd.
if aug(n,n)==0
fprintf("zero division error")
exit
end
x(n)=aug(n,n+1)/aug(n,n)
for i=n-1:-1:1
if aug(i,i)==0
fprintf("zero division error")
exit
end %backward substitution
x(i)=(aug(i,n+1)-
dot(aug(i,i+1:n),x(i+1:n)))/aug(i,i)
end
end
C = dot( A,B ) returns the
scalar dot product of A
and B . Vectors A and B
must have the same
length
9. 2. System with no solution 3. System with infinite solution
10. Time Complexity
10
For the given program it is calculated as:
For line 14= 2(n-1) since loop
For line 16= (n-1)+1 since loop from 1:n-1
For line 18= 2(n-1)(n-2) for nested loop
For line 24= 51(6n-3) since linspace generates 51
values(given) and min/max has time complexity n
For line 36= (6n-3) since loop is from i to n-1
on adding the above dominant value is =O(n2)
Big O notation is the most common metric for calculating time complexity. It
describes the execution time of a task in relation to the number of steps
required to complete it.
We calculate the
no of operations in
respective loops
and function.
Big ‘O’ notation
takes its dominant
value
11. Caveats
• Method may produce inaccurate
results when the terms in the
augmented matrix are rounded off.
• Most practical problems result in
very sparse matrices, sometimes
structured, sometimes
unstructured. If you use Gauss
elimination that sparsity is partially
or totally lost and consequently
calculation times soar.
12. Conclusion
• Gaussian elimination is the classical, and most preferred
method for solving linear systems Ax=b.
• Today's computers are sufficient to solve systems which
model physical, economic, biological, etc. phenomena.
• If scaled, partial pivoting (discussed in the next ppt) is
employed forth which accurate solution is assured.
• Much less computation required it takes π3/3
multiplications and n 3/3 +O(n) operations.