
- •Міністерство освіти і науки, молоді та спорту україни
- •Laboratory Works of Computing and Programming
- •Contents
- •1.2 Base Concepts of Operating Systems
- •1.3 Linux (lubuntu) Operating System
- •1.4 Tasks for Independent Work
- •1.5 Test Questions
- •2 Laboratory work № 2 Word Processor libreoffice.Writer
- •2.1 What Word Processors Can Do
- •2.2 Tasks for Independent Work
- •2.3 Test Questions
- •3 Laboratory work № 3
- •Introduction to the calc spreadsheet
- •3.1 The Basic Opportunities of Spreadsheets
- •3.1.1 Calc’s Environment
- •3.1.2 Calculations in Calc. Creating and Coping Formulas
- •3.2 The Calc charting capability
- •3.3 Tasks for Independent Work
- •4.2 Main Rules of Works in Scilab System
- •Variables
- •Input of vectors (arrays)
- •Input of matrixes
- •Some operations with matrixes with use of the operator ":"
- •Input from keyboard
- •Operators. Expressions use familiar arithmetic operators and precedence rules.
- •Intrinsic scilab Functions
- •Examples of Expressions
- •4.3 Individual Tasks for Laboratory Work
- •4.4 The Tasks for Self-Examination
- •4.5 Test Questions
- •5 Laboratory work № 5
- •5.3.1The plot function
- •5.3.3 Preparing Graphs for Presentation
- •Interactive Plot Editing
- •5.3.4 3D Plotting
- •5.4 Individual Tasks
- •5.6 Test Questions
- •6 Laboratory work № 6 programming in Scilab
- •6.1 Programming in scilab
- •What Happens When You Call a Function
- •Clearing Functions from Memory
- •6.2 Tasks for laboratory work
- •6.3 Example of performance of the laboratory work
- •6.4 Test Questions
- •7 ReferencEs
5.6 Test Questions
Describe basic plotting commands.
How to set the line styles, colors, and markers of the graph?
How to create line plots that have left and right y-axes?
Adding annotations, axis labels, titles, and legends to graphs.
How to edit plot in Scilab?
What functions do you use for the visualization of 3D data?
6 Laboratory work № 6 programming in Scilab
6.1 Programming in scilab
The SCILAB product provides a powerful programming language, as well as an interactive computational environment. You can enter commands from the language one at a time at the SCILAB command line, or you can write a series of commands to a file that you then execute as you would any SCILAB function. Use the SCILAB Editor or any other text editor to create your own function files. Call these functions as you would any other SCILAB function or command.
There are two kinds of program files:
Scripts, which do not accept input arguments or return output arguments. They operate on data in the workspace.
Functions, which can accept input arguments and return output arguments. Internal variables are local to the function.
So far, we have used SCILAB like a calculator. For repetitive tasks (and especially to plot graphical outputs), it is far more practical to write some short programs. We usually use SCRIPT files that we can launch from the SCILAB prompt line to call and save outputs from the most repetitive operations, usually written in FUNCTION files. Both type share the file extension *.m. We usually write those using the M-file editor (clicking the icon or by typing edit from the prompt line) but any ASCII editor (such as Windows Notepad) can also be used.
SCRIPT Files
These are scripts which contain a sequence of Scilab commands. They operate exactly the same way as typing the commands one after the other at the Scilab prompt. They are created by going File|New|M-file, which will bring up the Scilab editor. Save the file wherever you like. It is simply a text file with a .m extension. They can be edited by any program which reads text files, although the Scilab editor is probably best since it highlights/colorizes the text appropriately. You can run a m-file from the Scilab command prompt by typing the name of the file (without the .m extension). It will operate just like any other Scilab command. Make sure your working directory is set to the directory that the m-file is in, or Scilab won’t find it!!
We use SCRIPT files to assign values to variables, to call FUNCTION –usually SCILAB built-in functions, perform algebraic operations or operations on symbolic variables and plot figures. Basically, a SCRIPT file just performs the same operations we would have performed directly in the Workspace. Note that variables used in a SCRIPT are available in the Workspace. Scripts can operate on existing data in the workspace, or they can create new data on which to operate. Although scripts do not return output arguments, any variables that they create remain in the workspace, to be used in subsequent computations. For example, we can write the following m-file:
% test1.m
clear all % to clear variables in the Workspace
close all % to close all plots windows opened by SCILAB
x = 4; y = 2;
a = x + y
b = x * y
whos
We call this file (with corresponding output) from the Command Window:
>> test1
a =6
b =8
Name Size Bytes Class
a 1x1 8 double array
b 1x1 8 double array
x 1x1 8 double array
y 1x1 8 double array
Grand total is 4 elements using 32 bytes
>>
Of course, it is important to check that the file we are calling is located in the Current Directory.
One important feature of SCILAB is that it is not a compiled programming language like C/C++. Each time you call SCILAB, the program reads and performs the program file line by line. If SCILAB detects an error, it will output an error message (with the line where the error might be located).
FUNCTION Files
Functions are similar to m-files, but there is a well-defined input and output interface. A function takes arguments and returns something. It can take multiple scalar, vector, or matrix arguments, and return any number of things as well.
Some important things to note about functions:
The filename for the function must be the same as the function name (here, both are quadratic)
The function has a “local scope”… it does NOT have access to variables in the workspace. If you want it to have access to workspace variables, you must define the variables as global both inside the function and in the workspace (>> help global for more information).
FUNCTION files are usually used to perform repetitive simple mathematical operations. It is also useful if we don’t need intermediate values (since values internal to a FUNCTION file are not available from the workspace). Also, it is a nice way to divide a larger problem in simpler sub-operations. A function is defined by its inputs and outputs. The header of M-function looks like this
function [<list of output parameters>]=
<function name> (<list of input parameters >)
The first line of a function M-file starts with the keyword function. It gives the function name and order of input and output arguments or parameters.
All parameters or variables in function declaration are dummies. They are placeholders for actual parameters in function call. These parameters are only places for values of actual parameters. They will be replaced by the actual variables or values in the function call.
The next several lines in function declaration, up to the first blank or executable line, are comment lines that provide the help text. Comment lines starts with a percent sign “%”.
The rest of the file is the executable SCILAB code defining the calculation of the output parameters.
To start function calculations you need to invoke (or to call) it. To call function write operator
[<list of output parameters >]=
<function name> (<list of input parameters >)
All parameters in function call are actual. The amount and types of actual parameters must correspond to dummy parameters.
The list of output parameters is variable names separated by commas. You can use the variable names, constants, or expressions as input actual parameters. The correspondence between dummy and actual parameters set in place in the list. Types of input variables must match the types of the input parameters in the function declaration.
For example, the factorial of a positive number and its reciprocal can be described in a file fact.m:
function [f,g]=fact(n)
% factorial of a positive number n and its reciprocal
% the factorial is the product of integer numbers
% from 1 up to n
% function prod calculates the product of vector elements
f=prod (1:n);
g=1/f;
This function has one input agrument - n, name – fact and two output arguments f and g.
To invoke (or call) this function, write in command line
>>[a,b]=fact(4)
and will see the result of calculations:
a=24
b=0.0417
Let disscus the next example of function:
function a = test2(x,y)
a = x + y; b = x * y;
and the following call:
>> value = test2(4,2)
value =6
>> whos
Name Size Bytes Class
value 1x1 8 double array
Grand total is 1 element using 8 bytes
The multiplication result is lost (even if we have performed it in the FUNCTION file). Since, we can define multiple outputs from a FUNCTION file, this can be fixed using the following:
function [a,b] = test3(x,y)
% Calculation of sum and production of two numbers
a = x + y;
b = x * y;
>> [ans1,ans2] = test3(4,2)
ans1 =6
ans2 =8
>> whos
Name Size Bytes Class
ans1 1x1 8 double array
ans2 1x1 8 double array
Grand total is 3 elements using 24 bytes