Intro.
To SCILAB
SCILAB (a descendent of MATLAB) is an open
source interactive system for performing
engineering and scientific computations
Performs powerful operations using one or
two commands
You may also write your own scripts,
including functions, using familiar
programming constructs
SCILAB includes an excellent graphics
subsystem
1
Intro. Continued
When you first start SCILAB, you are
presented with the Command Prompt - ->
A lot of Help is availlable simply click the
Help button in the toolbar (button with the
question mark)
You may request help on a specific topic from
the command prompt, e.g.,
Help elementary
Intro. Continued
The fundamental data structure in SCILAB is
the matrix
At the command prompt, if you enter
A = [3 2 -6; 4 -2 1; 7 8 2]
then you create the matrix
A=
3.
2.
-6.
4.
-2. 1.
7.
8.
2.
3
Intro. Continued
The command
a = [-3 : 2: 18]
creates the matrix (vector)
a=
-3.
-1.
1.
3.
5.
7.
9.
11.
13.
15.
17.
i.e., SCILAB creates a row vector in which the first element is -3., the
last element is less than or equal to 18 and the increment (step size) is
2.
The command
a = [-2 :3 :9; 17 :8 : 43]
creates the matrix
a=
-2.
1.
4.
7.
17. 25. 33. 41.
Intro. Continued
Operations in SCILAB may be classified as
normal vector/matrix operations (as defined
in linear algebra) or element-wise operations
Element-wise operations are executed
element by element
The following example will help clarify the
meaning of element-wise operations.
Intro Continued
A = [1 2 3; 4 5 6];
B = [-2 3 1; 1 3 4];
A .* B = [-2. 6. 3.; 4. 15. 24.]
However A * B is undefined (# of
columns of A is not equal to the # of
rows of B)
6
Intro Continued
The normal matrix operators are:
+ - * /
^ **
\ (left inverse) and (transpose)
Element-wise operators are:
.* ./ .^
7
Functions in SCILAB
SCILAB has a tremendous number of built in
functions
However, you can extend this set of functions
by providing your own
Recall that the Trapezoidal Rule with the
correction term is often used to numerically
integrate a function which is differentiable
over the interval of integration. It is defined
as follows:
8
Functions Continued
b
a f(x) dx = h/2[f(a) + f(b)] + h2/12 [f(a) f(b)]
where h = b a
We now apply the Trapezoidal Rule to determine
4
(2x2 + 9x - 5) dx
1
Functions Continued
(1) We first define a function in SCILAB to evaluate f(x) for
any value x passed as a parameter. This may be done
in one of two ways.
Firstly, we may use SciNotes (the SCILAB editor) to
define the function as follows:
// Function to evaluate y = 2x^2 + 9x - 5
function y=f(x)
y = 2 * x^2 + 9 * x - 5;
endfunction
10
Functions Continued
The script defining the function will be saved
by default as f.sci. This is fine.
The function is then created in SCILAB by
executing the command
exec (f.sci) from the command prompt.
The second way in which a function may be
created in SCILAB is through the use of the
deff (online define function ), for e.g.,
11
Functions Continued
deff ('y = f(x)', 'y = 2 * x^2 + 9 * x 5)
12
Functions Continued
(2)
We next define a function to define
f(x) for any value of x passed as a
parameter. This is done as follows:
deff ('y = fprime(x)', 'y = 4 * x + 9')
13
Functions Continued
(3) Finally we develop a function to evaluate the integral.
This function accepts as parameters the values of a
and b. This is the function:
function y = trap(a, b)
h = b - a;
y = (h / 2) * (f(a) + f(b)) + ...
(h ^ 2) / 12 * (fprime(a) - fprime(b))
endfunction
Notice that the three-dot sequence (...) is used to
denote statement continuation.
14
Functions Continued
The function trap may be called from
the command prompt, for e.g.,
answer = trap (1, 4)
15
Numerical Integration
SCILAB uses a function called integrate to
perform integration by quadrature(a
numerical method used to find the area under
the graph of a function)
Once again, assume you wish to perform the
following:
4
(2x2 + 9x - 5) dx
1
This is done as follows:
16
Numerical Integration
(1)
We first define a function to evaluate f(x)
for any value x passed as a parameter.
This is done as follows:
deff (y = f(x), y = 2*x^2 + 9*x -5)
17
Numerical Integration
2.
At the command prompt, enter the following command:
integrate ('f(x)', 'x',1, 4)
The integral is determined
EXERCISE
Using your knowledge of Calculus, perform the following integration:
2.7
dx
0.1 (4 x + 1) ^ 1 / 2
Then use integrate to determine the answer in SCILAB
18
System of linear Equations
Assume you wish to solve the following
system of linear equations:
2x + y + 5z = 5
2x + 2y + 3z = 7
x + 3y + 3z = 6
The following script will perform the task:
19
System of linear equations (Continued)
// Script to solve the following system of linear equations
// 2x + y + 5z = 5
// 2x + 2y + 3z = 7
// x + 3y + 3z = 6
A = [2 1 5; 2 2 3; 1 3 3];
B = [5 7 6]';
X = A \ B;
disp (X)
20
System of linear equations (Continued)
Note that in SCILAB a script is
developed using the SCINote editor and
saving it as a .sce file.
Then it is executed by
exec (file-name)
21
Roots of a Polynomial
SCILAB provides a function called roots to determine the roots of a polynomial
Assume we wish to determine the roots of the following polynomial
y = (x + 1) (x + 2)
First the polynomial must be written in the following form:
y = 2 + 3x + x2
The coefficients are then used to define the polynomial using the poly function
as follows:
p = poly ([2 3 1], x, coeff)
The roots of the polynomial are then given by:
answer = roots(p)
22
Evaluating a Polynomial
SCILAB provides a function called horner which may
be used to evaluate a polynomial at a specified value
Assume we wish to evaluate the polynomial function
y = 4x3 - 3x2 + 9
at a specified x value
The following script uses console input and output
to request the specified value and output the answer
23
Evaluating a Polynomial
// Script to evaluate polynomial at a specific point
// The point is entered by the user
//Use the polynomial 4x^3 - 3x^2 + 9
p = poly ([9 0 -3 4], 'x', 'coeff');
x = input ('Enter x value');
answer = horner (p, x);
//disp (answer)
mprintf ("The answer is %d", answer)
24
Multiplying Polynomials
SCILAB provides a function called convol
(Convolution and polynomial multiplication) which is
used to multiply polynomials
Before discussing convol, it is necessary to discuss an
alternative format for representing polynomials. A
polynomial p may be represented as
p = [4 -3 9 0]
This is to be interpreted as a polynomial with
coefficients 4 -3 9, for example, 4x3 3x2 + 9x
25
Multiplying Polynomials (Continued)
Assume we wish to multiply the following
polynomials:
y = 4x3 3x2 + 9
y = x2 2
The following script does the job:
26
Multiplying Polynomials (Continued)
// Script to multiply two polynomials
// It multiplies (4x^3 - 3x^2 + 9) by ( x^2 - 2)
// First define the two polynomials
p = [4 -3 0 9];
q = [1 0 -2];
answer = convol (p, q);
disp (answer)
27
Multiplying Polynomials (Continued)
The execution of the above script
displays
4. - 3. - 8. 15. 0. - 18.
This is to be interpreted as
4x5 3x4 -8x3 +15x2 -18
28
Roots of any function
SCILAB provides a function called fsolve
which may be used to determine the
root of a function near a specified
value.
Assume we wish to determine the root
of sin(2x +/4) near x = /4.
This job is performed as follows:
29
Roots of any function
First define a function to determine sin(2x + /4 ) as
follows:
deff ('y = k(x)', 'y = sin(2*x + %pi/4)')
Apply fsolve as follows:
fsolve(%pi/4, k)
SCILAB returns 1.1780972. This, as you know, is
correct since the solution is 3 /8, which is 1.1780972.
30