Scilab/C4/LinearequationsGaussianMethods/English
Title of script: Solving System of Linear Equations using Gauss Elimination and GaussJordan Methods
Author: Shamika
Keywords: System of linear equations, Gaussian Methods



Slide 1  Dear Friends,
Welcome to the Spoken Tutorial on “Solving System of Linear Equations using Gauss Elimination and GaussJordan Methods” 
Slide 2 Learning Objective Slide  At the end of this tutorial, you will learn how to:

Slide 3System Requirement slide  To record this tutorial, I am using Ubuntu 12.04 as the operating system
with Scilab 5.3.3 version 
Slide 4 Prerequisites slide  * To practise this tutorial, a learner should have basic knowledge of Scilab and should know how to solve Linear Equations.

Slide 5 System of Linear Equations  A system of linear equations is a

Slide 6 System of Linear Equations  A system of linear equations can have one of the following conclusions

Slide 7 Gaussian Elimination Method  Given a system of equations

Slide 8 Gaussian Elimination Method  * We write the coefficients of the variables a one to a n

Slide 9 Gaussian Elimination Method

* How do we convert the augmented matrix to an upper triangular form matrix?

Slide 10 Example  * Let us solve this system of equations using Gaussian elimination method

Switch to Scilab and open naivegaussianelimination.sci  * Before we solve the system, let us go through the code for Gaussian elimination method.

Highlight format e comma twenty  * The first line of the code is format e comma twenty.

Point to e  * The letter e within single quotes denotes that the answer should be displayed in scientific notation

Point to twenty  * The number 20 is the number of digits that should be displayed.

Highlight funcprot  * The command funcprot is used to let Scilab know what to do when variables are redefined.

Point to zero  * The argument zero specifies that Scilab need not do anything if the variables are redefined.

No visual clue. This is extra information that is being offered to the listener.  * Other arguments are used to issue warnings or errors if the variables are redefined.

Highlight input  * Next we use the input function.

Highlight input

* It will display a message to the user and get the values of A and B matrices.

Extra information  * The matrices that the user inputs, will be stored in the variables A and B.

Highlight A
Highlight B 
* Here A is the coefficient matrix and B is the righthandside matrix or the constants matrix.

Highlight naivegaussianelimination  * Then we define the function naivegaussianelimination.

Highlight A
Highlight B 
* And we state that A and B are the arguments of the function naivegaussianelimination.

Highlight x  * We store the output in variable x.

Highlight size  * Then we find the size of matrices A and B using the size command.

Highlight n and n one  * Since they are 2dimensional matrices, we use n and n one to store the size of matrix A.

Highlight m one and p  * Similarly we can use m one and p for matrix b.

Highlight
if n ~= n1 error('gaussianelimination  Matrix A must be square'); 
* Then we have to determine if the matrices are compatible with each other and if A is a square matrix.

Highlight
elseif n ~= m1 error('gaussianelimination  incompatible dimension of A & b'); 
* If n and m one are not equal, we display a message incompatible dimension of A and b.

Highlight
C=[A b] 
* If the matrices are compatible, we place matrices A and b in one matrix C.

Highlight
n=size(A,1); for k=1:n1 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 
* The next block of code performs forward elimination.

Highlight
x(n)=b(n)/A(n,n); for i=n1: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 
* Finally we perform back substitution.

Click on Execute and select Save and Execute  * Let us save and execute the file.

Show Scilab Console  * Switch to Scilab console to solve the example.

Show Scilab Console  * On the console, we have a prompt to enter the value of the coefficient matrix.

Type
[3.41 1.23 1.09;2.71 2.14 1.29;1.89 1.91 1.89] 
* Type square bracket three point four one space one point two three space minus one point zero nine semi colon two point seven one space two point one four space one point two nine semi colon one point eight nine space minus one point nine one space minus one point eight nine close square bracket.

Type
[4.72;3.1;2.92] 
* The next prompt is for matrix b.

Type
naivegaussianelimination(A,b) 
* Then we call the function by typing

Show answer on Scilab console  * The solution to the system of linear equations is shown on Scilab console.

* Next we shall study the Gauss Jordan method.
 
Slide 11 – Gauss Jordan Method  In Gauss – Jordan Method

Slide 12 GaussJordan Method  * Then we divide the diagonal element and corresponding element of right hand side element, by the diagonal element.

Slide 13 Example

* Let us solve this example using Gauss Jordan Method.

Switch to Scilab console and open GaussJordan Elimination.sci  * Let us look at the code first.

Highlight
format 
* The first line of the code uses format function to specify the format of the displayed answers.

Highlight
'e' 
* The parameter e specifies the answer should be in scientific notation.

Highlight 20  * 20 denotes that only 20 digits should be displayed.

Highlight
A=input("Enter the coeffiecient matrix : ") b=input("Enter the righthand side matrix : ") 
* Then we get the A and b matrix using the input function.

Highlight
function [x] = GaussJordanElimination( A, b ) 
* We define the function Gauss Jordan Elimination with input arguments A and b and output argument x.

Highlight
[m, n] = size( A ) 
* We get the size of matrix A and store it in m and n

Highlight
[r, s] = size( b ) 
* Similarly we get the size of matrix b and store it in r and s

Highlight
if ( m <> r ) then error("Error: matrix A and vector b are incompatible sizes") end 
* If the sizes of A and b are not compatible, we display an error on the console using error function.

Highlight
for k = 1 : 1 : m indices = [ 1 : 1 : k1, k+1 : 1 : m ]
// of the pivoting row to get a zero for i = indices multiplier = C(i, k) / C(k,k) for j = k+1 : n C(i, j) = C(i, j)  multiplier * C(k, j) end end end 
* Then we perform row operations to get diagonal form of the matrix.

Highlight
x = zeros( m, s ) 
* Then we create a matrix of zeros called x with m rows and s columns.

Highlight
for i = 1 : 1 : m for j = 1 : 1 : s x(i, j) = C(i, m+j) / C(i, i) end end 
Once we have the diagonal form,

Highlight
x(i, j) = C(i, m+j) / C(i, i) 
* We store the value of each variable in x.

Highlight
return x 
* Then we return the value of x.

Highlight
endfunction 
* Finally we end the function.

Click on Execute and select Save and Execute  * Now let us save and execute the function.

Type the following
[0.7, 1725;0.4352,5.433]

* The prompt requires us to enter the value of matrix A.

Type the following
[1739;3.271] 
* The next prompt is for vector b.

Type the following
GaussJordanElimination(A,b)

* Then we call the function by typing

Show answers on Scilab console  * The values of x one and x two are twenty and one respectively.

Slide 14 Summary  <Pause>
Let us summarize. In this tutorial, we have learnt to:
equations 
Show Slide 16
Title: About the Spoken Tutorial Project

* About the Spoken Tutorial Project

Show Slide 17
Title: Spoken Tutorial Workshops The Spoken Tutorial Project Team

The Spoken Tutorial Project Team

Show Slide 18
Title: Acknowledgement

* Spoken Tutorial Project is a part of the Talk to a Teacher project

This is Bella Tony from the FOSSEE project, IIT Bombay signing off. Thanks for joining. 