Exercise Notes
Exercise Notes
MAIN WINDOWS
Scilab’s user interface consists of three main windows:
1. The Console, which pops up when Scilab is opened and on which it outputs
textual data (numeric answers, error messages, etc.)
2. The Editor, which is the main tool for writing, saving, and executing scripts
(programs)
3. The Graphics Window, on which Scilab presents plots.
SAMPLE SCRIPT
Plot y=0.5xsin(2x) from x=0 to x=10
Step 1: On the console, click the Editor icon on the toolbar. The Editor should pop
up.
Step 2: Define what variables your function needs. In row 1, type
x=[0:.1:10]’; a=.5*x;
Step 3: Next, define the function. In row 2, type y=a*sin(2*x);
Step 4: Finally, write the plot command. In row 3, type plot (x,y)
Step 5: Save the script by clicking on the Save icon, and name it wave1.sce.
Step 6: Finish by running the script. Click the Execute icon.
Step 7: Up pops the Graphic window with the plot of the defined equation. Sketch
the graph below.
Or to get help on the Scilab command module, simply type --> help module on the
command line.
Use the “help” menu to find what the following words denote or are used for:
a. ans _________________________________________
b. typeof ______________________________________
c. clear _______________________________________
d. clc _________________________________________
e. comma _____________________________________
f. semicolon ___________________________________
g. exists _______________________________________
h. who ________________________________________
i. list _________________________________________
j. rand ________________________________________
k. comments ___________________________________
l. disp ________________________________________
m. length ______________________________________
VARIABLES
Variables are case-sensitive and cannot contain blanks or spaces. To load a value
into a variable, use the equal sign. Try the following exercises for simple arithmetic
operations:
-->a=5 assigns the value 5 to variable a
-->b=2; assigns the value 2 to variable b
-->c=a+b _________________________
-->d=a-b _________________________
-->e=a*b _________________________
-->f=a/b _________________________
-->g=a^b _________________________
-->who _________________________
_________________________
Note that Scilab evaluates immediately lines that end with a carriage return.
Instructions that end with a semi-colon are evaluated but are not displayed on
screen.
Constant Matrices
Scilab considers a number of data objects as matrices. Scalars and Vectors are
all considered as matrices.
Scalars
Scalars are either real or complex numbers. The values of scalars can be assigned
to variable names chosen by the user.
Output
-->A=3 ______________________
-->B=5+3*%i ______________________
Vectors
The usual way of creating vectors is as follows:
Output
--> v=[2, -3+%i, 7] ______________________
--> w=[4 16 28*%i] ______________________
--> x=[-7; -7+%i; 19] ______________________
______________________
______________________
-->y=[-5 <return> ______________________
2 <return> ______________________
%pi] ______________________
What do you notice about the use of commas, blanks, semicolons and carriage
return in creating vectors?
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
Note with the following examples the role of the position of the blank:
Output
--> a=[1 +3] ____________________________
--> b=[1 + 3] ____________________________
--> c=[1+ 3] ____________________________
--> d=[1, + 8- 7] ____________________________
The resulting vectors begins with the first value and ends with the third value
stepping in increments of the second value. When not specified the default
increment is one.
-->v2=1:2:7
-->v3=-1:3:9
-->v4=1: -3: 9
-->v5=10 : -2 : 0
-->v6=10 : -2 :1
-->v7 = 10 : 4 : 0
The linspace function creates a linearly spaced vector with the arguments from,
to, number of points. The default value is 100 points.
--> linspace(0,3,6)
--> linspace(9,6,5)
Matrices
Row elements are separated by commas or spaces and column elements by
semi-colons.
Try the following exercises and write what values are returned by Scilab:
2. --> B=ones(2,3)
3. -->A.*B
4. -->A*B
5. --> C=zeros(2,4)
6. -->D=[1,2,3; 4,5,6;7,8,9]
Notice that the ones operator with two real numbers as arguments separated by
a comma creates a matric of ones using the arguments as dimensions (same for
zeros).
Try the following exercises and write what values are returned by Scilab:
-->A=[1 2; 3 4]
-->B=[5 6; 7 8]
-->D=[A;B;C]
-->E=matrix(D,3,4)
-->F=eye(E)
-->G=eye(4,3)
Notice that matrix D is created by using other matrix elements. The matrix
primitive creates a new matrix E with the elements of the matrix D using the
dimensions specified by the second two arguments.
The element ordering in the matrix D is top to bottom and then left to right.
Explain the ordering of the rearranged matrix in E
________________________________________________________________
________________________________________________________________
________________________________________________________________
-->b=”<spaces>” //space
-->a+b+c
In mathematics, a matrix (plural: matrices) is a rectangular array of numbers,
symbols, or expressions, arranged in rows and columns. For example, the dimensions
of the matrix below are 2 × 3 (read "two by three"), because there are two rows and
three columns:
Although Scilab is a useful calculator, its main power is that it gives a simple way
of working with matrices. Remember to keep typing in the commands as they
appear here, and observe and understand the Scilab response.
EXERCISE 1
Find x1, x2, and x3 so that
x1 + 2x2 − x3 = 1
−2x1 − 6x2 + 4x3 = −2
−x1 − 3x2 + 3x3 = 1
In spite of the new notation, it is still just as unpleasant to find the solution. In Scilab,
we can set up the equations and find the solution x using simple commands.
-->A=___________________________; //Set up A
-->b=___________________; //Set up b
-->x=___________________ // Find x
x=
-->b
-->A’
-->det(A)
-->spec(A)
-->inv(A)
Use the “help” menu to find what the following were used for in the above exercise:
a. ‘ ________________________________________
b. det______________________________________
c. spec________________________________________
d, inv_________________________________________
EXERCISE 3
-->a=”Scilab” //a 1x1 string matrix
--> eye(2,1)
-->3*ones(2,3)
--> linspace(3,9,4)
--> zeros(1,4)
Observation/s:
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
Observation/s:
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
If we have A(B) or A(B,C) on the left, and the right-hand side evaluates to a nonnull
matrix, then an assignment operation is performed. In that case the left member
expression stands for a submatrix description whose entries are replaced by the
ones of the right matrix entries. Of course, the right and left submatrices must have
compatible sizes, that is, they must have the same size, or the right-hand-side
matrix must be a scalar.
-->x=int(5*rand(2,2)) ______________________
______________________
______________________
-->A([1,2],[1,2])=x ______________________
______________________
______________________
-->A([1,2],[1,3])=[ ] ______________________
______________________
______________________
-->A(:,$)=[ ] ______________________
______________________
______________________
-->A(:,$+1)=[4;5] ______________________
______________________
______________________
When an expression A(B) or A(B,C) is not the left member of an assignment, then
it stands for a submatrix extraction and its evaluation builds a new matrix.
-->A = int(10*rand(3,7))
-->B=A([1,3],$-1:$) Observation:______________________
______________________
______________________
______________________
______________________
______________________
______________________
-->A(2,3)
-->A(1:2,2:4)
-->A(:,2)
-->A(3,:)
-->A(2:$,$)
-->A($,:)
In general A( i:j, k:l ) means the square sub–block of A between rows i to j and
columns k to l . The ranges can be replaced by just ‘:’ if all rows or columns are
to be included. The last row or column is designated $.
It's often simplest to create an array of "x" values using Scilab's "implicit for loop".
Try the statement:
x = 0 : 0.5 : 10
Observation:______________________________________________________
________________________________________________________________
________________________________________________________________
In most cases, when you call a function with an array as an argument, the
function will be applied to each element of the array individually, and the results
stored in an output array of the same size. Thus, if the function plusone adds 1.0
to its argument, then
y=x+1
CALLING PLOT
Once you have two arrays of the same size, you can plot one against the
other via the plot command.
Observation:______________________________________________________
________________________________________________________________
________________________________________________________________
There are a number of options to the plot command, which you can read
by typing help plot in the Scilab Control Window. Perform the following:
Observation:______________________________________________________
________________________________________________________________
________________________________________________________________
Observation:______________________________________________________
________________________________________________________________
________________________________________________________________
Observation:______________________________________________________
________________________________________________________________
________________________________________________________________
Observation:______________________________________________________
________________________________________________________________
_______________________________________________________________
Finally, the clf command allows you to control whether each plotting
command draws on top of the existing graph, or clears the window before drawing.
which superimposes all the data and plotting window so it looks like this:
Observation:______________________________________________________
________________________________________________________________
________________________________________________________________
This sequence, which uses the clf command to wipe the graphics window
clean after the first graph has been displayed.
-->x = 0 : 0.1 : 10;
-->y = sin(x);
-->z = sin(2*x);
-->plot(x, y);
-->clf;
-->plot(x, z, '+');