To SCILAB
Intro. Continued
Help elementary
Intro. Continued
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
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
Functions in SCILAB
Functions Continued
b
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:
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
12
Functions Continued
(2)
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
15
Numerical Integration
Numerical Integration
(1)
17
Numerical Integration
2.
EXERCISE
Using your knowledge of Calculus, perform the following integration:
2.7
dx
0.1 (4 x + 1) ^ 1 / 2
18
A = [2 1 5; 2 2 3; 1 3 3];
B = [5 7 6]';
X = A \ B;
disp (X)
20
Roots of a Polynomial
The coefficients are then used to define the polynomial using the poly function
as follows:
p = poly ([2 3 1], x, coeff)
Evaluating a Polynomial
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
26
27
30