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

2 LAboratory work # 2

TOPIC: Modeling of steady-state electric processes in MATLAB environment. Part 2.

PURPOSE OF THE WORK: Studying main principles of debugging of MATLAB-programs; verification of calculation results.

Debugging and verification of programs

As a rule, all programs contain mistakes and demand debugging, and practice shows, that debugging and verification of results occupy near 80 % of a program creation time. Mistakes have been two types: syntactic and logic.

As MATLAB language supposes a clear formalization of a spelling of operators and other components of the program and precise performance of mathematical operations, so infringement of these rules in many cases is supervised by MATLAB system. In consequence, the system gives out the diagnostic message about an assumed mistake.

Logic mistakes are connected to the following factors:

 Discrepancy of the verbal formulation of a problem to real object;

 Discrepancy of mathematical to verbal formulations;

 Mistakes in algorithm of mathematical statement of the problem;

 Mistakes in the program realizing the given algorithm,

 Mistakes of entering of the text of the program,

 Mistakes of entering of the input data.

In some cases logic mistakes result in infringement of the program syntax and become known at a translation stage. However, in most cases, the system itself may not define logic mistakes. Revealing of logic mistakes is a duty of the programmer. Therefore, the programmer should imagine well all logic of a solved task as a whole and criteria of check of adequacy of calculation of intermediate data and output results. In general, search of logic mistakes has no uniform rules and represents a creative problem.

However there are general recommendations for debugging. For example, at a stage of programming or algorithmization, it is necessary to think over methods of the future debugging. Some of the operators necessary only at a stage of debugging are entered in the program, for example, a printing of some especially important values; check of some correlations, which should be always carried out or for a concrete mode. The programmer should aspire to localization of area of mistakes. For this purpose the special methods of modular and structural programming are developed. About ones, it is possible to read in the special literature [ ].

Search of syntactic mistakes

To familiarize with a technique of search of syntactic mistakes, we shall artificially contain them into the text of the program and observe reaction of the MATLAB compiler to their occurrence.

Click the text of program Circuit 1. Save it in the same directory

as Circuit 1_debug. Now contain a syntactic mistake to the text of the

program to a line number 15: for operator of calculation E1 let’s cut the last

bracket:

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

Save the received file. Start the program.

Message in Command Window will be as:

> > Circuit 1_debug

??? Error: File: E:\ ..\Circuit 1_debug.m Line: 15 Column: 31

")" expected "; " found.

The given message precisely locates an entered mistake. Let’s correct it. Now we shall enter the other mistake: let’s change dimension of matrix F in a line 21 as follows:

F = [0; E1; -E2; 0];

Save the received file and start the program.

Message in Command Window will be the following:

> > Circuit 1 _debug

??? Error using == > \

Matrix dimensions must agree.

Error in == > E:\ ..\Circuit 1_debug.m

On line 22 == > I=M\F;

The given message warns of discrepancy of the matrix dimensions at performance of operator I=M\F in a line 22. Now let’s correct this mistake and save a file.

Further, enter mistakes to a spelling of standard functions (for example, sqrt). Save the program, start it and look the message about the mistake. You may try to enter other mistakes. Do not forget to save the program before start and then to correct entered mistakes.