CHAPTER1
Introduction
to MATLAB
MATLAB(short for MATrix LABoratory)is a special-purpose computer program
optimized to perform engineering and scientific calculations.It started life as a program
designed to perform matrix mathematics,but over the years it has grown into a flexible
computing system capable of solving essentially any technical problem.
The MATLAB program implements the MATLAB programming language and
provides a very extensive library of predefined functions to make technical programming
tasks easier and more efficient.This book introduces the MATLAB language as it is
implemented in MATLAB Version 7.4(Release 2007a)and shows how to use it to solve
typical technical problems.
MATLAB is a huge program with an incredibly rich variety of functions.Even the
basic version of MATLAB without any toolkits is much richer than other technical
programming languages.There are more than 1000 functions in the basic MATLAB
product alone,and the toolkits extend this capability with many more functions in various
specialties.This book makes no attempt to introduce the user to all of MATLAB’s
functions.Instead,it teaches a user the basics of how to write,debug,and optimize good
MATLAB programs and presents a subset of the most important functions.Just as
importantly,it teaches the programmer how to use MATLAB’s own tools to locate the
right function for a specific purpose from the enormous number of choices available.
1.1 The Advantages of MATLAB
MATLAB has many advantages compared with conventional computer languages for
technical problem solving.These include
1.Ease of Use
MATLAB is an interpreted language,like many versions of Basic.Like Basic,it is
very easy to use.The program can be used as a scratch pad to evaluate expressions
typed at the command line,or it can be used to execute large prewritten programs.
Programs may be easily written and modified with the built-in integrated
development environment,and debugged with the MATLAB debugger.Because
the language is so easy to use,it is ideal for the rapid prototyping of new programs.
Many program development tools are provided to make the program easy to
use.They include an integrated editor/debugger,on-line documentation and
manuals,a workspace browser,and extensive demos.
2.Platform Independence
MATLAB is supported on many different computer systems,providing a large
measure of platform independence.At the time of this writing,the language is
supported on Windows 2000/XP/Vista,Linux,several versions of Unix,and the
Macintosh.Programs written on any platform will run on all of the other
platforms,and data files written on any platform may be read transparently on
any other platform.As a result,programs written in MATLAB can migrate to
new platforms when the needs of the user change.
3.Predefined Functions
MATLAB comes complete with an extensive library of predefined functions that
provide tested and prepackaged solutions to many basic technical tasks.For
example,suppose that you are writing a program that must calculate the statistics
associated with an input data set.In most languages,you would need to write
your own subroutines or functions to implement calculations such as the arithmetic
mean,standard deviation,median,and so on.These and hundreds of other
functions are built right into the MATLAB language,making your job much easier.
In addition to the large library of functions built into the basic MATLAB
language,there are many special-purpose toolboxes available to help solve
complex problems in specific areas.For example,a user can buy standard
toolboxes to solve problems in signal processing,control systems,
communications,image processing,and neural networks,among many others.
There is also an extensive collection of free user-contributed MATLAB programs
that are shared through the MATLAB Web site.
4.Device-Independent Plotting
Unlike most other computer languages,MATLAB has many integral plotting
and imaging commands.The plots and images can be displayed on any graphical
output device supported by the computer on which MATLAB is running.This
capability makes MATLAB an outstanding tool for visualizing technical data.
5.Graphical User Interface
MATLAB includes tools that allow a programmer to interactively construct a
Graphical User Interface(GUI)for his or her program.With this capability,the
programmer can design sophisticated data-analysis programs that can be operated
by relatively inexperienced users.
6.MATLAB Compiler
MATLAB’s flexibility and platform independence is achieved by compiling
MATLAB programs into a device-independent p-code and then interpreting the
pcode instructions at runtime.This approach is similar to that used by Microsoft’s
Visual Basic language.Unfortunately,the resulting programs can sometimes execute
slowly because the MATLAB code is interpreted rather than compiled.We will point
out features that tend to slow program execution when we encounter them.
A separate MATLAB compiler is available.This compiler can compile a
MATLAB program into a true executable that runs faster than the interpreted
code.It is a great way to convert a prototype MATLAB program into an
executable suitable for sale and distribution to users.
1.2 Disadvantages of MATLAB
MATLAB has two principal disadvantages.The first is that it is an interpreted language
and therefore may execute more slowly than compiled languages.This problem can be
mitigated by properly structuring the MATLAB program.
The second disadvantage is cost: a full copy of MATLAB is five to ten times more
expensive than a conventional C or Fortran compiler.This relatively high cost is more
than offset by the reduced time required for an engineer or scientist to create a working
program,so MATLAB is cost-effective for businesses.However,it is too expensive for
most individuals to consider purchasing.Fortunately,there is also an inexpensive Student
Edition of MATLAB,which is a great tool for students wishing to learn the language.The
Student Edition of MATLAB is essentially identical to the full edition.
1.3 The MATLAB Environment
The fundamental unit of data in any MATLAB program is the array.An array is a collection
of data values organized into rows and columns and known by a single name.Individual data
values within an array may be accessed by including the name of the array,followed by
subscripts in parentheses that identify the row and column of the particular value.Even
scalars are treated as arrays by MATLAB—they are simply arrays with only one row and one
column.We will learn how to create and manipulate MATLAB arrays in Section 1.4.
When MATLAB executes,it can display several types of windows that accept
commands or display information.The three most important types of windows are
Command Windows,where commands may be entered; Figure Windows,which display
plots and graphs; and Edit Windows,which permit a user to create and modify MATLAB
programs.We will see examples of all three types of windows in this section.
In addition,MATLAB can display other windows that provide help and that allow
the user to examine the values of variables defined in memory.We will examine some of
these additional windows here,examine the others when we discuss how to debug
MATLAB programs.
1.3.1 The MATLAB Desktop
When you start MATLAB Version 7.4,a special window called the MATLAB desktop
appears.The desktop is a window that contains other windows showing MATLAB data,
plus toolbars and a “Start” button similar to that used by Windows XP.By default,most
MATLAB tools are “docked” to the desktop so that they appear inside the desktop
window.However,the user can choose to “undock” any or all tools,making them appear
in windows separate from the desktop.
The default configuration of the MATLAB desktop is shown in Figure 1.1.It
integrates many tools for managing files,variables,and applications within the MATLAB
environment.
The Command Window
The Command History Window
The Start Button
The Documents Window,including the Editor/Debugger and the Array Editor
Figure Windows
Workspace Browser
Help Browser
Path Browser
We will discuss the functions of these tools in later sections of this chapter.
1.3.2 The Command Window
The right-hand side of the default MATLAB desktop contains the Command Window.A
user can enter interactive commands at the command prompt(.)in the Command
Window,and they will be executed on the spot.
As an example of a simple interactive calculation,suppose that you want to calculate
the area of a circle with a radius of 2.5 m.This can be done in the MATLAB Command
Window by typing
MATLAB calculates the answer as soon as the Enter key is pressed and stores the answer
in a variable(really a 1×1 array)called area.The contents of the variable are displayed
in the Command Window as shown in Figure 1.2,and the variable can be used in further
calculations.(Note that π is predefined in MATLAB,so we can just use pi without first
declaring it to be 3.141592 ...).
Instead of typing commands directly in the Command Window,a user can place a
series of commands into a file,and the entire file can be executed by typing its name in
the Command Window.Such files are called script files.Script files(and functions,
which we will see later)are also known as M-files,because they have a file extension
of “.m”.
1.3.3 The Command History Window
The Command History Window displays a list of the commands that a user has entered in
the Command Window.The list of previous commands can extend back to previous
executions of the program.Commands remain in the list until they are deleted.To
reexecute any command,simply double-click it with the left mouse button.To delete one
or more commands from the Command History window,select the commands and
right-click them with the mouse.A popup menu will be displayed that allows the user to
delete the items(see Figure 1.3).
1.3.4 The Start Button
The Start Button(see Figure 1.4)allows a user to access MATLAB tools,desktop
tools,help files,and so on.It works just like the Start button on a Windows desktop.To