Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
МІНІСТЕРСТВО ОСВІТИ І НАУКИ УКРАЇНИ.doc
Скачиваний:
2
Добавлен:
04.11.2018
Размер:
2.01 Mб
Скачать

Script-files and Function-files

The files containing MATLAB commands, statements and operators, refer to as M-files.

There are two types of M-files:

Script-files not having entrance parameters;

Function-files having entrance parameters.

Enter of input data by awarding method. Comments

At the first stage of acquaintance to MATLAB we should execute enter of the input data, using the awarding operator directly in the text of the program.

Let's start the program with enter of comments.

Each line of comments begins with a symbol of %. It is necessary to remember, that comments in Russian can be entered only by capital letters.

It is possible to write down each MATLAB operator as in a separate line, and some operators in one line are consecutive. If operators are written down in a column, i.e. on one in a line, after each of them it is possible to put a semicolon ;. It means that the result of calculations will not be displayed automatically at program execution. If you don’t end the operator with a semicolon ; MATLAB performs the computation but does not display any output. The operators have been written down in one line, are obliged to be separated one from other by semicolons ;. Operators of input data with the appropriate comments are given in the text of program Circuit 1 (see item1.10).

Organization of calculations. Verification of results

In the text of program Circuit 1 (see the item1.1) is reflected a stage of calculations E1, E2, XL, XC, Z1, Z2, Z3 and formation of M and F matrixes.

The note: Number is a constant of MATLAB and it is marked in pi.

For check of correctness of the organization of calculations we shall calculate - an error of calculations on an external contour of the circuit Fig.1.1.

Output of results of calculations

For the output of results, we shall take advantage of the operator disp, which allows printing of symbolical results. As MATLAB does not allow printing by one operator both numbers, and symbols, we apply standard function num2str (x), transforming a numerical variable X into a variable of string-type (see the text of program Circuit 1 in item 1.10).

Saving program and Search path creation

For saving program click Save as option from File menu and contain the created M - file named Circuit 1 into your personal directory.

To have possibility calling from MATLAB the file created by yourself, you must specify search path. For this purpose, we shall choose Set Path from File menu on MATLAB desktop and appeared dialogue window is used.

Running the program

To execute the program, there is enough to enter a name of the M - file (in our case it is Circuit 1) in a command window after the invitation symbol and to press ENTER (or click Run icon on the panel of tools). Results of calculations will be displayed in Command window.

Text of the program and listing of performance

% Example of calculation of sine wave current circuit

% PROGRAM Circuit 1

f=50;

R1=1;

R2=2;

R3=3;

L2=1e-3;

C3=1e-4;

ph1=30;

ph2=60;

XL=2*pi*f*L2;

XC=1 / (2*pi*f*C3);

E1=100/sqrt (2) *exp (ph1*pi/180*i);

E2=200/sqrt (2) *exp (ph2*pi/180*i);

Z1=R1; Z2=R2+i*XL; Z3=R3-i*XC;

M = [1 -1 -1;

Z1 0 Z3;

0 Z2 -Z3];

F = [0; E1; -E2];

I=M\F;

disp ([' I1 = ', num2str (I (1,1))]);

disp ([' I2 = ', num2str (I (2,1))]);

disp ([' I3 = ', num2str (I (3,1))]);

% Check up according to 2-nd Kirchhoff’s rule

eps=E1-E2-Z1*I (1,1)-Z2*I (2,1);

disp ([' error calculation eps = ', num2str (eps)]);

% Calculation modules and arguments (radians) of the calculated currents

I1M=abs (I (1,1));

I2M=abs (I (2,1));

I3M=abs (I (3,1));

phI1=angle (I (1,1));

phI2=angle (I (2,1));

phI3=angle (I (3,1));

disp ([' I1M = ', num2str (I1M)]); disp ([' phI1 = ', num2str (phI1)]);

disp ([' I2M = ', num2str (I2M)]); disp ([' phI2 = ', num2str (phI2)]);

disp ([' I3M = ', num2str (I3M)]); disp ([' phI3 = ', num2str (phI3)]);

Results of calculations:

>> I1=-7.37618-26.903i

I2=-5.63886-29.2223i

I3=-1.7373+2.3193i

error calculation eps=2.6645e-015-1.4211e-014i

I1M=27.8959

phI1=-1.8384

I2M=29.7614

phI2=-1.7614

I3M=2.8978

phI3=2.2137

>>

Conclusions: The error of calculation is about 10-15, hence, account is executed truly.