A brief introduction to SCILAB

Similar documents
Dr Richard Greenaway

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

Finding, Starting and Using Matlab

McTutorial: A MATLAB Tutorial

Introduction to Engineering gii

Desktop Command window

Laboratory 1 Octave Tutorial

EE3TP4: Signals and Systems Lab 1: Introduction to Matlab Tim Davidson Ext Objective. Report. Introduction to Matlab

CITS2401 Computer Analysis & Visualisation

Introduction to MATLAB

Introduction to MATLAB

Introduction to MATLAB

AN INTRODUCTION TO MATLAB

ELEC4042 Signal Processing 2 MATLAB Review (prepared by A/Prof Ambikairajah)

EE 301 Signals & Systems I MATLAB Tutorial with Questions

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

Experiment 1: Introduction to MATLAB I. Introduction. 1.1 Objectives and Expectations: 1.2 What is MATLAB?

EGR 111 Introduction to MATLAB

What is MATLAB and howtostart it up?

Dr Richard Greenaway

Introduction to MATLAB. Simon O Keefe Non-Standard Computation Group

A Guide to Using Some Basic MATLAB Functions

Introduction to MATLAB

ECON 502 INTRODUCTION TO MATLAB Nov 9, 2007 TA: Murat Koyuncu

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT

MATLAB Tutorial. Digital Signal Processing. Course Details. Topics. MATLAB Environment. Introduction. Digital Signal Processing (DSP)

Computer Programming in MATLAB

Appendix A. Introduction to MATLAB. A.1 What Is MATLAB?

Introduction to MATLAB Programming

Scilab Programming. The open source platform for numerical computation. Satish Annigeri Ph.D.

Introduction to MATLAB

MATLAB Programming for Numerical Computation Dr. Niket Kaisare Department Of Chemical Engineering Indian Institute of Technology, Madras

A QUICK INTRODUCTION TO MATLAB

Appendix A: MATLAB Basics

A QUICK INTRODUCTION TO MATLAB. Intro to matlab getting started

Lecturer: Keyvan Dehmamy

Introduction. Matlab for Psychologists. Overview. Coding v. button clicking. Hello, nice to meet you. Variables

STAT/MATH 395 A - PROBABILITY II UW Winter Quarter Matlab Tutorial

Variable Definition and Statement Suppression You can create your own variables, and assign them values using = >> a = a = 3.

A very brief Matlab introduction

ARRAY VARIABLES (ROW VECTORS)

Excel R Tips. is used for multiplication. + is used for addition. is used for subtraction. / is used for division

MATLAB Basics EE107: COMMUNICATION SYSTEMS HUSSAIN ELKOTBY

LAB 1 General MATLAB Information 1

Lecture 2 FORTRAN Basics. Lubna Ahmed

ENGR Fall Exam 1

An Introduction to MATLAB and the Control Systems toolbox Aravind Parchuri, Darren Hon and Albert Honein

Part #1. A0B17MTB Matlab. Miloslav Čapek Filip Kozák, Viktor Adler, Pavel Valtr

Digital Image Analysis and Processing CPE

MATH 3511 Basics of MATLAB

OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors.

QUICK INTRODUCTION TO MATLAB PART I

Chapter 1 MATLAB Preliminaries

Physics 326G Winter Class 2. In this class you will learn how to define and work with arrays or vectors.

A General Introduction to Matlab

Getting started with MATLAB

Introduction to Scientific and Engineering Computing, BIL108E. Karaman

YOGYAKARTA STATE UNIVERSITY MATHEMATICS AND NATURAL SCIENCES FACULTY MATHEMATICS EDUCATION STUDY PROGRAM

1 Introduction to MATLAB

MATLAB TUTORIAL WORKSHEET

MATLAB = MATrix LABoratory. Interactive system. Basic data element is an array that does not require dimensioning.

Built-in Types of Data

The Graphing Calculator

Macro Programming Reference Guide. Copyright 2005 Scott Martinez

MATLAB Fundamentals. Berlin Chen Department of Computer Science & Information Engineering National Taiwan Normal University

Class #15: Experiment Introduction to Matlab

MATLAB Lesson I. Chiara Lelli. October 2, Politecnico di Milano

ENGR Fall Exam 1

Attia, John Okyere. Control Statements. Electronics and Circuit Analysis using MATLAB. Ed. John Okyere Attia Boca Raton: CRC Press LLC, 1999

Introduction to the workbook and spreadsheet

1 Introduction to MATLAB

Creates a 1 X 1 matrix (scalar) with a value of 1 in the column 1, row 1 position and prints the matrix aaa in the command window.

This is a basic tutorial for the MATLAB program which is a high-performance language for technical computing for platforms:

MATH 5520 Basics of MATLAB

Math 2250 MATLAB TUTORIAL Fall 2005

PIV Programming. Today s Contents: 1. Matlab Programming 2. An example of PIV in Matlab code 3. EDPIV 4. PIV plugin for ImageJ 5.

Dr. Nahid Sanzida b e. uet .ac.

2.0 MATLAB Fundamentals

Introduction to MATLAB

Introduction to MATLAB

Laboratory 1 Introduction to MATLAB for Signals and Systems

Numerical Methods Lecture 1

MATLAB QUICK START TUTORIAL

MATLAB: The Basics. Dmitry Adamskiy 9 November 2011

Matlab Workshop I. Niloufer Mackey and Lixin Shen

A GUIDE FOR USING MATLAB IN COMPUTER SCIENCE AND COMPUTER ENGINEERING TABLE OF CONTENTS

Matlab Tutorial. The value assigned to a variable can be checked by simply typing in the variable name:

Matlab- Command Window Operations, Scalars and Arrays

Matrices 4: use of MATLAB

ü 1.1 Getting Started

EE 350. Continuous-Time Linear Systems. Recitation 1. 1

Some elements for Matlab programming

Starting Matlab. MATLAB Laboratory 09/09/10 Lecture. Command Window. Drives/Directories. Go to.

Introduction to Matlab

MATLAB NOTES. Matlab designed for numerical computing. Strongly oriented towards use of arrays, one and two dimensional.

Lab of COMP 406. MATLAB: Quick Start. Lab tutor : Gene Yu Zhao Mailbox: or Lab 1: 11th Sep, 2013

Introduction to MATLAB

MAT 275 Laboratory 1 Introduction to MATLAB

MATLAB for beginners. KiJung Yoon, 1. 1 Center for Learning and Memory, University of Texas at Austin, Austin, TX 78712, USA

Lab 1 Intro to MATLAB and FreeMat

Transcription:

A brief introduction to SCILAB SCILAB is a powerful and versatile package for mathematical modelling and an excellent tool for solving a wide range of engineering problems. SCILAB supports simple interactive calculation as well as complex and sophisticated programming. SCILAB offers a free opensource multi-platform alternative to the immensely popular Matlab package. The software is available for Windows, Linux and Mac-OS X and is actively supported by the community. SCILAB is easy to learn and will help you understand all of the intermediate steps in solving even the most complicated problems. This brief introduction is designed to provide you with a few pointers and examples to help you get started. Further information and full documentation is available from the SCILAB website (www.scilab.org). Commenting your code Comments can be very useful and you should get into the habit of using them liberally within your scripts. SCILAB comments are preceded by two forward slash characters (see below). Note that multiline comments are not used (each line of a multiline comment needs to be preceded by ). Flux produced by an inductor Comments can also be added to the end of a line of script, as follows: vmin = 11.5; Min supply voltage vmax = 13.8; Max supply voltage Note the use of the colon at the end of a line of script. Thus suppresses printing of the line of code when the script is executed. Script lines A long line of script can be broken with a line-wrap by using the ellipsis (...) at the end of each line to indicate that the command actually continues on the next line. These have not been used in the examples given in the book. Instead the ; character has been used to indicate the point at which a new line must start. s = 1-1/2 + 1/3-1/4 + 1/5-1/6 + 1/7... - 1/8 + 1/9-1/10 + 1/11-1/12; 1

In the examples shown in the book we have shown long lines wrapped but terminated by ; as shown below: s = 1-1/2 + 1/3-1/4 + 1/5-1/6 + 1/7-1/8 + 1/9-1/10 + 1/11-1/12; Predefined constants SCILAB s predefined constants are preceded by the % character as follows: Constant Meaning Value %pi π 3.14 %i j 1 %e e 2.713 %inf + Infinity %nan Not a number Operators SCILAB uses conventional mathematical operators: Operator Meaning + Addition - Subtraction / Division * Multiplication OR & AND = Equality ( ) Parenthesis ^ Power Conjugation Assigning values to variable Operators are used to assign values to variables and are follow conventional mathematical rules of precedence: r = 1/8; assigns the value 0.125 to the variable, r d = 2 * %pi assigns the value 6.28 to the variable, d m = (60 * n)/(2 * %pi) Parenthesis are evaluated before division xl = 2 * %pi * f * L; Inductive reactance 2

Exponent notation Exponent notation can be used to indicate very large or very small numbers, as follows: a = 1E-3; a = 1 10-3 or 0.001 c = 2 * %pi 2 diameter = 2.2 10 6 or 200000 Complex numbers Complex numbers are indicated by using %i to represent operator j as shown in the following example: Z1 = 1 - %i; 1 j Z2 = 3 + (%i*2); 3 +j2 Variables SCILAB variables can be defined using statements of the form: w = 2; natural frequency in rad/sec. df = 0.2; damping factor t = 0:1:10; range of values for t 0 to 10 in steps of 1 Variable names can be as long and descriptive as you need but should not exceed 24 characters in length. All ASCII letters upper and lower case letters (a to z and A to Z) and digits (0 to 9) are allowed, with the additional characters %, _, #,!, $,?. Note that the % characters is reserved for use with predefined constants as mentioned earlier. Note also that SCILAB is case sensitive, which means that upper and lower case letters are considered to be different. Common functions SCILAB provides the usual range of common functions including: sin, cos, tan, asin, acos, atan, abs, min, max, sqrt, and sum. i = sin(0.5); assigns the value 0.4794255 to a t = max(2, 3, abs(-5), sin(1)); assigns the value 5 to t String variables Strings variables are delimited by pairs of single or double quotes and may be concatenated (i.e. joined) using the + operator: filename = 'results.dat'; subject = "Diode " + "current"; 3

Message boxes Message boxes are a convenient way of displaying results, for example: messagebox("flux produced = " + string(phi) + " mwb"); Dialogue boxes SCILAB can display a variety of different types of dialogue box, for example: wait = buttondialog("please wait","ok"); answ = buttondialog("output the results?", "Yes No", "User prompt"); User input User input can be gained in a similar manner. The value(s) entered by a user will be returned in the evstr variable, as shown below: gain = evstr(x_dialog('required margin? ','0.235')); Note that a default value (0.235) will appear when the dialogue box is displayed. Multiple inputs are also possible (see Example 1 on page 6). Matrices SCILAB is a powerful tool for manipulating matrices. There are several ways of entering a matrix but the most simple is as follows: 1. Separate each element in a row by a blank space or a comma 2. Separate each row of elements with a semi-colon 3. Enclose the entire list of elements in a pair of square brackets. For example, to enter a 3 x 3 magic square and assign to the variable M: M = [8 1 6; 3 5 7; 4 9 2] The 3 x 3 matrix, M, is then: 8. 1. 6. 3. 5. 7. 4. 9. 2. 4

Flow control SCILAB supports conventional program flow control using if, elseif, and end statements. The following logical operators may be used: Operator Meaning == Equal ~= Not equal >= Greater than or equal <= Less than or equal > Greater than < Less than ~ Not If a logical expression is true, it returns a Boolean variable T (true), otherwise F (false). The if statement takes the form: if condition action end The body will be executed only when the condition statement is true. The if statement can provide further conditional actions, as shown below: if condition_1 action_1 elseif condition_2 action_2 elseif condition_3 action_3 elseif...... end The three liberally commented examples that follow are designed to provide you with a quick overview of the capabilities of SCILAB. They do, however, only barely scratch the surface SCILAB is an extremely powerful tool! Mike Tooley January 2012 5

Example 1 A simple interactive calculation The application generates a dialogue box (SCILAB calls this a multiple values request box ) where the user enters the five input variables. An initial default value is displayed for each variable. After the result is calculated its values is displayed in a message box. Flux produced by an inductor First get input data from a dialogue box txt = ['Mean length of the magnetic path (mm)';'cross-sectional area of the core (mm2)';'relative permeability of the core';'number of turns';'current (A)']; A single line of code has been wrapped here! var = x_mdialog('enter parameters for the inductor:',txt,['150';'100';'4800';'200';'0.5']); As above! l = 1E-3*evstr(var(1)); a = 1E-6*evstr(var(2)); p = evstr(var(3)); n = evstr(var(4)); i = evstr(var(5)); Calculate reluctance of the magnetic path s = l/(12.57*1e-7*p*a); Calculate flux produced phi =1E+3*(n * i)/s; Display result messagebox("flux produced = " + string(phi) + " mwb") Example 2 A simple calculation with graphical output The application uses the SCILAB plot2d function to output a graph. Note how the range of frequency values is specified in the sixth line of the script. Plot of current in a series resonant circuit This script plots a graph if i against f over the range 100 Hz to 1 khz. First set up the table of values for frequency f = [100:1:1000]'; Next enter values for each of the circuit variables 6

(note the use of exponent notation where appropriate) C = 1E-6; Capacitance = 1 uf L = 100E-3 Inductance = 1 mh r = 50; Resistance = 50 ohm v = 10; Supply = 10V Calculate the inductive reactance xl = 2 * %pi * f * L; Calculate the capacitive reactance xc = (2 * %pi * f * C)^-1; Calculate the impedance z = sqrt(r^2 + (xl - xc)^2); Calculate the current i = v * z^-1; Now plot the graph of current against frequency but first get rid of any earlier plot clf(); plot2d(f,i,style=[2]); plot line in blue xgrid(color('lightgrey')); place a grey grid on the plot xtitle(['series resonant circuit'],'frequency (Hz)','Current (A)'); plot(f,i); and finally plot the curve Experiment with the effect of different values for r on the shape (i.e. bandwidth) of the curve. Try values between 10 and 200 ohm 7

Example 3 Fast Fourier analysis This final application shows how the Fast Fourier transform, y = fft(s), can be used to display the frequency spectrum of a complex waveform: Fast Fourier analysis of a complex waveform Build a test signal sampled at 1000 Hz which contains to two pure sinusoidal components at 50 and 70 Hz sample_rate=1000; t = 0:1/sample_rate:0.6; N=size(t,'*'); number of samples s= 1*sin(2*%pi*50*t)+ 1/3 * sin(3*%pi*100*t+%pi/2); Obtain the Fourier transform y=fft(s); As the fft response is symmetric only retain the first N/2 points f=sample_rate*(0:(n/2))/n; associated frequency vector n=size(f,'*'); Clear the graphic display and then plot the frequency spectrum clf() plot2d(f,abs(y(1:n))); 8