SciLab Manual - UE18MA251 PDF
SciLab Manual - UE18MA251 PDF
SCILAB MANUAL
LINEAR ALGEBRA- UE18MA251
IV Semester - B. Tech
January-May 2020
Compiled by
1
Contents
SCILAB SYLLABUS ......................................................................................5
ABOUT SCILAB.............................................................................................6
Overview of Scilab .......................................................................................6
How to get and install Scilab........................................................................7
The general environment and the console....................................................7
The graphics window ...................................................................................9
Programming ................................................................................................9
Variables, assignment and display .............................................................10
CLASS NUMBER - 1 ....................................................................................14
Topic: Gaussian Elimination ......................................................................14
Practice Problems .......................................................................................16
CLASS NUMBER - 2 ....................................................................................17
Topic: LU decomposition of a matrix ........................................................17
Practice Problems .......................................................................................19
CLASS NUMBER – 3 ...................................................................................20
Topic: The Gauss - Jordan method of calculating A-1 ..............................20
Practice Problems: ......................................................................................22
CLASS NUMBER – 4 ...................................................................................23
Topic: Span of the Column Space of A .....................................................23
Practice Problems: ......................................................................................24
CLASS NUMBER –5 ....................................................................................25
Topic: The Four Fundamental Subspaces ..................................................25
Practice Problems: ......................................................................................27
CLASS NUMBER - 6 ....................................................................................28
Topic: Projections by Least Squares ..........................................................28
2
Practice Problems: ......................................................................................29
CLASS NUMBER -7 .....................................................................................30
Topic: The Gram- Schmidt Orthogonalization ..........................................30
Practice Problems: ......................................................................................31
CLASS NUMBER - 8 ....................................................................................32
Topic: Eigen values and Eigen vectors of a given square matrix ..............32
Practice Problems: ......................................................................................33
CLASS NUMBER - 9 ....................................................................................34
Topic: Numerically Largest Eigen value of A - The Power Method ........34
Practice Problems: ......................................................................................37
3
Table of Figures
Figure I - Gaussian Elimination - Example 1................................................................ 14
Figure II - Gaussian Elimination – Output 1 ................................................................. 15
Figure III - Gaussian Elimination - Example 2 ............................................................. 15
Figure IV - Gaussian Elimination - Output 2 ................................................................ 16
Figure V - LU decomposition of a matrix - Example 3 ................................................ 17
Figure VI - LU decomposition of a matrix - Output 3 .................................................. 18
Figure VII - LU decomposition of a matrix - Example 4 ............................................. 18
Figure VIII - LU decomposition of a matrix - Output 4 ............................................... 19
Figure IX - The Gauss - Jordan method of calculating A-1- Example 5 ....................... 20
Figure X - The Gauss - Jordan method of calculating A-1- Output 5 ............................ 21
Figure XI - The Gauss - Jordan method of calculating A-1- Example 5A .................... 21
Figure XII - The Gauss - Jordan method of calculating A-1- Output 5A ..................... 22
Figure XIII - Span of the Column Space of A - Example 6.......................................... 23
Figure XIV - Span of the Column Space of A - Output 6............................................. 24
Figure XV - The Four Fundamental Subspaces - Example 7 ....................................... 25
Figure XVI - The Four Fundamental Subspaces - Output 7 - 1 .................................... 26
Figure XVII - The Four Fundamental Subspaces - Output 7 - 2 .................................. 26
Figure XVIII - Projections by Least Squares - Example 8 ........................................... 28
Figure XIX - Projections by Least Squares - Output 8 ................................................. 29
Figure XX - The Gram- Schmidt Orthogonalization - Example 9................................ 30
Figure XXI - The Gram- Schmidt Orthogonalization - Output 9 ................................. 31
Figure XXII - Eigen values and Eigen vectors of a given square matrix - Example 10
....................................................................................................................................... 32
Figure XXIII - Eigen values and Eigen vectors of a given square matrix - Output 10 33
Figure XXIV - Numerically Largest Eigen value of A - The Power Method - Example
11 ................................................................................................................................... 34
Figure XXV - Numerically Largest Eigen value of A - The Power Method - Output 11
- 1................................................................................................................................... 35
Figure XXVI - Numerically Largest Eigen value of A - The Power Method - Output
11 - 2.............................................................................................................................. 36
Figure XXVII - Numerically Largest Eigen value of A - The Power Method - Output
11 – 3 ............................................................................................................................. 37
4
SCILAB SYLLABUS
Gaussian Elimination , The LU Decomposition, Inverse of a Matrix by the Gauss-
Jordan Method, The Span of Column Space of a Matrix, The Four Fundamental
Subspaces, Projections by Least Squares, The Gram-Schmidt Orthogonalization, Eigen
values and Eigen Vectors of a Matrix, The Largest Eigen Value of a Matrix by the Power
Method.
Lesson Plan:
1 Gaussian Elimination
2 The LU Decomposition
5
ABOUT SCILAB
Overview of Scilab
-->
Just type a command and press the Enter key (Windows and Linux) or the Return key
(Mac OS X) in the keyboard to obtain the corresponding result.
7
The menu bar
The menus listed below are particularly useful.
Applications
• The command history allows us to find all the commands from previous
sessions to the current session.
• The variables browser allows us to find all variables previously used during
the current session.
Edit
Preferences (in Scilab menu under Mac OS X) allows us to set and customize colours,
fonts and font size in the console and in the editor, which is very useful for screen
projection. Clicking on Clear Console clears the entire content of the console. In this
case, the command history is still available and calculations made during the session
remain in memory. Commands that have been erased are still available through the
keyboard’s arrow keys.
The Editor
Typing directly into the console has two disadvantages: it is not possible to save the
commands and it is not easy to edit multiple lines of instruction. The editor is the
appropriate tool to run multiple instructions.
To open the editor from the console, click on the first icon in the toolbar or on
Applications SciNotes in the menu bar. The editor opens with a default file named
“Untitled 1 “.
Saving
The extension “.sce “at the end of a file name will launch automatically Scilab when
opening it (except under Linux and Mac OS X).
8
• Execute “…file with no echo “(Ctrl+Shift+E under Windows and Linux, Cmd+Shift+
E under Mac OS X): the file is executed without writing the program in the console
(saving the file first is mandatory).
• Execute “… file with echo “(Ctrl+L under Windows and Linux, Cmd+L under Mac
OS X): rewrites the file into the console and executes it.
• Execute “…until the caret, with echo “(Ctrl+E under Windows and Linux, Cmd+E
under Mac OS X): rewrites the selection chosen with the mouse into the console and
executes it or executes the file data until the caret position defined by the user.
Standard copy/paste can also be used.
A graphics window opens automatically when making any plot. It is possible to plot
curves, surfaces and sequences of points.
Online help
To access the online help, click on ? Scilab Help in the menu bar, or type in the
console:
help
To get help with any function, type help in the console followed by the name of the
appropriate function. For example:
help sin
Programming
In the examples given in this manual, any line preceded by “ --> “ is a command, the
other lines are the returns from commands (calculation results, error messages…). Do
not write “ --> “ in the editor. They are introduced here to make the distinction between
command lines and calculation results as it is displayed in the console after
copying/pasting. When presented in a table (without “ > “ and no calculation result), the
commands are depicted exactly as they should be typed in the editor.
9
Variables, assignment and display
Variables
Scilab is not a computer algebra system. It calculates only with numbers. All
calculations are done with matrices, although this may go unnoticed. Even if the concept
of matrices is unknown, vectors and sequences of numbers can explain it, as they are,
in fact, matrices of dimension 1 × nor n × 1 and a number is itself a matrix of dimension
1 × 1. Variables do not need to be declared in advance, but any variable must have a
value. For example, obtaining the value of a variable which has not been given a value
produces an error.
-->a
! --error 4
Undefined variable: a
If a value is assigned to the variable a, there is no longer an error:
--> a=%pi/4
a=
0.7853982
--> a
a=
0.7853982
Variables may take any name that is not already defined by the system:
--> Piby2=%pi/2
Piby2 =
1.5707963
The result of a calculation that is not assigned to a variable is automatically assigned
to the variable called ans:
-->3*(4-2)
ans =
6.
-->ans
ans =
6.
10
Functions
Functions are the easiest and most natural way to make computations from variables a
nd for obtaining results from variables.
A function definition begins with function and ends with endfunction.
Requesting the assignment of a variable
Assignment is made easily using the “= “operator.
Display
Writing
Typing the name of a variable displays its value, except when there is “; “at the end of
the command line.
Brackets
Matrices are defined using square brackets.
As mentioned before, matrix computation is the basis of calculations in Scilab. A spac
e or comma is used to separate columns and semicolons are used to separate rows.
To define a column vector and obtain a column display:
-->v=[3;-2;5]
v=
3.
- 2.
5.
To define a row vector and obtain a row display:
--> v = [3,-2,5]
v=
3. - 2. 5.
To define a matrix and obtain a tabular display:
--> m = [1 2 3;4 5 6;7 8 9]
m=
1. 2. 3.
4. 5. 6.
7. 8. 9.
11
disp function
The disp must always be used with parentheses. With the vector v previously defined:
-->v(2)
ans =
- 2.
-->disp(v(2))
- 2.
To display a string (usually a sentence), put it between quotes:
-->disp("The solution is") will display the following
The solution is
Accessing elements
Square braces are used to define matrices. A space or a comma is used to switch from
one column to another and semicolons are used to switch from one line to another.
-->m= [1 2 3;4 5 6]
m=
1. 2. 3.
4. 5. 6.
Parentheses are used to access elements or to modify them.
-->m(2,3)
ans =
6.
-->m(2,3)=23
m =
1. 2. 3.
4. 5. 23.
The “ : “ operator is used to designate all the rows or all columns of a matrix.
To view the second row of the matrix m, type:
-->m(2,:)
ans =
4. 5. 23.
To view the third column:
-->m(:,3)
ans =
3.
23.
12
To obtain the transpose of a matrix or a vector use the single quote symbol “ ' “:
-->m'
ans =
1. 4.
2. 5.
3. 23.
Operations
To solve the linear system AX = Y, in which A is a square matrix, use the backslash
“\ “
X = A \ Y.
Be cautious, the operation Y / A will give
(only if the dimensions are correct) another result, that is to say the matrix Z for which
Z A = Y.
Length
The length function returns the number of coordinates for a vector. The size function
returns the dimensions (rows, columns) for a matrix.
-->U=[1:10]
U=
1. 2. 3. 4. 5. 6. 7. 8. 9. 10.
-->length(U)
ans =
10.
-->m=[1 2 3;4 5 6];
-->size(U)
ans =
2. 3.
13
CLASS NUMBER - 1
Topic: Gaussian Elimination
Procedure: Given a system of n equations in n unknowns we use the method of
pivoting to solve for the unknowns. The method starts by subtracting multiples of the
first equation from the other equations. The aim is to eliminate the first unknown from
the second equation onwards. We use the coefficient of the first unknown in the first
equation as the first pivot to achieve this. At the end of the first stage of elimination,
there will be a column of zeros below the first pivot. Next, the pivot for the second stage
of elimination is located in the second row second column of the system. A multiple of
the second equation will now be subtracted from the remaining equations using the
second pivot to create zeros just below it in that column. The process is continued until
the system is reduced to an upper triangular one. The system can now be solved
backward bottom to top.
Solution:
14
Output:
15
Output:
Practice Problems
Solve the following system of equations by Gaussian Elimination. Identify the pivots
in each case.
1. 2x + 5y + z = 0 , 4x + 8y + z = 2, y –z = 3
2. 2x + 3y + z = 8 , 4x + 7y + 5z = 20, -2y + 2z = 0
3. 2x – 3y = 3, 4x – 5y + z = 7, 2x – y – 3z = 5.
16
CLASS NUMBER - 2
Topic: LU decomposition of a matrix
Procedure: Given a square matrix A, we can factorize A as a product of two matrices
L and U. The matrix U is upper triangular with pivots on the main diagonal and the
matrix L is lower triangular with 1’s on the main diagonal and the multipliers in their
respective positions. The factorization is carried out using the method of Gaussian
Elimination.
Solution:
17
Output:
18
Output:
Practice Problems
1. Factorize the following matrices as A = LU
2 3 1 2 3 0
i) A= 4 7 5 ii) A= 4 5 1
1 2 2 2 1 3
19
CLASS NUMBER – 3
Topic: The Gauss - Jordan method of calculating A-1
Procedure: To find the inverse of a non-singular matrix A, we begin with an
augmented system [ A I ] where I is an identity matrix of the size same as that of A
and reduce A to an upper triangular system by Gaussian Elimination. The system is
further reduced to a diagonal one with pivots on the main diagonal and finally to
[ I A-1 ].
1 1 1
Example 5: Find the inverse of the matrix A = 4 3 1
3 5 3
by Gauss – Jordan method.
Solution:
20
Output:
Alternate Solution:
21
Output:
Practice Problems:
1. Find the inverse of the following matrices:
1 0 0 2 1 0 0 0 1
1 1 1 1 2 1 0 1 1
0 0 1 0 1 2 1 1 1
22
CLASS NUMBER – 4
Topic: Span of the Column Space of A
Procedure: Given a matrix A, we reduce it to an upper triangular form using Gaussian
Elimination. The columns that contain the pivots span the column space of A.
Example 6: Identify the columns that are in the column space of A where
4 5 9 2
A = 6 5 1 12
3
4 8 3
Solution:
23
Output:
Practice Problems:
Identify the columns that span the column space of A in the following cases.
1 3 3 2
1. A = 2 6 9 7
1 3 4
3
2 4 6 4
2. A = 2 5 7 6
2
2 3 5
24
CLASS NUMBER –5
Topic: The Four Fundamental Subspaces
Procedure: Given a matrix A, we reduce it to row reduced form and find its rank by
identifying the columns that contain the pivots. We then find the four fundamental
subspaces viz, the column space C(A), the row space C(AT), the null space N(A) and
the left null space N(AT).
0 1 0
Example 7: Find the four fundamental subspaces of A = 0 0 1
1
0 0
Solution:
25
Output:
26
Practice Problems:
1 2 0 1
1. Find the four fundamental subspaces of A = 0 1 1 0
1 2 0 1
1 3 3 2
2. Find the four fundamental subspaces of A = 2 6 9 7
4
1 3 3
27
CLASS NUMBER - 6
Topic: Projections by Least Squares
Procedure: Suppose we do a series of experiments and expect the output b to be a linear
function of the input t. We look for a straight line b = C + Dt. If there are experimental
errors then we have a system of equations
C + Dt1 = b1
That is, we have the system of equations Ax = b. The best solution is obtained by
minimizing the error
2
E 2 b Ax (b1 C Dt1)2 (b2 C Dt2 )2 .... (bm C Dtm )2
Example 8: Find the solution x = ( C , D ) of the system Ax = b and the line of best
1 1
fit C + Dt = b given
1
1 , b = ( 1, 1, 3 ).
A = 1 2
Solution:
28
Output:
Practice Problems:
1. Solve Ax = b by least squares where
1 0 1
A= 0 1,b= 1
1 1 0
4. Find a linear relationship between b and t that suits the following data the best:
b 2, 0, 3, 5 at t = - 1 , 0, 1 , 2.
29
CLASS NUMBER -7
Topic: The Gram- Schmidt Orthogonalization
Procedure: Given a set of mutually independent vectors, we produce a set of
orthonormal vectors by applying the Gram – Schmidt process.
Solution:
30
Output:
Practice Problems:
Apply the Gram – Schmidt process to the following set of vectors and find the
orthogonal matrix:
1. ( 1, 1, 0 ) , ( 1, 0, 1 ) , ( 0, 1, 1 )
2. ( 0, 0, 1 ) , ( 0, 1, 1 ) , ( 1, 1, 1 )
3. ( 1, 0, 1 ) , ( 1, 1, 0 ) , ( 2, 1, 1 )
31
CLASS NUMBER - 8
Topic: Eigen values and Eigen vectors of a given square matrix
Procedure: Given a square matrix A, we find the characteristic polynomial of A by
expanding the matrix equation | A – λ I |. The Eigen values of A are obtained solving
the characteristic equation | A – λ I | = 0. The corresponding Eigen vectors are
obtained by solving the system of equations Ax = λx.
Example 10: Find the Eigen values and the corresponding Eigen vectors of
3 2 5
A = 2 3 6
5 6 4
Solution:
Figure XXII - Eigen values and Eigen vectors of a given square matrix - Example 10
32
Output:
Figure XXIII - Eigen values and Eigen vectors of a given square matrix - Output 10
Practice Problems:
Find the Eigen values and the corresponding Eigen vectors of the following matrices:
6 2 6 2 1
1. 5 2
8 2
6 7 4 6 4 4 1
3 1
2 2. 3.
4 3 2 4 0 1 2 2
33
CLASS NUMBER - 9
Topic: Numerically Largest Eigen value of A - The Power Method
Procedure: Given a square matrix A and an initial vector x0, to find the numerically
largest Eigen value of A, we compute the product Ax0 and rewrite it in the form λ1 x1
where λ1 is the first approximation to the numerically largest Eigen value λ and x1 is the
corresponding Eigen vector which is the first approximation to the actual Eigen vector.
We then find Ax1 and rewrite it in the form λ2x2. This process is continued until we get
the two successive values of λ to be nearly the same.
3 0 1
Example 11: Find the largest Eigen value of A = 2 2 2 starting with an
4
2 5
initial approximation x = [ 1 1 1]T
Solution:
Figure XXIV - Numerically Largest Eigen value of A - The Power Method - Example 11
34
Output:
Figure XXV - Numerically Largest Eigen value of A - The Power Method - Output 11 - 1
35
Figure XXVI - Numerically Largest Eigen value of A - The Power Method - Output 11 - 2
36
Figure XXVII - Numerically Largest Eigen value of A - The Power Method - Output 11 – 3
Practice Problems:
Use the power method to compute the largest Eigen value and the corresponding
Eigen vector of the following matrices:
1 3 2
1. 4 4 1 , x0 = ( 1, 0, 0 )
6 3 5
1 6 1
2. 1 2 0 , x0 = ( 1, 0, 0 )
0 0 3
37
38