Programming

This chapter introduces programming in MATLAB. The programming language (i.e., the structure of the commands and the set of commands) in MATLAB is ideally suited to learn the basic principles of programming, and after that to solve all kinds of problems. In this course, the emphasis is on learning to read existing programs, and learning to write or change programs. For this, it is important that you understand the basic principles of programming and that you are able to apply these principles yourself.

A computer program is nothing else but a series of consecutive commands that are executed by the computer. The `order' and the number of commands does not need to be decided beforehand, but can depend upon initial values or the value of intermediate results. This is achieved by imposing a route (flow) through the commands. This makes a program as flexible as possible.

The basic components of programming are:

These basic components will be treated extensively in this chapter.

MATLAB is a high level programming language
This means that the commands you program are very far away from the series of commands the microprocessor executes (these commands are expressed in assembly language). This high level considerably facilitates programming, but also makes certain things impossible. A big restriction is that MATLAB programs only work within MATLAB itself. Essentially, a MATLAB program is nothing else but a series of MATLAB commands that are automatically executed consecutively. For many applications this offers a lot of advantages, like the facts that you can calculate with matrices in MATLAB and that you can use all built-in MATLAB functions. Moreover, all kinds of input and output facilities (for example the presentation of results in files or graphs) are available.

MATLAB versus other programming languages
Also in other respects, MATLAB is different from standard programming languages like C, C++ and Java. In the first place, MATLAB is a so called interpreter language, which means that MATLAB programs are read, interpreted and executed line by line. This takes a relatively long computation time, but it is very convenient for finding errors and making prototypes or test programs. Other programming languages (e.g. C, C++, Fortran and Java) are compiler languages. The program is translated into commands for the processor in a so called executable file (with extension .exe or .dll) once. After this, these files can be executed very quickly as many times as you want. However, it is more difficult to find errors in your program during the translation or execution.

Secondly, variables used in a MATLAB program do not need to be declared beforehand, and you do not need to state beforehand to which type or class a variable belongs. In MATLAB every variable is either of the type matrix (numerical) or of the type string (text), while in standard programming languages you need to declare beforehand what the type and size of every variable is. Standard types are real (numerical), integer, boolean (true or false), char (letter) etc. This makes matrix calculations in C cumbersome, while in MATLAB it goes automatically.

Another distinction is that MATLAB does not know pointers, while in most other programming languages a distinction is made between the value of a variable and its memory address. Also, MATLAB is not object oriented.

MATLAB is the ideal environment for learning to program, solve computationally simple problems and write prototype programs. If speed is really important, you can consider using another language. It is often possible as well to call pieces of compiled C code within MATLAB to improve computation speed. However, this will not be considered in this introductory course. Finally, MATLAB is not the appropriate tool for some applications, in particular large scale programs, Windows programming or Web-based programming. However, if you know the basic principles of programming, it is relatively easy to learn a new programming language.

Approach to programming in MATLAB
The reason that we want to write a computer program is that we want to solve a problem. Programming by itself does not solve the problem! We can only use a computer to (automatically) execute a certain solution strategy, saving a lot of work and time. However, we have to come up with the solution strategy ourselves and make the strategy clear to the computer! Therefore, programming is `the translation of a technical problem into computer instructions'. A standard procedure for solving a technical problem using programming in MATLAB is the following:

1.
analyse the problem and determine a solution strategy (on paper)
2.
work out a formulation (on paper)
3.
write an M-file in the MATLAB Editor/Debugger
4.
testing and debugging
5.
solve the problem
In this course, we will mainly consider points 3 and 4, and you will learn important elements and constructs from programming languages that you need to write a good program.



Previous      Next      Up      Contents


Esteur 2010-03-22