This repository has been archived by the owner on Jul 11, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGaussElim.sci
57 lines (51 loc) · 1.95 KB
/
GaussElim.sci
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//***************************************************************************
// Gauss Elimination Method for solving the system of linear equations ****
// AX = b ****
// NPDE-TCA UG-Level Workshop at IMA Bhubaneshwar ****
// By Manas,FOSSEE,IITB ****
//***************************************************************************
A=[10 1 1; 2 10 1; 1 1 5]
b=[12;13;7]
function [x] = GaussElim(A,b)
//This function obtains the solutions to the system of linear equatons
//A*x=b,given the matrix coefficients A and the right-hand side vector,b
//Gaussian elimination
// Inputs:
//A - matrix of real numbers of size nxn
//b - a right-hand side column vector.
// Output:
//x - the solution to the given equation.
//Retrieve the size of matrix A and vector b
[n,n1]=size(A);
[m1,p]=size(b);
//Check that size of A and size of b are compatible
if n ~= n1
error('gaussianelimination - Matrix A must be square');
abort;
elseif n ~= m1
error('gaussianelimination - incompatible dimension of A & b');
abort;
end;
//Create the augmented system C
C=[A b];
//Forward Elimination
n=size(A,1);
for k=1:n-1
for i=k+1:n
factor=A(i,k)/A(k,k);
for j=k+1:n
A(i,j)=A(i,j)-factor*A(k,j);
end
b(i)=b(i)-factor*b(k);
end
end
//Back Substitution
x(n)=b(n)/A(n,n);
for i=n-1:-1:1
sum=0;
for j=i+1:n
sum=sum+A(i,j)*x(j);
end
x(i)=(b(i)-sum)/A(i,i);
end
endfunction