Вы находитесь на странице: 1из 4

MATLAB 1 CHAPTER 1

CHAPTER 1
INTRODUCTION
1.1 INTRODUCTION








That statement encapsulates the view of The MathWorks, Inc., the developer of MATLAB .
MATLAB 7 is an ambitious program. It contains hundreds of commands to do mathematics.
You can use it to graph functions, solve equations, perform statistical tests, and much more. It
is a high-level programming language that can communicate with its cousins, e.g., Fortran
and C. You can produce sound and animate graphics. You can do simulations and modeling
(especially if you have access not just to basic MATLAB but also to its accessory
Simulink). You can prepare materials for export to the World Wide Web. In addition, you
can use MATLAB to combine mathematical computations with text and graphics in order to
produce a polished, integrated, interactive document.

A program this sophisticated contains many features and options. There are literally hundreds
of useful commands at your disposal. The MATLAB help documentation contains thousands
of entries. The standard references, whether the MathWorks Users Guide for the product, or
any of our competitors, contain a myriad of tables describing an endless stream of commands,
options, and features that the user might be expected to learn or access.

MATLAB is more than a fancy calculator; it is an extremely useful and versatile tool. Even if
you know only a little about MATLAB, you can use it to accomplish wonderful things. The
hard part, however, is figuring out which of the hundreds of commands, scores of help pages,
and thousands of items of documentation you need to look at to start using it quickly and
effectively.

1.2 STARTING MATLAB
You start MATLAB as you would any other software application. On a PC you access it via
the Start menu, in Programs under a folder Matlab. Alternatively, you may have an icon
set up that enables you to start MATLAB witha simple double-click. On a UNIX machine,
generally you need only type matlab in a terminal window, though you may first have to find
the matlab/bin directory and add it to your path. Or you may have an icon or a special button
on your desktop that achieves the task.

However you start MATLAB, you will briefly see a window that displays the MATLAB logo
as well as some MATLAB product information, and then a MATLAB Desktop window will
launch. That window will contain a title bar, a menu bar, a tool bar, and other embedded
MATLAB is a high-level technical computing language and interactive
environment for algorithm development, data visualization, data analysis,
and numerical computation. Using MATLAB, you can solve technical
computing problems faster than with traditional programming languages,
such as C, C++, and Fortran. The MathWorks, Inc.
MATLAB 2 CHAPTER 1

windows. The largest and most important window is the Command Window on the right. The
other four windows are the Current Directory browser, the Command History window, the
Workspace browser. For now we concentrate on the Command Window to get you started
issuing MATLAB commands as quickly as possible. At the top of the Command Window,
you may see some general information about MATLAB, perhaps some special instructions
for getting started or accessing help, but most important of all, a line that contains a prompt.
The prompt will likely be a double caret (>>). If the Command Window is active, its title
bar will be dark, and the prompt will be followed by a cursor (a vertical line or box, usually
blinking). That is the place where you will enter your MATLAB commands. If the Command
Window is not active, just click in it anywhere. Figure 1.1 contains an example of a newly
launched MATLAB Desktop.


Figure 1.1: A MATLAB Desktop.


1.3 TYPING IN THE COMMAND WINDOW
Click in the Command Window to make it active. When a window becomes active, its title
bar darkens. It is also likely that your cursor will change from outline form to solid, or from
light to dark, or it may simply appear. Now you can begin entering commands. Try typing
5+6; then press ENTER or RETURN. Next try factor (100), and finally sin(10). Your
command widow of the MATLAB Desktop should look like Figure 1.2.

1.4 SAVING AND RELOADING YOUR WORK
All variables used in the current MATLAB session are saved in the Workspace. You can
view the content of the Workspace by clicking on Workspace. You can also check contents
of the Workspace typing whos in the Command Window. For instance,
MATLAB 3 CHAPTER 1


Figure 1.2: Some Simple Commands.

>> whos
Name Size Bytes Class Attributes

a 1x1 8 double
ans 1x1 8 double
b 1x1 8 double
c 1x1 8 double

shows all variables used in current session. You can also use command who to generate a list
of variables used in current session
>> who

Your variables are:

a ans b c

To save your current workspace select Save Workspace as from the File menu. Chose a
name for your file, e.g. filename.mat and next click on Save. Remember that the file you just
created must be located in MATLAB's search path.
To load contents of the file named filename into MATLAB's workspace type load filename
in the Command Window. You can also select open from the File menu.
MATLAB 4 CHAPTER 1

1.5 INTERRUPTING A RUNNING PROGRAM
To interrupt a running program press simultaneously the Ctrl-c keys. Sometimes you have to
repeat pressing these keys a couple of times to halt execution of your program. This is not a
recommended way to exit a program, however, in certain circumstances it is a necessity. For
instance, a poorly written computer code can put MATLAB in the infinite loop and this
would be the only option you will have left.


1.6 LONG COMMAND LINES
To enter a statement that is too long to be typed in one line, use three periods, , followed
by Enter or Return. For instance,
>> x=sin(1)-sin(2)+sin(3)-sin(4)+sin(5)-sin(6)+sin(7)-sin(8)+...
sin(9)-sin(10)
x =
0.7744
You can suppress output to the screen by adding a semicolon after the statement
u = 2 + 3;

1.7 ONLINE HELP
MATLAB has an extensive online help mechanism. You can access the online help in one of
several ways. Typing help at the command prompt will reveal a long list of topics on which
help is available. Just to illustrate, try typing help general. Now you see a long list of
general purpose MATLAB commands. Finally, try help solve to learn about the command
solve. In every instance above, more information than your screen can hold will scroll by.

There is a much more user-friendly way to access the online help, namely via the MATLAB
Help Browser. You can activate it in several ways; for example, typing either helpwin or
helpdesk at the command prompt brings it up.

Alternatively, it is available through the menu bar under Help. Finally, the question mark
button on the tool bar will also invoke the Help Browser.

1.8 ENDING A SESSION
The simplestway to conclude aMATLAB session is to type quit at the prompt. You can also
click on the special symbol that closes your windows (usually an in the upper left- or right-
hand corner). Either of these may or may not close all the other MATLAB windows (which
we talked about in the last section) that are open. Still another way to exit is to use the Exit
MATLAB option from the File menu of the Desktop. Before you exit MATLAB, you should
be sure to save any variables, print any graphics or other files you need, and in general clean
up after yourself.