1. Introduction to MATLAB
In this chapter, we will talk about the basics of MATLAB and how to get started with it. We expect the reader to have basic programming skills, and therefore we will not cover any MATLAB programming concepts in detail. When discussing any topic, we will try not to go into such specifics that cause us to deviate from our main goal. Instead, we will provide proper references which can be consulted for more information.
Introduction
MATLAB is a programming language created with the basic goal of providing a simple intuitive platform for engineering design applications. However, as well as being a programming language, it can also be classified as software for computation and visualization. On one hand, you can call sophisticated programming routines in MATLAB, while on the other hand you can just open a simple graphical user interface to fill in the specifications, click OK, and MATLAB will perform computations and visualize it for you.
As we will see in later chapters, most engineering tasks require processing of matrices, for example image processing or data regression. MATLAB provides an excellent platform and routines for matrix operations. In fact, MATLAB is short for MATrix LABoratory because its main feature is to provide direct operations on matrices and to avoid complicated loops.
Current engineering applications use numerical simulations extensively, which require specific functions along with a programming language. For example, a signal processing application will need filter, fft and similar basic functions. In typical programming environments such as C/C++, you need to write these basic functions yourself. MATLAB provides all such basic and advanced functions in built-in packages known as toolboxes. MATLAB currently has a wide range of toolboxes for different engineering fields, e.g. it has toolboxes devoted to signal processing, image processing, communication, control systems and finance. In addition, some engineers are more inclined towards graphical models and want to avoid writing programming routines. MATLAB also features Simulink, which provides a platform for building and simulating graphical models without knowledge of programming.
MATLAB provides an interactive environment to perform engineering tasks for most current fields. You can also use your own toolboxes or use extra toolboxes built by others, which are freely available at the MATLAB File exchange at the following address:
http://http://www.mathworks.com/matlabcentral/fileexchange .
With its intuitive language, minimalistic programming commands, capability of one shot matrix operations, a wide range of toolboxes for most engineering fields, interactive platform and excellent visualization capabilities, MATLAB is regarded as an excellent and preferred tool for academic research and industrial applications.
Interface
After you have successfully installed MATLAB, you can open it by double clicking the icon or typing matlab in the terminal/run window. Depending on your machine, you will see something like Figure . The whole interface is known as the MATLAB Desktop. It consists of many components and windows which can be reorganized and enabled/disabled by mouse actions or via Desktop Layout menu options. Here are some of the important components one can find in a standard MATLAB Desktop:
Figure 1-1.
MATLAB Desktop
Command Window
The command window can be considered as a terminal. It is shown at the bottom of the middle column in Figure . Here, you can type your commands and run MATLAB programs (known as scripts). MATLAB displays >> as the command prompt.
Current Directory
The Current Directory in MATLAB represents a directory in which MATLAB looks for the functions and program files. In order to run a program, it must be in the current directory. The Current Directory window shows all the files and folders in the current directory. You can change the current directory or even add some folders to the MATLAB search path.
Workspace
The MATLAB workspace contains all the variables present and is shown at the top right of Figure . MATLAB programming differs from conventional programming languages in the sense that any variables created during a program execution remain, even after the program has executed, until you explicitly clear them or close the MATLAB session.
Figures
MATLAB uses figure windows to display any plot or visualization. This is shown in the leftmost column of Figure . We can open multiple figure windows using the figure command.
Command History
The command history window stores and displays all previous MATLAB commands issued in the command window. You can rerun any command by double clicking on the command window. We can also access the command via the keyboard, using the up and down arrow keys.
Editor
MATLAB provides an integrated editor to write commands and programs and execute them. The editor also can be used to debug programs. It is shown at the top middle of Figure .
Help Browser
MATLAB provides excellent documentation for all its functions with sufficient examples. To open MATLAB help, you can type doc or go to Help Product Help. To directly open help about a function (for example sin), we would type
doc sin;
Getting Started
In our first example, we will create a simple program to multiply two numbers. MATLAB programs are called scripts or also M-files because their extension is .m. To create a script named myfirstprogram.m , let us go to the command window and type
edit myfirstprogram
MATLAB will open an editor window with a blank file name. Type the following code
a=4;
b=3;
c=a*b;
disp(c);
Save this file and run the code by entering the following in the command window
myfirstprogram
When you press enter, you get the following output
c=
Let us spend some time in understanding this simple program. We dont need any sophisticated program definitions or imports to run a simple program such as multiplication in MATLAB. In this program, we have defined two variables a and b. To define/assign a variable, we simply need to type
variablename= variablevalue;
We then multiplied them to store the answer in c and displayed it using the disp command.
The semicolon used to terminate each line is not necessary. If the semicolon is omitted, MATLAB will also display the value computed in the right-hand side of the statement. For example, typing
c=a*b;
will result in the computation of c without displaying the result, while typing
c=a*b
will result in the computation of c and also result in the display of the output in the command window. We can change the value of any variable by reassigning its value. For example
c=7;
will change the value of c to 7.
The first question which comes to our mind concerns the type of a. Is it stored as a double or an integer? First of all, all the variables stored in MATLAB are matrices. Secondly, all the numerical values are treated as double. So 4 is a 11 double numerical matrix. So if we compute the division of a by b using