An Introduction to Scilab Tsing Nam Kiu 丁南僑 Department of Mathematics The University of Hong Kong 2009 January 7.

Slides:



Advertisements
Similar presentations
StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013.
Advertisements

Lecture 5.
259 Lecture 17 Working with Data in MATLAB. Overview  In this lecture, we’ll look at some commands that are useful for working with data!  fzero  sum,
Functions. COMP104 Functions / Slide 2 Introduction to Functions * A complex problem is often easier to solve by dividing it into several smaller parts,
Introduction to Matlab
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 10.
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
CSE 123 Symbolic Processing. Declaring Symbolic Variables and Constants To enable symbolic processing, the variables and constants involved must first.
Lesson  In this investigation you will explore the predictability of random outcomes. You will use a familiar random process, the flip of a coin.
Introduction to Matlab Οικονομίδης Δημήτρης
ICS 103 Lab 2-Arithmetic Expressions. Lab Objectives Learn different arithmetic operators Learn different arithmetic operators Learn how to use arithmetic.
Solving Quadratic Equations Tammy Wallace Varina High.
Introduction to MATLAB ENGR 1187 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
What is MATLAB ? MATrix LABratory –Originally, it was a front-end to FORTRAN matrix routines developed in the U. of New Mexico and Stanford –Today.
Chapter 10 Review: Matrix Algebra
MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson.
Martin Ellison University of Warwick and CEPR Bank of England, December 2005 Introduction to MATLAB.
Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling.
1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.
Introduction to MATLAB January 18, 2008 Steve Gu Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,
MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Dept of Mechanical Engineering LSU.
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) MATH Functions in C Language.
Math 15 Lecture 7 University of California, Merced Scilab A “Very” Short Introduction.
Math 15 Lecture 10 University of California, Merced Scilab Programming – No. 1.
A Brief Introduction to Matlab Laila Guessous Dept. of Mechanical Engineering Oakland University.
Introduction to Engineering MATLAB – 1 Introduction to MATLAB Agenda Introduction Arithmetic Operations MATLAB Windows Command Window Defining Variables.
INTRODUCTION TO MATLAB LAB# 01
Numerical Computation Lecture 2: Introduction to Matlab Programming United International College.
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Introduction to Engineering MATLAB – 2 Introduction to MATLAB - 2 Agenda Defining Variables MATLAB Windows.
Introduction to MATLAB
Scientific Computing Introduction to Matlab Programming.
Introduction to MATLAB Session 1 Simopekka Vänskä, THL 2010.
ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Module 4 Matlab ME 6104 – Fundamentals of Computer-Aided Design.
OUTLINE Overview Numbers, variables and similar in Matlab
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Recap Saving Plots Summary of Chapter 5 Introduction of Chapter 6.
MATLAB Lecture Two Tuesday 5 July Chapter 3.
Introduction to MATLAB II Steve Gu Jan 25, Outline Matrix Operation –Matrix functions –Element-wise operations Dynamic Systems –Classification –2nd.
Introduction to Matlab
Introduction to Matlab Electromagnetic Theory LAB by Engr. Mian Shahzad Iqbal.
MA/CS 375 Fall 2003 Lecture 3. .* Multiplication We can use the.* operator to perform multiplication entry by entry of two matrices:
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
MA/CS 375 Fall 2002 Lecture 2. Motivation for Suffering All This Math and Stuff Try the Actor demo from
Intro to Matlab Rogelio Long September 3, How to access MyDesktop Log in with your utep id and password.
Matlab Tutorial Iman Moazzen First Session – September 11, 2013.
Introduction to Matlab Engr. Mian Shahzad Iqbal LAB NO.2
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
전자장 1 실험 - Matlab 사용법 - Photonic Systems Laboratory School of EE, Seoul National University Photonic Systems Lab School of EECS, S.N.U.
Basic command Vectors and matrices in Scilab Data types: (real or complex) numbers, vectors, matrices, polynomials, strings, functions, … –Vectors in Scilab:
Outline What is MATLAB MATLAB desktop Variables, Vectors and Matrices Matrix operations Array operations Built-in functions: Scalar, Vector, Matrix Data.
Jake Blanchard University of Wisconsin Spring 2006.
Signals in Matlab Matlab as a name stands for Matrix Laboratory.
L – Modeling and Simulating Social Systems with MATLAB
Introduction to MATLAB
Seminar 1 for DCSP Using Matlab.
MATLAB DENC 2533 ECADD LAB 9.
Introduction to Matlab
Matlab Workshop 9/22/2018.
StatLab Matlab Workshop
MATH 493 Introduction to MATLAB
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
Introduction to MATLAB
Lecture 4 Structure plan
Introduction to Matlab
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
-seminar 1 for digital signal processing
Matlab Basics Tutorial
Matlab Basics.
Presentation transcript:

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