An Introduction to Scilab Tsing Nam Kiu 丁南僑 Department of Mathematics The University of Hong Kong 2009 January 7
What is a Scilab? n Scilab is a mathematical software n Similar software: Matlab, Mathematica, Octave, Euler Math Toolbox, Maxima, … n What is special about Scilab: free, highly supported, powerful, many users, … n Home page of Scilab: A short introduction of Scilab:
Using Scilab as a calculator n +, –, * (multiplication), / (division), ^ (power) Examples: n > ( ) / (2.8 – 1.2 * 3) n > 2^3 or 2*2*2 n > 2^– 3 n > 2^100 n > ans^(1/100)
Using Scilab as a calculator (2) n Commonly used functions: cos, sin, tan, acos, asin, atan, sqrt, exp, log, log10 n 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) n A smarter way to find roots of polynomials: > p = poly([1 –1 1],"x","coeff") > roots(p)
Using Scilab as a calculator (3) n special constants: %i, %pi, %e > tan(%pi / 4) > %e ( = exp(1) ) > (1+%i)*(1--%i) n Learning how to use Scilab and getting help: Click on “?” on menu > help command See documentation on Scilab website
Vectors and matrices in Scilab n Data types: (real or complex) numbers, vectors, matrices, polynomials, strings, functions, … n Vectors in Scilab: > x = [0 1 2 – 3] > y = [2; 4; 6; 8] > z = [ ] ’ n ’ is conjugate transpose of a matrix > 3*x, y+z, y–z > x+y, x+1
Vectors and matrices in Scilab (2) n Matrices in Scilab: > A = [ ; 2 3 –4 0] > B = A ’ > A * y, x * B, A * B, B * A, (B*A)^2 n Special matrices (and vectors): > ones(2,3), zeros(1,2), eye(3,3) > rand, rand(3,2) n Empty vector or matrix: > a = [ ] n Building matrix by blocks: > C = [A 2*A], x = [9 x 7], a = [a 1]
Solving linear equations n 3 x x 2 – x 3 = 1 x 1 + x 3 = 2 2 x 1 – 2 x 2 + x 3 = – 1 n 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 n Important remark: theoretically it does not make sense to divide something by a matrix!
The colon “:” operator n > 1:10, 1:100, xx = 1:100; n Using “;” to suppress answer output n > sum(xx) n > 1:2:10, –3:3:11, 4:–1:1, 2:1:0, n > t = 0: 0.1: 2*%pi > y = sin(t) > plot(t,y), plot(t,sin(t),t,cos(t)) n Task 1: plot the straight lines y = x +1 and y = exp(x) on the same graph, from x = – 2 to x = 2
Elements of vectors and matrices n Example > v = rand(4,1) > v(1), v(3), v([2 4]), v(4:-1:1), v($) n “$” means the last entry n Example > A = [ ; ] > A(2,3), A(1,:), A(:, 2), A(:, [4 2])
Exercises n Task 2: simulate tossing of a coin: 0 = head, 1 = tail. functions to use: rand, round, … n Task 3: simulate tossing of 100 coins
Exercises (2) n Task 4: simulate throwing 3 dices, each dice has outcome from 1 to 6 with equal probabilities; functions to use: rand, floor, ceil, … n 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,
Programming in Scilab n Click on menu bar to open Scipad; then write your scilab function file. n Format of a function: function [out1, out2,...] = name(in1, in2,...) (body of function definition; may have many lines) endfunction n One file may contain more than one function. n To use the functions, you must load the function file by choosing File -> Execute the file from the menu.
Programming in Scilab (2) n A simple function to find the n-th term of the Fibonnaci sequence 0, 1, 1, 2, 3, 5, 8, 13, 21, … n function k = fibo(n) if n == 1, k = 0; elseif n==2, k = 1; else k = fibo(n-1) + fibo(n-2); end endfunction n Save the file as fibo.sci (or any other file name). n Execute it from Scilab menu bar n Try, say: > fibo(5), fibo(2), fibo(10), fibo(100)
Programming in Scilab (3) n 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 2 K = fibonacci(n-1) + fibonacci(n-2); else disp('error! -- input is not a positive integer'); end endfunction
Programming in Scilab (4) n 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. n For each time you do the experiment, you’ll get a number k. Therefore you should get m numbers k 1, k 2, …, k m at the end. n Inputs of the function are m, n; output is a vector k = [ k 1 k 2 … k m ].
Recap We have discussed and learned the following: n What Scilab is n Basic usage of Scilab (as a calculator) n Vectors and Matrices in Scilab n Solving linear equations n Simulation of some random events n Basic Scilab programming