Top Banner
Introduction to Scilab Aditya Sengupta Indian Institute of Technology Bombay s[email protected] November 8, 2009
27

Introduction to Scilab · Introduction to Scilab Aditya Sengupta Indian Institute of Technology Bombay [email protected] November 8, 2009

Mar 24, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduction to Scilab · Introduction to Scilab Aditya Sengupta Indian Institute of Technology Bombay sengupta@ee.iitb.ac.in November 8, 2009

Introduction to Scilab

Aditya Sengupta

Indian Institute of Technology [email protected]

November 8, 2009

Page 2: Introduction to Scilab · Introduction to Scilab Aditya Sengupta Indian Institute of Technology Bombay sengupta@ee.iitb.ac.in November 8, 2009

Outline Introduction Scilab Objects: Matrices and Polynomials. Basic Programming Basic Input And Output Basic Graphics

Outline

1 Introduction

2 Scilab Objects: Matrices and Polynomials.

3 Basic Programming

4 Basic Input And Output

5 Basic Graphics

Aditya Sengupta, EE, IITB Scilab/Objects/Programming/Input & Output/Graphics

Page 3: Introduction to Scilab · Introduction to Scilab Aditya Sengupta Indian Institute of Technology Bombay sengupta@ee.iitb.ac.in November 8, 2009

Outline Introduction Scilab Objects: Matrices and Polynomials. Basic Programming Basic Input And Output Basic Graphics

About Scilab

Around since 1990

Numerical Computational package

Free and Open Source

Maintained by INRIA

Aditya Sengupta, EE, IITB Scilab/Objects/Programming/Input & Output/Graphics

Page 4: Introduction to Scilab · Introduction to Scilab Aditya Sengupta Indian Institute of Technology Bombay sengupta@ee.iitb.ac.in November 8, 2009

Outline Introduction Scilab Objects: Matrices and Polynomials. Basic Programming Basic Input And Output Basic Graphics

About Scilab

Inspired by Cleve Molers MATLAB

Interpreted

Very High LevelScilab : C = C : Assembly

Available for Linux, Mac and Windows

Aditya Sengupta, EE, IITB Scilab/Objects/Programming/Input & Output/Graphics

Page 5: Introduction to Scilab · Introduction to Scilab Aditya Sengupta Indian Institute of Technology Bombay sengupta@ee.iitb.ac.in November 8, 2009

Outline Introduction Scilab Objects: Matrices and Polynomials. Basic Programming Basic Input And Output Basic Graphics

Scilab Window looks like

Aditya Sengupta, EE, IITB Scilab/Objects/Programming/Input & Output/Graphics

Page 6: Introduction to Scilab · Introduction to Scilab Aditya Sengupta Indian Institute of Technology Bombay sengupta@ee.iitb.ac.in November 8, 2009

Outline Introduction Scilab Objects: Matrices and Polynomials. Basic Programming Basic Input And Output Basic Graphics

Try This Stuff

→ 42 + 4^2 - 64/4

→ a=1, b=2, c=3

→ a + b + c

→ institute = ‘‘IITB’’;

→ typeof(institute)

→ clear(‘‘institute’’)

→ exists(‘‘institute’’)

Aditya Sengupta, EE, IITB Scilab/Objects/Programming/Input & Output/Graphics

Page 7: Introduction to Scilab · Introduction to Scilab Aditya Sengupta Indian Institute of Technology Bombay sengupta@ee.iitb.ac.in November 8, 2009

Outline Introduction Scilab Objects: Matrices and Polynomials. Basic Programming Basic Input And Output Basic Graphics

Try This Stuff

→ 1/0

→ ieee(2)

→ 1/0

→ �e

→ sin(�pi/2), cos(�pi/2)

→ (10+5*�i)*(2*�i)

→ 2*cos(�pi/5)

Aditya Sengupta, EE, IITB Scilab/Objects/Programming/Input & Output/Graphics

Page 8: Introduction to Scilab · Introduction to Scilab Aditya Sengupta Indian Institute of Technology Bombay sengupta@ee.iitb.ac.in November 8, 2009

Outline Introduction Scilab Objects: Matrices and Polynomials. Basic Programming Basic Input And Output Basic Graphics

About Scilab

Everything is a matrix!

Even a real scalar is a 1× 1 matrix

You can define numbers, character strings, booleans,polynomials and lists

Aditya Sengupta, EE, IITB Scilab/Objects/Programming/Input & Output/Graphics

Page 9: Introduction to Scilab · Introduction to Scilab Aditya Sengupta Indian Institute of Technology Bombay sengupta@ee.iitb.ac.in November 8, 2009

Outline Introduction Scilab Objects: Matrices and Polynomials. Basic Programming Basic Input And Output Basic Graphics

Try This Stuff

→ a=[1 2 3], b=[2 3 4]

→ a’

→ a*b

→ a.*b

→ a’*b

→ a*b’

→ size(a)

→ length(a)

→ diag(a)

Aditya Sengupta, EE, IITB Scilab/Objects/Programming/Input & Output/Graphics

Page 10: Introduction to Scilab · Introduction to Scilab Aditya Sengupta Indian Institute of Technology Bombay sengupta@ee.iitb.ac.in November 8, 2009

Outline Introduction Scilab Objects: Matrices and Polynomials. Basic Programming Basic Input And Output Basic Graphics

Try This Stuff

→ A=[1 2; 0 4], B=[1 2; 3 4]

→ A+B, A-B

→ A*B, B*A, A.*B, B.*A

→ det(A)

→ inv(A)

→ size(A)

→ length(A)

→ diag(A)

Aditya Sengupta, EE, IITB Scilab/Objects/Programming/Input & Output/Graphics

Page 11: Introduction to Scilab · Introduction to Scilab Aditya Sengupta Indian Institute of Technology Bombay sengupta@ee.iitb.ac.in November 8, 2009

Outline Introduction Scilab Objects: Matrices and Polynomials. Basic Programming Basic Input And Output Basic Graphics

Try This Stuff

→ A=1:4 //This is a comment

→ B=2:2:8 // range

→ B([3 4]) // submatrix extraction

→ A(6)=6 // add an element// oops! Forgot the fifth element!

→ A($− 1)=5 // ‘‘ $’’ is last element

→ B=2*A // reassignment

→ B=[B 2*B; 3*B] // new rows

→ B($ + 1,:)=4*B(1,:)

→ B([2 3], 2:$− 1) //submatrix extraction

Aditya Sengupta, EE, IITB Scilab/Objects/Programming/Input & Output/Graphics

Page 12: Introduction to Scilab · Introduction to Scilab Aditya Sengupta Indian Institute of Technology Bombay sengupta@ee.iitb.ac.in November 8, 2009

Outline Introduction Scilab Objects: Matrices and Polynomials. Basic Programming Basic Input And Output Basic Graphics

Try This Stuff

→ pwd

→ cd(‘‘path-to-directory’’)

→ diary(‘‘ my-record-of-what-follows.sci’’)

→ help("diary")

→ inv([1 2; 0 4])

→ C=rand(3,3)

→ C>.5 // boolean matrix

→ find(C>.5)

→ C(find(C>.5))

→ disp(C)

Aditya Sengupta, EE, IITB Scilab/Objects/Programming/Input & Output/Graphics

Page 13: Introduction to Scilab · Introduction to Scilab Aditya Sengupta Indian Institute of Technology Bombay sengupta@ee.iitb.ac.in November 8, 2009

Outline Introduction Scilab Objects: Matrices and Polynomials. Basic Programming Basic Input And Output Basic Graphics

Try This Stuff

→ P=poly([2 3 1], ‘x’, ‘coeff’)

→ Q=poly([-1 4], ‘x’)

→ P*Q, P+Q, P-Q

→ roots(P), roots(Q), roots(P*Q)

→ factors(P), factors(Q), factors(P*Q)

→ 1/P

→ Q/P

→ derivat(P), derivat(Q), derivat(Q/P)

→ horner(P, 0), horner(P, [0 1 2]) //to evaluate ata value or a set of values

Aditya Sengupta, EE, IITB Scilab/Objects/Programming/Input & Output/Graphics

Page 14: Introduction to Scilab · Introduction to Scilab Aditya Sengupta Indian Institute of Technology Bombay sengupta@ee.iitb.ac.in November 8, 2009

Outline Introduction Scilab Objects: Matrices and Polynomials. Basic Programming Basic Input And Output Basic Graphics

Conventions

Commands may be put in scripts.

Extension is .sce

If it only contains function definitions, the extension is .sci

These are conventions!

Execute:exec(‘path-to-script/script-name.sce’)

Aditya Sengupta, EE, IITB Scilab/Objects/Programming/Input & Output/Graphics

Page 15: Introduction to Scilab · Introduction to Scilab Aditya Sengupta Indian Institute of Technology Bombay sengupta@ee.iitb.ac.in November 8, 2009

Outline Introduction Scilab Objects: Matrices and Polynomials. Basic Programming Basic Input And Output Basic Graphics

Functions

function [y1, y2, ...]=foo(x1, x2, ...)statementstatementstatement

endfunctionOR

deff(‘‘[y]=foo([x])’’, ‘‘statements’’)

Aditya Sengupta, EE, IITB Scilab/Objects/Programming/Input & Output/Graphics

Page 16: Introduction to Scilab · Introduction to Scilab Aditya Sengupta Indian Institute of Technology Bombay sengupta@ee.iitb.ac.in November 8, 2009

Outline Introduction Scilab Objects: Matrices and Polynomials. Basic Programming Basic Input And Output Basic Graphics

Functions

If function definitions are in a script file, usegetf(‘path/script.sci’)

To see the source of a Scilab coded function usefun2string(function-name)

Aditya Sengupta, EE, IITB Scilab/Objects/Programming/Input & Output/Graphics

Page 17: Introduction to Scilab · Introduction to Scilab Aditya Sengupta Indian Institute of Technology Bombay sengupta@ee.iitb.ac.in November 8, 2009

Outline Introduction Scilab Objects: Matrices and Polynomials. Basic Programming Basic Input And Output Basic Graphics

Branching

if condition thenstatementstatementstatement

elsestatementstatementstatement

end

Aditya Sengupta, EE, IITB Scilab/Objects/Programming/Input & Output/Graphics

Page 18: Introduction to Scilab · Introduction to Scilab Aditya Sengupta Indian Institute of Technology Bombay sengupta@ee.iitb.ac.in November 8, 2009

Outline Introduction Scilab Objects: Matrices and Polynomials. Basic Programming Basic Input And Output Basic Graphics

Iterations

for name = expressionstatementstatementstatement

end// Use break to stop execution within statement block

Aditya Sengupta, EE, IITB Scilab/Objects/Programming/Input & Output/Graphics

Page 19: Introduction to Scilab · Introduction to Scilab Aditya Sengupta Indian Institute of Technology Bombay sengupta@ee.iitb.ac.in November 8, 2009

Outline Introduction Scilab Objects: Matrices and Polynomials. Basic Programming Basic Input And Output Basic Graphics

Iterations

while conditionstatementstatementstatement

// Use break to stop execution within statement block

Aditya Sengupta, EE, IITB Scilab/Objects/Programming/Input & Output/Graphics

Page 20: Introduction to Scilab · Introduction to Scilab Aditya Sengupta Indian Institute of Technology Bombay sengupta@ee.iitb.ac.in November 8, 2009

Outline Introduction Scilab Objects: Matrices and Polynomials. Basic Programming Basic Input And Output Basic Graphics

Try This Stuff

function y = myfactorial(x)if x==0 then y=1else y = x*myfactorial(x-1)end

endfunction

Aditya Sengupta, EE, IITB Scilab/Objects/Programming/Input & Output/Graphics

Page 21: Introduction to Scilab · Introduction to Scilab Aditya Sengupta Indian Institute of Technology Bombay sengupta@ee.iitb.ac.in November 8, 2009

Outline Introduction Scilab Objects: Matrices and Polynomials. Basic Programming Basic Input And Output Basic Graphics

Try This Stuff

→ // try a few examples:

→ myfactorial(5), myfactorial(0)

→ // now try Scilabs own function:

→ factorial(5), factorial(0)

Aditya Sengupta, EE, IITB Scilab/Objects/Programming/Input & Output/Graphics

Page 22: Introduction to Scilab · Introduction to Scilab Aditya Sengupta Indian Institute of Technology Bombay sengupta@ee.iitb.ac.in November 8, 2009

Outline Introduction Scilab Objects: Matrices and Polynomials. Basic Programming Basic Input And Output Basic Graphics

Input

→ name=input(‘‘Enter your name: ’’)

→ // oops (try entering your name in “ ”)

→ or try this:

→ name=input(‘‘Enter your name: ’’, ‘‘string’’)

→ disp(name);

→ more comfortable with C? try this:

→ mprintf(‘‘Your name is �s’’, name)

Aditya Sengupta, EE, IITB Scilab/Objects/Programming/Input & Output/Graphics

Page 23: Introduction to Scilab · Introduction to Scilab Aditya Sengupta Indian Institute of Technology Bombay sengupta@ee.iitb.ac.in November 8, 2009

Outline Introduction Scilab Objects: Matrices and Polynomials. Basic Programming Basic Input And Output Basic Graphics

[Optional] Look these up in help:

→ mopen

→ mprintf

→ mfprintf

→ mscanf

→ mfscanf

→ mclose

Aditya Sengupta, EE, IITB Scilab/Objects/Programming/Input & Output/Graphics

Page 24: Introduction to Scilab · Introduction to Scilab Aditya Sengupta Indian Institute of Technology Bombay sengupta@ee.iitb.ac.in November 8, 2009

Outline Introduction Scilab Objects: Matrices and Polynomials. Basic Programming Basic Input And Output Basic Graphics

plot2d

→ x=linspace(-�pi, �pi, 40)

→ plot2d(x, sin(x))

→ //Try getting the axes in the centre

→ //Don’t like the continuous version?

→ plot2d3(x,sin(x))

Aditya Sengupta, EE, IITB Scilab/Objects/Programming/Input & Output/Graphics

Page 25: Introduction to Scilab · Introduction to Scilab Aditya Sengupta Indian Institute of Technology Bombay sengupta@ee.iitb.ac.in November 8, 2009

Outline Introduction Scilab Objects: Matrices and Polynomials. Basic Programming Basic Input And Output Basic Graphics

plot3d

→ y=x

→ plot3d(x, y, sin(x)’*cos(y))— Notice the transpose

Aditya Sengupta, EE, IITB Scilab/Objects/Programming/Input & Output/Graphics

Page 26: Introduction to Scilab · Introduction to Scilab Aditya Sengupta Indian Institute of Technology Bombay sengupta@ee.iitb.ac.in November 8, 2009

Outline Introduction Scilab Objects: Matrices and Polynomials. Basic Programming Basic Input And Output Basic Graphics

z=sin(x)’*cos(y)

Aditya Sengupta, EE, IITB Scilab/Objects/Programming/Input & Output/Graphics

Page 27: Introduction to Scilab · Introduction to Scilab Aditya Sengupta Indian Institute of Technology Bombay sengupta@ee.iitb.ac.in November 8, 2009

Outline Introduction Scilab Objects: Matrices and Polynomials. Basic Programming Basic Input And Output Basic Graphics

Thank You!

www.scilab.org

www.scilab.in

http://scilab.in/cgi-bin/mailman/listinfo/scilab-india

“Modeling and Simulation in Scilab/Scicos” by StephenL.Campbell, Jean-Philippe Chancelier and Ramine Nikoukah,(Springer)

Aditya Sengupta, EE, IITB Scilab/Objects/Programming/Input & Output/Graphics