0% found this document useful (0 votes)
41 views8 pages

Introduction to SCILAB for EE 179.1

The document is an activity guide for introducing students to the Scilab software environment and its basic features. It contains 12 questions for students to answer to help them explore Scilab's variable types, operators, flow control, functions, and scripting capabilities. The guide walks students through declaring variables, performing arithmetic on matrices, accessing matrix elements, creating and calling simple functions, and batch executing commands through scripting.

Uploaded by

Patrick Go
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views8 pages

Introduction to SCILAB for EE 179.1

The document is an activity guide for introducing students to the Scilab software environment and its basic features. It contains 12 questions for students to answer to help them explore Scilab's variable types, operators, flow control, functions, and scripting capabilities. The guide walks students through declaring variables, performing arithmetic on matrices, accessing matrix elements, creating and calling simple functions, and batch executing commands through scripting.

Uploaded by

Patrick Go
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Name: Patrick Dale Cuartero

EE 179.1 Section: KGMJ


3:00 PM

Course and Year: BSECE V


Laboratory Schedule: W 12:00 PM -

Activity 1: Introduction to SCILAB


Abstract
In this activity, you will be exploring the functions of Scilab, is a freely distributed
open source scientific software package. This is where you use and find out the
functions needed in this course. At first you will be guided from the softwares
environment and features. Later part you will be introduced to fundamental
mathematical functions that will be used throughout this course. Further activities
are expected to use the softwares different tools for easy analysis.
At the end of all laboratory activities, tt is expected that you will harness
competitiveness using this software as you finish EE 179.1 Feedback and Control
System Laboratory.

1 Objectives

To introduce students to a mathematical software that can be used to


simulate control systems.

2 List of equipment/software

Personal Computer
Installation of SCILAB

3 SCILAB Environment and Features Overview


Scilab is a free and open-source mathematical software. It can be used to simulate
mathematical applications from basic to advanced engineering systems.
Simulations can be through set of commands entered in the interactive command,
through a script written in the SciNotes, or through XCOS. XCOS is Scilabs
counterpart for Matlabs Simulink.
For this activity, you will be introduced to the basic command line interface as well
as scripting of a set of commands in SciNotes. Some basic features of Scilab such as
variable declarations, operations and flow control will be examined in this activity.
Some commonly used functions will also be introduced.

3.1 Variable
In Scilab, you can easily create and instantiate variables anytime. Unlike the C
language, Scilabs variables are dynamic and dont have to be created before they
are stored with values.

Name: Patrick Dale Cuartero


Course and Year: BSECE V
EE 179.1 Section: KGMJ
Laboratory Schedule: W 12:00 PM 3:00 PM
Open you Scilab now and try the following in the console.
--> clear
--> a = 1
The console should respond with the value of a. Now, after the above command, try
doing the following:
--> b = a
The console should respond with the value of b which is just the value of a in the
previous command.
1. How about this one?
Undefined variable: aaa
Since aaa has not been assigned a value yet, it still does not exist.
Another convenient feature of Scilab is that you can assign any datatype to a
variable even after you had previously assigned a different datatype. For example:
--> b = 1
--> b = Hi, this is a string
--> b = [this,is, a, vector/array]
These commands will not produce an error. The datatype of the variables in Scilab
adapt to whatever value you store in it.
You may have noticed that the last command is an array. Arrays in Scilab are values
enclosed in [ and ] with values separated by spaces or commas. You can also
create a matrix by separating rows with a semicolon.
--> an_array = [1, 3, 4]
--> a_matrix = [1, 2; 4, 5]
2. What will you enter in the command line if you want to assign a_matrix

with

[ ]
1 2 3
4 5 6
7 8 9

You can easily declare an array of sequence of numbers using :


--> 1:10
// returns an array of numbers from 1 to 10 with an
interval of 1
--> 1:2:10
// returns an array of numbers from 1 to 10 with an
interval of 2

Name: Patrick Dale Cuartero


Course and Year: BSECE V
EE 179.1 Section: KGMJ
Laboratory Schedule: W 12:00 PM 3:00 PM
3.1.1 Polynomial
You can easily create polynomial using the poly function read the help file for
different methods of using the poly function. The simplest way is the following:
--> s= poly(0,s)

//this assign polynomial s^1 to the variable s.

Now you can manipulate this variable similar to number.


3. Write the result of (s^2+2*s+1)/(s+1) as shown in the console.
Operators
Scilab has a lot of operators in addition to basic arithmetic operations. Since Scilab
operates on matrices by default, basic arithmetic operations are applied on
matrices.
Operat
or
+
*
/
\
^

Description
Matrix addition
Matrix subtraction
Matrix Multiplication
Matrix division.

A / B= AB

Matrix back-division.

A = A1B

Matrix exponential.
Transpose

If you want element-wise operation using those operators, the operator is preceded
with a .
--> a = [1, 2; 3, 4]
--> b = [3, 4; 5, 6]
--> a + b
ans =
4.
6.
8.
10.
4. Do a matrix multiplication and element-wise multiplication on a and b.
What are the results? Are the results equal?
5. What is the result of a?
Accessing an element in an array or matrix is by calling the variable with a
parenthesis. For example, to access the 1st element of b:

Name: Patrick Dale Cuartero


Course and Year: BSECE V
EE 179.1 Section: KGMJ
Laboratory Schedule: W 12:00 PM 3:00 PM
--> b(1) //this will return 3. Matrix b is treated as a vector reading
top to bottom starting from the left.
This is the same as
--> b(1,1) //accessing element in column 1 row 1. First index is for
the row, the second is for the column.
You can use the $ to indicate the index of the last element.
--> b(1,$) //returns the last element of the first row.
Sub-matrices can be extracted by putting an array or matrix as index. The content
of the matrix index will be the rows/columns that will be included in the sub-matrix.
You can use : to include all elements on that row/column.
--> c = [1, 2 ,3; 4, 5, 6; 7, 8, 9]
--> d = c([1,3],[1,2])
ans =
1.
2.
3.
8.
--> e = c(:,1) //returns all the rows in the first column.
6. What is the result of c(1:3,$)?

3.2 Flow Control


The flow control in Scilab can be done with if then/else statements, select/case ,
return, for loop, do and while loops. In addition, keywords such as break, continue,
pause, abort are also useable to alter the flow of a set of Scilab commands. The
syntax for this is in described in detailed in the Help menu of Scilab. To access the
Help, type help in the command line or click the help button in the menu bar.

3.3 Functions in Scilab


You can create a reuseable set of commands as a function in Scilab. The basic
syntax is the following:
FUNCTION [y1,, yn]= functionname(x1, , xm)
//some statements or commands here
ENDFUNCTION
3.3.1 Some of the commonly used functions in Scilab
Refer to the Help menu for details
1. plot used to plot expressions in Scilab.
2. poly function to create a polynomial.
3. roots Solve the roots of a polynomial

Name: Patrick Dale Cuartero


Course and Year: BSECE V
EE 179.1 Section: KGMJ
Laboratory Schedule: W 12:00 PM 3:00 PM
4. coeff extract the coefficients of a polynomial.
5. evstr - evaluate a string of Scilab statements or commands
6. csim Simulation of a linear system. (time response)
7. ones Generate a matrix of ones
8. zeros Generate a matrix of zeroes
9. rand generate a matrix of random values
[Link] generate an identity matrix.
[Link] inverse of a matrix
[Link] extract the diagonal of matrix
[Link] absolute value
[Link], imag, complex for complex numbers
[Link] conjugate of a complex
[Link] partial fraction expansion of a give transfer function
[Link] system linear definition
18.ss2tf State-space representation to transfer function conversion
19.tf2ss Transfer function to state-space representation
[Link] random system generator.

3.4 Batch Commands or Scripting


Scilab has an integrated text editor called SciNotes for creating and editing Scilab
scripts. A set of commands or statements can be written in a Scilab script that can
be ran or executed in a single action. SciNotes has some advance text editing
functionalities for coding like parenthesis matching and syntax highlighting. Open
Scinote by clicking the notebook icon below the menu bar.
After opening the SciNotes, do the following:
7. Write the following in the editor:
a = [0:0.1:2*%pi]
b = this is executed after a=1
plot(a,sin(2*a))
//end
What is the result or the behavior of the above statements in the console
after executing the script?
8. Write a function called myfunct that accepts two parameters A and B.
The function will return the result of (A+B)*B. Execute the script what is
the result?
9. Call the function you created in #8 and pass as parameters the values 3
and 9. What is the result?
10. What will your function return if the parameters are [1,2,3] and [4;
6;7]?
11. How about [1, 2, 3 ; 3, 5, 1; 5 6 -1] and [3, -1, 4 ; -3, 5, 1; -5 6 -1]?

Name: Patrick Dale Cuartero


Course and Year: BSECE V
EE 179.1 Section: KGMJ
Laboratory Schedule: W 12:00 PM 3:00 PM
12. Do you have to re-execute your function from SciNotes? Why?

3.5 Answers to Questions


1. --> b = aaa
!--error 4
2. a_matrix = [1,2,3;4,5,6;7,8,9]
3. p = 1 + s
----1
4. No.
a*b
ans =

13.

16.

29.

36.

a.*b
ans =
3.

8.

15.

24.

5. a'
ans =
1.

3.

2.

4.

6. c(1:3,$)
ans =
3.
6.
9.
7. a = [0:0.1:2*%pi] //is registering values unto variable a
b = this is executed after a=1 //syntax error
!--error 2
Invalid factor.

Name: Patrick Dale Cuartero


Course and Year: BSECE V
EE 179.1 Section: KGMJ
Laboratory Schedule: W 12:00 PM 3:00 PM
plot(a,sin(2*a)) //plots the function

8. Note that the first 3 lines are in [Link] file to be executed in the console
before calling myfunct.
-->function [x] = myfunct(A,B)
-->x=(A+B)*B
-->endfunction
-->myfunct(1,1)
ans=1
9. -->myfunct(3,9)
ans =
108.
10. Initially the given parameters do not fit in the function since it is programmed to
receive only two inputs. It is expected to result to an error.
-->myfucnt[1,2,3]
!--error 4
Undefined variable: myfucnt
-->myfunct[4; 6;7]

Name: Patrick Dale Cuartero


EE 179.1 Section: KGMJ
3:00 PM
!--error 276

Course and Year: BSECE V


Laboratory Schedule: W 12:00 PM -

Missing operator, comma, or semicolon.


11. Initially the given parameters do not fit in the function since it is programmed to
receive only two inputs. It is expected to result to an error.
Both parameters return !--error 276 ( Missing operator, comma, or semicolon.)
12. We not only have to re-execute the function but to program it where it will
accept multiple inputs or arrays. In return the function will execute properly given
the parameters above.

4 Conclusion
The Scilab is a freeware where most engineering applications in programming is
performed. I have learned that this software is capable to compute arithmetic
equations. large number of arrays depending on computers memory, even
polynomials finding the roots. In short, this is a tool to simplify problems that are
difficult and time-consuming to compute in real life.

Common questions

Powered by AI

Scilab is a freely distributed and open-source software that provides a cost-effective solution for simulating control systems, making it accessible for educational institutions and professionals. It offers features similar to MATLAB, including XCOS for system simulations, which is comparable to Simulink. Scilab enables dynamic typing, extensive use of mathematical functions, and the ability to handle complex scientific computations. It supports matrix-oriented syntax essential for engineering applications, and its scripting capability allows users to automate and document simulations effectively, enhancing productivity and reproducibility .

SciNotes in Scilab is an integrated text editor equipped with advanced text editing functionalities like parenthesis matching and syntax highlighting. These features help prevent syntax errors by visually distinguishing matching elements and structuring code, facilitating easier debugging and comprehension. Furthermore, SciNotes enhances script development by allowing users to write and execute batch commands as scripts, improving workflow efficiency by enabling the execution of multiple commands in a single step .

Flow control in Scilab, encompassing constructs like if-else, for loops, and while loops, enables developers to dictate execution pathways based on conditions, which is crucial for simulations requiring iterative adjustments. For example, a control system simulator may use a while loop to repeatedly adjust system parameters until an optimal response is achieved. This flexibility supports complex decision-making required in engineering applications, such as adaptive filtering or real-time system analysis .

In Scilab, variables are dynamically typed, meaning they can be assigned any data type anytime without prior specification, unlike static typed languages like C where variables must be declared with a specific type before use. This flexibility allows seamless operations across data types without needing to redefine variables, facilitating rapid prototyping and testing in engineering systems .

In engineering, revisiting and modifying Scilab scripts and functions is vital due to the dynamic nature of engineering problems that often require new data handling, algorithm improvements, or integration of additional features. As project parameters change or more efficient solutions are developed, existing scripts need updates to maintain relevance, accuracy, and performance. This continuous refinement process helps meet evolving engineering challenges and ensure accurate simulations and analyses .

The poly function in Scilab creates polynomial expressions, allowing users to manipulate polynomials as algebraic variables within calculations. A basic use case is creating a polynomial variable 's' with s= poly(0,'s'), which assigns s^1 to variable s, enabling algebraic operations like solving (s^2 + 2*s + 1)/(s + 1) directly via polynomial division .

To plot a sine wave in Scilab, you define a variable 'a' as an array of values, e.g., a = [0:0.1:2*%pi]. Then, execute the plot command plot(a, sin(2*a)) to graph the sine function. Errors might occur if the syntax is incorrect, such as a missing comma, or if the variable 'b' is incorrectly defined as per syntax rules, producing an 'Invalid factor' error when unexpected expressions are used .

Sub-matrix extraction in Scilab is performed using matrix indexing. For example, given matrix c = [1, 2, 3; 4, 5, 6; 7, 8, 9], to extract a sub-matrix from the first and third rows and first two columns, you use c([1,3],[1,2]), resulting in the sub-matrix [1,2; 7,8]. Another example is extracting all elements from the first column using c(:,1), which results in the vector [1; 4; 7]. The indexing uses row and column indices or ':' to include all elements in a dimension .

To adapt a Scilab function for handling arrays, modify the function to include iteration or vectorized operations that can process each element in the array efficiently. This involves using loops or inherent Scilab functions that support element-wise operations, ensuring the function's flexibility to manage inputs of varying dimensions. This adaptability is necessary to perform complex computations on datasets typical in engineering scenarios, enhancing the function's utility across different use cases .

Matrix multiplication and element-wise multiplication in Scilab yield different results: matrix multiplication (a*b) results in a new matrix [13, 16; 29, 36], whereas element-wise multiplication (a.*b) results in [3, 8; 15, 24]. These operations are not equivalent; matrix multiplication involves a dot product-like process across rows and columns, while element-wise directly multiplies individual elements .

You might also like