An Introduction to Scilab - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

An Introduction to Scilab

Description:

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 ... – PowerPoint PPT presentation

Number of Views:4744
Avg rating:3.0/5.0
Slides: 18
Provided by: admi715
Category:

less

Transcript and Presenter's Notes

Title: An Introduction to Scilab


1
An Introduction to Scilab
Tsing Nam Kiu ??? Department of Mathematics The
University of Hong Kong 2009 January 7
2
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.hknkt/Scilab/IntroToScilab.htm
    l

3
Using Scilab as a calculator
  • , , (multiplication), / (division),
    (power)
  • Examples
  • gt (12.34 0.03) / (2.8 1.2 3)
  • gt 23 or 222
  • gt 2 3
  • gt 2100
  • gt ans(1/100)

4
Using Scilab as a calculator (2)
  • Commonly used functions
  • cos, sin, tan, acos, asin, atan, sqrt, exp, log,
    log10
  • Solving quadratic equation x2 x10
  • gt a 1, b 1, c 1
  • gt ( a sqrt(b2 4ac))/(2a)
  • gt ( a sqrt(b2 4ac))/(2a)
  • A smarter way to find roots of polynomials
  • gt p poly(1 1 1,"x","coeff")
  • gt roots(p)

5
Using Scilab as a calculator (3)
  • special constants i, pi, e
  • gt tan(pi / 4)
  • gt e ( exp(1) )
  • gt (1i)(1--i)
  • Learning how to use Scilab and getting help
  • Click on ? on menu
  • gt help command
  • See documentation on Scilab website

6
Vectors and matrices in Scilab
  • Data types (real or complex) numbers, vectors,
    matrices, polynomials, strings, functions,
  • Vectors in Scilab
  • gt x 0 1 2 3
  • gt y 2 4 6 8
  • gt z 1 2 3 4
  • is conjugate transpose of a matrix
  • gt 3x, yz, yz
  • gt xy, x1

7
Vectors and matrices in Scilab (2)
  • Matrices in Scilab
  • gt A 0 1 0 1 2 3 4 0
  • gt B A
  • gt A y, x B, A B, B A, (BA)2
  • Special matrices (and vectors)
  • gt ones(2,3), zeros(1,2), eye(3,3)
  • gt rand, rand(3,2)
  • Empty vector or matrix gt a
  • Building matrix by blocks
  • gt C A 2A, x 9 x 7, a a 1

8
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
  • gt A 3 2 1 1 0 1 2 2 1
  • gt b 1 2 1
  • gt x inv(A)b (inv is inverse of a matrix)
  • gt x A \ b
  • Important remark theoretically it does not make
    sense to divide something by a matrix!

9
The colon operator
  • gt 110, 1100, xx 1100
  • Using to suppress answer output
  • gt sum(xx)
  • gt 1210, 3311, 411, 210,
  • gt t 0 0.1 2pi
  • gt y sin(t)
  • gt 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

10
Elements of vectors and matrices
  • Example
  • gt v rand(4,1)
  • gt v(1), v(3), v(2 4), v(4-11), v()
  • means the last entry
  • Example
  • gt A 1 2 3 4 5 6 7 8 9 10
  • gt A(2,3), A(1,), A(, 2), A(, 4 2)

11
Exercises
  • Task 2 simulate tossing of a coin
  • 0 head, 1 tail.
  • functions to use rand, round,
  • Task 3 simulate tossing of 100 coins

12
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,

13
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 -gt Execute the file from
    the menu.

14
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 n2, 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 gt fibo(5), fibo(2), fibo(10),
    fibo(100)

15
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 n1, K 0
  • elseif n2, K 1
  • elseif ngt2 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

16
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.

17
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
Write a Comment
User Comments (0)
About PowerShow.com