Scilab is a free mathematical software similar to Matlab. It can be used as a calculator to perform basic arithmetic and functions. Vectors and matrices can be created and manipulated using standard linear algebra operations. Systems of linear equations can be solved using inverse and backslash operators. Programming in Scilab allows defining custom functions to automate tasks like simulation and analysis of random processes. Basic examples were provided for coin tossing, dice throwing, and calculating Fibonacci numbers.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
107 views17 pages
WYK Scilab Talk
Scilab is a free mathematical software similar to Matlab. It can be used as a calculator to perform basic arithmetic and functions. Vectors and matrices can be created and manipulated using standard linear algebra operations. Systems of linear equations can be solved using inverse and backslash operators. Programming in Scilab allows defining custom functions to automate tasks like simulation and analysis of random processes. Basic examples were provided for coin tossing, dice throwing, and calculating Fibonacci numbers.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17
An Introduction to Scilab
Tsing Nam Kiu
Department of Mathematics The University of Hong Kong 2009 January 7 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~nkt/Scilab/IntroToScilab.html 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) 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) 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 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, yz > x+y, x+1 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] 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 sense to divide something by a matrix! 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 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]) Exercises Task 2: simulate tossing of a coin: 0 = head, 1 = tail. functions to use: rand, round,
Task 3: simulate tossing of 100 coins
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 Hs or Ts in the resulting sequence; functions to use: diff, find, max, 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 file by choosing File -> Execute the file from the menu. 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) 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 2 K = fibonacci(n-1) + fibonacci(n-2); else disp('error! -- input is not a positive integer'); end endfunction 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 Hs or Ts in the resulting sequence. For each time you do the experiment, youll get a number k. Therefore you should get m numbers k1, k2, , km at the end. Inputs of the function are m, n; output is a vector k = [ k1 k2 km]. Recap We have discussed and learned the following: What Scilab is