![Page 1: An Introduction to Scilab Tsing Nam Kiu 丁南僑 Department of Mathematics The University of Hong Kong 2009 January 7](https://reader036.vdocuments.site/reader036/viewer/2022082505/56649de75503460f94ae16ad/html5/thumbnails/1.jpg)
An Introduction to Scilab
Tsing Nam Kiu 丁南僑Department of Mathematics
The University of Hong Kong
2009 January 7
![Page 2: An Introduction to Scilab Tsing Nam Kiu 丁南僑 Department of Mathematics The University of Hong Kong 2009 January 7](https://reader036.vdocuments.site/reader036/viewer/2022082505/56649de75503460f94ae16ad/html5/thumbnails/2.jpg)
What is a Scilab?
Scilab is a mathematical software Similar software: Matlab, Mathematica, Octave,
Euler Math Toolbox, Maxima, …
What is special about Scilab: free, highly supported, powerful, many users, …
Home page of Scilab: www.scilab.org A short introduction of Scilab: http://hkumath.hku.hk~n
kt/Scilab/IntroToScilab.html
![Page 3: An Introduction to Scilab Tsing Nam Kiu 丁南僑 Department of Mathematics The University of Hong Kong 2009 January 7](https://reader036.vdocuments.site/reader036/viewer/2022082505/56649de75503460f94ae16ad/html5/thumbnails/3.jpg)
Using Scilab as a calculator
+, – , * (multiplication), / (division), ^ (power)
Examples:
> (12.34 + 0.03) / (2.8 – 1.2 * 3)
> 2^3 or 2*2*2
> 2^– 3
> 2^100
> ans^(1/100)
![Page 4: An Introduction to Scilab Tsing Nam Kiu 丁南僑 Department of Mathematics The University of Hong Kong 2009 January 7](https://reader036.vdocuments.site/reader036/viewer/2022082505/56649de75503460f94ae16ad/html5/thumbnails/4.jpg)
Using Scilab as a calculator (2)
Commonly used functions:
cos, sin, tan, acos, asin, atan, sqrt, exp, log, log10 Solving quadratic equation x^2 – x+1=0:
> a = 1, b = – 1, c = 1
> (– a + sqrt(b^2 – 4*a*c))/(2*a)
> (– a – sqrt(b^2 – 4*a*c))/(2*a) A smarter way to find roots of polynomials:
> p = poly([1 –1 1],"x","coeff")
> roots(p)
![Page 5: An Introduction to Scilab Tsing Nam Kiu 丁南僑 Department of Mathematics The University of Hong Kong 2009 January 7](https://reader036.vdocuments.site/reader036/viewer/2022082505/56649de75503460f94ae16ad/html5/thumbnails/5.jpg)
Using Scilab as a calculator (3)
special constants: %i, %pi, %e
> tan(%pi / 4)
> %e ( = exp(1) )
> (1+%i)*(1--%i) Learning how to use Scilab and getting help:
Click on “?” on menu
> help command
See documentation on Scilab website
![Page 6: An Introduction to Scilab Tsing Nam Kiu 丁南僑 Department of Mathematics The University of Hong Kong 2009 January 7](https://reader036.vdocuments.site/reader036/viewer/2022082505/56649de75503460f94ae16ad/html5/thumbnails/6.jpg)
Vectors and matrices in Scilab
Data types: (real or complex) numbers, vectors, matrices, polynomials, strings, functions, …
Vectors in Scilab:
> x = [0 1 2 – 3]
> y = [2; 4; 6; 8]
> z = [1 2 3 4] ’ ’ is conjugate transpose of a matrix
> 3*x, y+z, y–z
> x+y, x+1
![Page 7: An Introduction to Scilab Tsing Nam Kiu 丁南僑 Department of Mathematics The University of Hong Kong 2009 January 7](https://reader036.vdocuments.site/reader036/viewer/2022082505/56649de75503460f94ae16ad/html5/thumbnails/7.jpg)
Vectors and matrices in Scilab (2)
Matrices in Scilab:
> A = [0 1 0 1; 2 3 –4 0]
> B = A ’
> A * y, x * B, A * B, B * A, (B*A)^2 Special matrices (and vectors):
> ones(2,3), zeros(1,2), eye(3,3)
> rand, rand(3,2) Empty vector or matrix: > a = [ ] Building matrix by blocks:
> C = [A 2*A], x = [9 x 7], a = [a 1]
![Page 8: An Introduction to Scilab Tsing Nam Kiu 丁南僑 Department of Mathematics The University of Hong Kong 2009 January 7](https://reader036.vdocuments.site/reader036/viewer/2022082505/56649de75503460f94ae16ad/html5/thumbnails/8.jpg)
Solving linear equations
3 x1 + 2 x2 – x3 = 1
x1 + x3 = 2
2 x1 – 2 x2 + x3 = – 1 To solve the above system of linear equations:
> A = [3 2 – 1 ; 1 0 1; 2 – 2 1]
> b = [1 2 – 1]’
> x = inv(A)*b (inv is inverse of a matrix)
> x = A \ b Important remark: theoretically it does not make s
ense to divide something by a matrix!
![Page 9: An Introduction to Scilab Tsing Nam Kiu 丁南僑 Department of Mathematics The University of Hong Kong 2009 January 7](https://reader036.vdocuments.site/reader036/viewer/2022082505/56649de75503460f94ae16ad/html5/thumbnails/9.jpg)
The colon “:” operator
> 1:10, 1:100, xx = 1:100; Using “;” to suppress answer output > sum(xx) > 1:2:10, –3:3:11, 4:–1:1, 2:1:0, > t = 0: 0.1: 2*%pi
> y = sin(t)
> plot(t,y), plot(t,sin(t),t,cos(t)) Task 1: plot the straight lines
y = x +1 and y = exp(x) on the same graph, from x = – 2 to x = 2
![Page 10: An Introduction to Scilab Tsing Nam Kiu 丁南僑 Department of Mathematics The University of Hong Kong 2009 January 7](https://reader036.vdocuments.site/reader036/viewer/2022082505/56649de75503460f94ae16ad/html5/thumbnails/10.jpg)
Elements of vectors and matrices
Example
> v = rand(4,1)
> v(1), v(3), v([2 4]), v(4:-1:1), v($) “$” means the last entry Example
> A = [1 2 3 4 5; 6 7 8 9 10]
> A(2,3), A(1,:), A(:, 2), A(:, [4 2])
![Page 11: An Introduction to Scilab Tsing Nam Kiu 丁南僑 Department of Mathematics The University of Hong Kong 2009 January 7](https://reader036.vdocuments.site/reader036/viewer/2022082505/56649de75503460f94ae16ad/html5/thumbnails/11.jpg)
Exercises
Task 2: simulate tossing of a coin:
0 = head, 1 = tail.
functions to use: rand, round, …
Task 3: simulate tossing of 100 coins
![Page 12: An Introduction to Scilab Tsing Nam Kiu 丁南僑 Department of Mathematics The University of Hong Kong 2009 January 7](https://reader036.vdocuments.site/reader036/viewer/2022082505/56649de75503460f94ae16ad/html5/thumbnails/12.jpg)
Exercises (2)
Task 4: simulate throwing 3 dices, each dice has outcome from 1 to 6 with equal probabilities;
functions to use: rand, floor, ceil, …
Task 5 (challenging!): simulate tossing a coin 100 times and find the longest run of consecutive H’s or T’s in the resulting sequence;
functions to use: diff, find, max,
![Page 13: An Introduction to Scilab Tsing Nam Kiu 丁南僑 Department of Mathematics The University of Hong Kong 2009 January 7](https://reader036.vdocuments.site/reader036/viewer/2022082505/56649de75503460f94ae16ad/html5/thumbnails/13.jpg)
Programming in Scilab
Click on menu bar to open Scipad; then write your scilab function file.
Format of a function:function [out1, out2, ...] = name(in1, in2, ...) (body of function definition; may have many lines)endfunction
One file may contain more than one function. To use the functions, you must load the function fi
le by choosing File -> Execute the file from the menu.
![Page 14: An Introduction to Scilab Tsing Nam Kiu 丁南僑 Department of Mathematics The University of Hong Kong 2009 January 7](https://reader036.vdocuments.site/reader036/viewer/2022082505/56649de75503460f94ae16ad/html5/thumbnails/14.jpg)
Programming in Scilab (2)
A simple function to find the n-th term of the Fibonnaci sequence 0, 1, 1, 2, 3, 5, 8, 13, 21, …
function k = fibo(n) if n == 1, k = 0;elseif n==2, k = 1;else k = fibo(n-1) + fibo(n-2);end
endfunction Save the file as fibo.sci (or any other file name). Execute it from Scilab menu bar Try, say: > fibo(5), fibo(2), fibo(10), fibo(100)
![Page 15: An Introduction to Scilab Tsing Nam Kiu 丁南僑 Department of Mathematics The University of Hong Kong 2009 January 7](https://reader036.vdocuments.site/reader036/viewer/2022082505/56649de75503460f94ae16ad/html5/thumbnails/15.jpg)
Programming in Scilab (3)
An improved programme:function K = fibonacci(n)//function K = fibonacci(n)//Gives the n-th term of the Fibonacci sequence ,1,1,2,3,5,8,13,... if n==1,
K = 0; elseif n==2, K = 1;elseif n>2 & int(n)==n
// check if n is an integer greater than 2K = fibonacci(n-1) + fibonacci(n-2);
else disp('error! -- input is not a positive integer'); end
endfunction
![Page 16: An Introduction to Scilab Tsing Nam Kiu 丁南僑 Department of Mathematics The University of Hong Kong 2009 January 7](https://reader036.vdocuments.site/reader036/viewer/2022082505/56649de75503460f94ae16ad/html5/thumbnails/16.jpg)
Programming in Scilab (4)
Programming Task (challenging!): write a programme to automate Task 5, which is to perform the following experiment m times. The experiment is to simulate tossing a coin n times and find the longest run (k) of consecutive H’s or T’s in the resulting sequence.
For each time you do the experiment, you’ll get a number k. Therefore you should get m numbers k
1, k2, …, km at the end. Inputs of the function are m, n; output is a vector
k = [ k1 k2 … km].
![Page 17: An Introduction to Scilab Tsing Nam Kiu 丁南僑 Department of Mathematics The University of Hong Kong 2009 January 7](https://reader036.vdocuments.site/reader036/viewer/2022082505/56649de75503460f94ae16ad/html5/thumbnails/17.jpg)
Recap
We have discussed and learned the following: What Scilab is Basic usage of Scilab (as a calculator) Vectors and Matrices in Scilab Solving linear equations Simulation of some random events Basic Scilab programming