Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
labScilab_English_Part1.doc
Скачиваний:
26
Добавлен:
07.02.2016
Размер:
2.38 Mб
Скачать

4.2 Main Rules of Works in Scilab System

Using the prompt line, it is possible to assign values to variables and to perform usual operations:

--> x = 4

x =4

--> y = 2

y =2

--> x + y

ans =6

--> x * y

ans =8

Here, we note that SCILAB assigns the result of the operations to a variable named ans.

We can find out the value of a given variable by typing the name on the prompt line:

--> ans

ans =8

The solution to the above addition has been lost. A good way to prevent that is to assign operation results to a given variable:

--> x = 4; // semi-colons prevent the output on the screen

--> y = 2; // two slash enables comments

--> a = x + y

a =6

--> b = x * y

b =8

Scilab stores numbers (and characters) in matrices. A matrix can be seen as a table, consisting of m rows and n columns; (m*n matrices, also denoted i*j matrices). Scalar variables do not exist per se, they are treated as 1x1 matrices. The general form of a Scilab matrix (here 3x3 matrix) is A = [11 12 13; 21 22 23; 31 32 33] Row elements can also be separated by commas: A = [11, 12, 13; 21, 22, 23; 31, 32, 33]

In both cases semicolons separate rows.

Variables

Like most other programming languages, the SCILAB language provides mathematical expressions, but unlike most programming languages, these expressions are the matrices.

SCILAB does not require any type declarations or dimension statements. When SCILAB encounters a new variable name, it automatically creates the variable and allocates the appropriate amount of storage. If the variable already exists, SCILAB changes its contents and, if necessary, allocates new storage. For example,

-->num_students = 25

creates a 1-by-1 matrix named num_students and stores the value 25 in its single element. To view the matrix assigned to any variable, simply enter the variable name.

Variable names consist of a letter, followed by any number of letters, digits, or underscores. SCILAB is case sensitive; it distinguishes between uppercase and lowercase letters. A and a are not the same variable.

Variables can be numeral, text and other types. We shall consider numeral and text variables. It is impossible to admit the repeat of a name of variable with a name of any command.

All numbers in Scilab have 15 digits of accuracy.

Numerical variables are numbers, vectors, matrixes and multivariate files. In a computer all numbers are submitted approximately with 16 decimal signs, under each real number is allocated 8 bites, under complex - 16.

Integers. In system they are not allocated obviously. We shall type and execute separately each command:

a=2 a=2.0 a=2; a=1:6 b=1:20 c=10:-2:5

Real numbers. SCILAB uses conventional decimal notation, with an optional decimal point and leading plus or minus sign, for numbers. Scientific notation uses the letter e to specify a power-of-ten scale factor. Some examples of legal numbers are

3 -99 0.0001

9.6397238 1.60210e-20 6.02252e23

The range of real numbers is kept in constants: realmax realmin

Some other constants of system Scilab: pi i j eps.

Perform commands

--> rad=2; ht=3;

--> vol=%pi*ht*rad~2

vol =37.6991

Text variables - Text variables enter in inverted commas:

t='Moscow'

Formats to a conclusion of numbers in package Scilab

Function format([type],[long]) set the format of variable to output on screen. Type can be equal ‘v’ for format with decimal point or ‘e’ for science format.

Examples:

x=rand(1,4);

-->format('v',10);x

x = 0.2113249 0.7560439 0.0002211 0.3303271

-->format(20);x

x = 0.21132486546412110 0.75604385416954756 0.00022113462910056 0.33032709173858166

-->format('e',10);x

x = 2.113D-01 7.560D-01 2.211D-04 3.303D-01

-->format(20);x

x = 2.1132486546412D-01 7.5604385416955D-01 2.2113462910056D-04 3.3032709173858D-01

-->x=[100 %eps];

-->format('e',10);x

x = 1.000D+02 2.220D-16

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]