16

SCILAB - content.kopykitab.com · early versions of such an alternative - SCILAB - appeared in 90‘s itself, it was not until much later that it acquired the attention it deserved

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

SCILAB(A Free Software to MATLAB)

SCILAB(A Free Software to MATLAB)

Er. HEMA RAMACHANDRAN B.Tech., M.Tech., M.Phil., PG Diploma (Software Engg.)

Speed-IT Research Fellow, College of Engineering, Trivandrum

Former Principal, University College of Engineering, University of Kerala,

Kerala

Dr. ACHUTHSANKAR S. NAIR B.Tech., M.Tech., M.Phil. (Engg.), Ph.D. (Elec. Engg.)

Director, State Inter-University Centre of Excellence in Bio-informatics University of Kerala, Trivandrum

Former Director, C-DIT, Government of KeralaKerala

S. CHAND & COMPANY LTD.(AN ISO 9001 : 2008 COMPANY)

RAM NAGAR, NEW DELHI - 110055

S. CHAND & COMPANY LTD.(An ISO 9001 : 2008 Company)Head Office: 7361, RAM NAGAR, NEW DELHI - 110 055Phone: 23672080-81-82, 9899107446, 9911310888 Fax: 91-11-23677446Shop at: schandgroup.com; e-mail: [email protected]

Branches :AHMEDABAD : 1st Floor, Heritage, Near Gujarat Vidhyapeeth, Ashram Road, Ahmedabad - 380 014,

Ph: 27541965, 27542369, [email protected] : No. 6, Ahuja Chambers, 1st Cross, Kumara Krupa Road, Bengaluru - 560 001,

Ph: 22268048, 22354008, [email protected] : Bajaj Tower, Plot No. 243, Lala Lajpat Rai Colony, Raisen Road, Bhopal - 462 011,

Ph: 4274723. [email protected] : S.C.O. 2419-20, First Floor, Sector - 22-C (Near Aroma Hotel), Chandigarh -160 022,

Ph: 2725443, 2725446, [email protected] : 152, Anna Salai, Chennai - 600 002, Ph: 28460026, 28460027, [email protected] COIMBATORE : No. 5, 30 Feet Road, Krishnasamy Nagar, Ramanathapuram, Coimbatore -641045,

Ph: 0422-2323620 [email protected] (Marketing Office)CUTTACK : 1st Floor, Bhartia Tower, Badambadi, Cuttack - 753 009, Ph: 2332580; 2332581,

[email protected] : 1st Floor, 20, New Road, Near Dwarka Store, Dehradun - 248 001,

Ph: 2711101, 2710861, [email protected] : Pan Bazar, Guwahati - 781 001, Ph: 2738811, 2735640 [email protected] : Padma Plaza, H.No. 3-4-630, Opp. Ratna College, Narayanaguda, Hyderabad - 500 029,

Ph: 24651135, 24744815, [email protected] : A-14, Janta Store Shopping Complex, University Marg, Bapu Nagar, Jaipur - 302 015,

Ph: 2719126, [email protected] : Mai Hiran Gate, Jalandhar - 144 008, Ph: 2401630, 5000630, [email protected] : 67/B, B-Block, Gandhi Nagar, Jammu - 180 004, (M) 09878651464 (Marketing Office)KOCHI : Kachapilly Square, Mullassery Canal Road, Ernakulam, Kochi - 682 011, Ph: 2378207,

[email protected] : 285/J, Bipin Bihari Ganguli Street, Kolkata - 700 012, Ph: 22367459, 22373914,

[email protected] : Mahabeer Market, 25 Gwynne Road, Aminabad, Lucknow - 226 018, Ph: 2626801, 2284815,

[email protected] : Blackie House, 103/5, Walchand Hirachand Marg, Opp. G.P.O., Mumbai - 400 001,

Ph: 22690881, 22610885, [email protected] : Karnal Bag, Model Mill Chowk, Umrer Road, Nagpur - 440 032, Ph: 2723901, 2777666

[email protected] : 104, Citicentre Ashok, Govind Mitra Road, Patna - 800 004, Ph: 2300489, 2302100,

[email protected] : 291/1, Ganesh Gayatri Complex, 1st Floor, Somwarpeth, Near Jain Mandir,

Pune - 411 011, Ph: 64017298, [email protected] (Marketing Office)RAIPUR : Kailash Residency, Plot No. 4B, Bottle House Road, Shankar Nagar, Raipur - 492 007,

Ph: 09981200834, [email protected] (Marketing Office)RANCHI : Flat No. 104, Sri Draupadi Smriti Apartments, East of Jaipal Singh Stadium, Neel Ratan Street,

Upper Bazar, Ranchi - 834 001, Ph: 2208761, [email protected] (Marketing Office)SILIGURI : 122, Raja Ram Mohan Roy Road, East Vivekanandapally, P.O., Siliguri-734001,

Dist., Jalpaiguri, (W.B.) Ph. 0353-2520750 (Marketing Office)VISAKHAPATNAM : Plot No. 7, 1st Floor, Allipuram Extension, Opp. Radhakrishna Towers, Seethammadhara North

Extn., Visakhapatnam - 530 013, (M) 09347580841, [email protected] (Marketing Office)

© 2012, Er. Hema Ramachandran and Dr. Achuthsankar S. NairAll rights reserved. No part of this publication may be reproduced or copied in any material form (including photo copying or storing it in any medium in form of graphics, electronic or mechanical means and whether or not transient or incidental to some other use of this publication) without written permission of the copyright owner. Any breach of this will entail legal action and prosecution without further notice.Jurisdiction : All desputes with respect to this publication shall be subject to the jurisdiction of the Courts, tribunals and forums of New Delhi, India only.

First Edition 2012

ISBN : 81-219-3970-4 Code : 20 139printed in indiaBy Rajendra Ravindra Printers Pvt. Ltd., 7361, Ram Nagar, New Delhi -110 055 and published by S. Chand & Company Ltd., 7361, Ram Nagar, New Delhi -110 055.

PREFACETechnical and Scientific Computing is an important application area of computer

science and has been heavily centred around MATLABS - hailed very often as the language of technical and scientific computing. This “matrix laboratory” has been around for almost 3 decades and has found a permanent place not only in curricula of applied science and engineering studies, but also in research and development arena. While there is no doubt that MATLAB has been emerging stronger and stronger, with the acceleration in popularity of free-software movement all over the world, need was felt for a free-software alternative to MATLAB. Though the early versions of such an alternative - SCILAB - appeared in 90‘s itself, it was not until much later that it acquired the attention it deserved. Today it has picked up sufficient momentum by establishing its presence in the field, especially academia. However, its onward march is hampered by the lack of a comprehensive primer introducing it. There are some very nicely written tutorials and user guides, good in patches and not covering the core of features. This book is planned to fill that lacunae. We have modelled it after the standard primers in MATLAB and hence would by easy speed-reading for MATLAB users and also for elaborate study of first-timers in scientific computing. We have chosen statistics, image processing and Scicos tool boxes as special treatment, mainly aimed at hardcore science and technology workers. Exercises appear in all chapters to suit academic use of the book.

We would like to thank Dr. Claude Gomez, Director of Scilab Consortium, France, for his encouragement in publishing this book. Of course we are grateful to our students which were the seeds of this book. We benefitted immensely by their critical feedback and also appreciation. We look forward to critical and creative feedback as a fuel for improvement in future editions.

We hope this book finds it way to not only class rooms but also research labs. We enjoyed writing this book and we hope the reader also has an enjoyable reading experience.

AUTHORS

Disclaimer : While the authors of this book has made every effort to avoid any mistake or omission and have used their skill, expertise and knowledge to the best of their capacity to provide accurate and updated information. The authors and S. Chand does not give any representation or warranty with respect to the accuracy or completeness of the contents of this publication and are selling this publication on the condition and understanding that they shall not be made liable in any manner whatsoever. S.Chand and the authors expressly disclaim all and any liability/responsibility to any person, whether a purchaser or reader of this publication or not, in respect of anything and everything forming part of the contents of this publication. S. Chand shall not be responsible for any errors, omissions or damages arising out of the use of the information contained in this publication.Further, the appearance of the personal name, location, place and incidence, if any; in the illustrations used herein is purely coincidental and work of imagination. Thus the same should in no manner be termed as defamatory to any individual.

CONTENTS

1. INTRODUCTION TO SCILAB 1–14 1.1 What is Scilab ? 1 1.2 Matlab 1 1.3 Free Software 2 1.4 History of Scilab 3 1.5 Licence of Scilab 4 1.6 Downloading & Installing Scilab 4 1.7 Organisation of the book 7 1.8 A Quick taste of Scilab 7 Exercise 14

2. THE SCILAB ENVIRONMENT 15–24 2.1 Introduction 15 2.2. Manipulating the command line 15 2.3 Working Directory 15 2.4 Comments 16 2.5 Variables in Memory 16 2.6 Recording Sessions 18 2.7 Help 19 2.8 Startup Commands 20 2.9 The Scilab Menu Bar 20 2.9.1 File Menu 20 2.9.2 Edit Menu 20 2.9.3 Preferences Menu 21 2.9.4 Control Menu 21 2.9.5 Applications Menu 21 2.9.6 ? Menu 22 2.9.7 Toolboxes 22 2.10 Demos 23 Exercise 24

3. SCALARS & VECTORS 25–35 3.1 Introduction 25 3.2 Initialising vectors in Scilab 25 3.3 Mathematical operations on vectors 26 3.3.1 Addition 27

(vii)

3.3.2 Subtraction 27 3.3.3 Multiplication 27 3.3.4 Division 27 3.3.5 Element wise multiplication 27 3.3.6 Element wise division 27 3.3.7 Transpose 28 3.4 Relational operations on vectors 28 3.4.1 “less than” relational operator < 28 3.4.2 “less than or equal to” relational operator <= 28 3.4.3 “greater than” relational operator > 28 3.4.4 “greater than or equal to” relational operator >= 28 3.4.5 “equal to” relational operator == 28 3.4.6 “not equal to” relational operator ~ = 28 3.5 Logical operations on vectors 29 3.5.1 Logical AND operator (&) 29 3.5.2 Logical OR operator (|) 29 3.5.3 Logical Complement (~ ) 29 3.6 Built- in logical functions 29 3.6.1 isempty ( ) 29 3.6.2 find() 29 3.6.3 isreal ( ) 29 3.6.1 isglobal ( ) 30

3.7 Elementary Mathematical Functions 30 3.7.1 clean ( ) 30 3.7.3 ceil () 30 3.7.4 floor() 30 3.7.5 round ( ) 30 3.7.6 fix() 31 3.7.7 sign ( ) 31 3.8 Mathematical functions on scalars 31 3.8.1 modulo ( ) 31 3.8.2 rat ( ) 31 3.8.3 sqrt ( ) 32 3.8.4 exp() 32 3.8.5 log ( ) 32 3.8.6 log10 ( ) 32 3.8.7 log2 ( ) 32 3. 9 Complex numbers 32 3.10 Trigonometric functions 33 3.11 Inverse trigonometric functions 34 3.12 Hyperbolic functions 34 Exercise 35

(viii)

4. MATRICES 36–46 4.1 Introduction 36 4.2 Arithmetic operators for Matrices 37 4.2.1 Basic arithmetic 37 4.2.2 Element wise arithmetic 38 4.3 Basic matrix processing 39 4.3.1 Matrices with various data types 39 4.3.2 Basic Matrix Operations 42 Exercise 46

5. PROGRAMMING IN SCILAB 47–63 5.1 Introduction 47 5.2 Variables & Variable names 47 5.3 Assignment statements 48 5.4 Arithmetic, Relational & Logical operators 48 5.5 Input & Output 48 5.6 Flow control/branching /conditional statements 50 5.6.1 if/if - else statement 51 5.6.2 Select-case statement 52 5.6.3 for loops 55 5.6.4 Nesting of loops 55 5.6.5 While loop 56 5.7 Break and continue 56 5.8 Handling Matrices With Loops 58 5.9 Scripts 61 5.10 The Concept of Functions 61 5.11 UserDefinedFunctions 62 5.12 Special Function conamands 63 Exercise 63

6. POLYNOMIALS 66–70 6.1 Introduction 66 6.2 Creating Polynomials 66 6.3 Basic polynomial commands 67 6.4 Finding Roots of a Polynomial 67 6.5 Polynomial Arithmetic 68 6.6 Miscellaneous Polynomial Handling 69 Exercise 70

7. MENUS AND DIALOG BOXES 71–77 7.1 Introduction 71 7.2 A simple menu example 71 7.3 Scilab window with ‘Greetings menu added‘. 72

(ix)

7.4 Executing submeans from command line 72 7.5 LinkingmenustoScilabcodefromexternalfiles 72 7.6 Entering data through dialog boxes 73 7.7 Printing a message in a message box 75 7.8 Dialog box for entering a matrix 77 Exercise 77

8. GRAPHIC OUTPUT 78–95 8.1 Introduction 78 8.2 2d Plotting 80 8.2.1 plot command 80 8.2.2 Plot 2D 83 8.2.3 Plot2d2 84 8.2.4 plot2d3 85 8.2.5 Histplot, Matplot & Grayplot 85 8.2.6 Grayplot 86 8.3 Function versions for graphic commands 87 8.4 3d plotting 87 8.4.1 plot3d 88 8.4.2 plot3d1 88 8.4.5 Contour 89 8.4.6 Hist3D 89 8.5 Other Graphic Primitives 90 8.5.1 Line & polygon plotting 90 8.5.2 Rectangle plotting 93 8.5.3 Arc plotting 94 8.5.4 Strings 95 8.5.5 Save and load 95 8.6 Other Graphic Commands 95 Exercise 95

9. STRING HANDLING FUNCTIONS 96–100 9.1 Symbolic Processing in Scilab 96 9.2 Creation of a linear combination of arguments 97 9.3 String to ASCII conversion 97 9.4 Creation of a string of blank characters 97 9.5 Conversion of strings to uppercase and lower case 98 9.6 String matching 98 9.7 String concatenation 98 9.8 Reversing a string 98 9.9 Replacement of a string by another 99 9.10 Length of a string 99 9.11 Type checking 99 Exercise 100

(x)

10. STATISTICS 101–113 10.1 Introduction 101 10.2 Basic Statistical Functions 101 10.2.1 Mean of a vector 101 10.2.2 Weighted mean of a vector or a matrix 101 10.2.3 Median of a vector 101 10.2.4. Sum of a Vector 102 10.2.5 Geometric mean of a vector 102 10.2.6 Harmonic mean of a vector 102 10.2.7 Standard Deviation of a vector 102 10.2.8 Mean absolute deviation 102 10.2.9 Variance of a vector 102 10.2.10 Sorting a vector 102 10.2.11 Smallest element of Vector, 103 10.2.12 Largest element of a vector 103 10.2.13 Quartile of a vector 103 10.2.14 Inter Quartile ranges of a vector 103 10.3 Applying Statistical Functions on Matrices 103 10.4 Distributions 104 10.5 Frequency of values of a matrix or vector 105 10.6 Centre 106 10.7 Weighted centre 107 10.8 Central moment 107 10.9 Correlation 107 10.10 Covariance 107 10.11 Variance-Covariance Matrix 108 10.12 Percentiles 108 10.13 Frequencies 108 10.14 Cumulative Sum 103 10.15 Difference Of Two Independent Samples 109 10.16 Fischer Test 109 10.17 Fischer Test For Unequal Samples 110 10.18 Principal Component Analysis 110 10.19 Miscellaneous 111 10.20 Sampling with replacement 111 10.21 Sample with Replacement 112 10.22 Labostat Toolbox 113

11. IMAGE PROCESSING USING 114–126 11.1 Introduction 114 11.2 Basic idea of digital images 114 11.3 Installation on Windows 114

(ix)

11.4 Basic Image processing 115 11.4 Image arithmetic 118 11.5 Adding Noise to Images 121 11.6 Filtering 122

12. SCICOS TOOL BOX FUNCTIONS 127–140 12.1 Introduction 127 12.2 Installing Scicos Lab 4.3 127 12.3DefiningTransferFunctionsandSimulatingit 131 12.4 Root locus by Evans method 134 12.5 Bode plot 136 12.6 State space representation in Scicos 137 12.7 Batch functions 139 Exercise 139

13. SCICOS VISUAL EDITOR 141–156 13.1 Dynamic system modeling and simulation using Scicos 141 13.2 Familarisation with Scicos and its Blocks 141 13.3 Source & Sinks Palettes 143 13.4 Linear and Non-linear palettes 144 13.5 Branching palettes 145 13.6 Events palettes 146 13.7 Demo Blocks Palettes 147 13.8 The Old Blocks Palettes 147 13.9 O Thers Palette 148 13.10 Handling Scicos Blocks 148 13.10.1 Transfer function block 148 13.10.2 Square wave generator (source palettes) 148 13.10.3 MUX - Multiplexer from Branching palette 149 13.10.4 LOGICAL_OP comes under the category of Others palette 149 13.10.5 Threshold negative to positive 149 13.10.6. Display block comes under the category of sink palettes 149

13.10.7 CLOCK comes under the category of Events palette 150 13.10.8 Capacitor 150 13.10.9 Inductor 150 13.10.10 Bache 151 13.10.11.Dflipflop 151 13.11 A Sample Simulation 151

13.12 Demo Simulations From Scicos 153 13.13 XCOS 156 Exercise 156

APPENDIX : A Full Text of SCILAB License 157–200

(ix)

1

1C

HA

PT

ER

Introduction to Scilab

1.1 WHAT IS SCILAB ?If you are already familiar with the software package known as Matlab and you are also aware

of the free software movement, then it is easy to answer the question “What is Scilab?’’ - Scilab is a free software alternative to Matlab. For the sake of readers who may be unfamiliar with either or both of Matlab and free softwares, we present in this chapter a brief introduction of the same. However, the reader may also skip this chapter in the fi rst reading and begin with the second chapter, and gain a fi rst hand understanding of Scilab.

1.2 MATLABMatlab, believed to be used by over a million users in industry and academia, appeared in

late 1970s and has become a hot favourite of users who are either not skilled or simply uninterested in getting entangled with syntax of languages like C or Pascal, even to solve simple problems. Matlab is hailed as the language of technical computing and often described as a “Quick and Dirty” programming language. The interpreted nature makes it “quick” and the “dirty” part indicates its fl exibility of syntax, which programming puritans might not agree with. Formally, Matlab is a numerical computing environment and fourth generation programming language. It is developed by The MathWorks. Among Mathlab’s capabilities, the key is matrix manipulation (the name Matlab itself is an acronymn of Matrix Laboratory), handling of functions and data, implementation of algorithms and creation of user interfaces. Matlab is well known for the many tool boxes (specialized collection of commands and features which can be added on to basic Matlab), in specialized areas of science, technology, mathematics, statistics etc. Release 7.8 is in vogue in 2011.

Fig 1.1 Logo of Matlab

2 SCILAB (A free Software alternative to MATLAB)

Fig 1.2 Matlab window

Matlab is a proprietary software and a licence fee is mandatory for its use. The tool boxes are optional and each comes under its own licence fee. As we will see in the next section, the free software movement attempts to offer technically, ethically superior and free alternatives to proprietary softwares. There are many free open source alternatives to Matlab, in particular GNU Octave, FreeMat, and Scilab. They attempt to be compatible with the Matlab language. The statistical language S also can be considered in this category as it treats arrays as basic entities. The open source language R is an implementation of S.

1.3 FREE SOFTWAREWikipaedia defines free software as follows; “Free software or software libre is software that

can be used, studied, and modified without restriction, and which can be copied and redistributed in modified or unmodified form either without restriction, or with minimal restrictions only to ensure that further recipients can also do these things and that manufacturers of consumer-facing hardware allow user modifications to their hardware. Free software is available gratis (free of charge) in most cases”. Free software ideas originated from Richard Stallman who conceived the free software movement in 1983. The Free Software Foundation was founded soon after to advance Stallman’s free software ideas. It may be noted that there are currently alternative terms for free software such as “software libre”, “Free and Open Source Software” (“FOSS”) and “Free, Libre and Open Source Software” (“FLOSS”).

A free software generally permits 4 kinds of freedom to its users: The freedom to run the software anywhere, anyway The freedom to copy and distribute The freedom of access to the source code of the software The freedom to modify and redistribute

A philosophical discussion of free software ideology is beyond the scope of this book.

SCILAB (A Free Software To MATLAB)

Publisher : SChand Publications ISBN : 9788121939706Author : Er. HemaRamachandran, Dr.Achuthsankar S. Nair

Type the URL : http://www.kopykitab.com/product/11700

Get this eBook

20%OFF