Você está na página 1de 21

Introduction

MATLAB
MATLAB (MATrix LABoratory) is an interactive
software system for numerical computations and
graphics. As the name suggests, MATLAB is
especially designed for matrix computations: solving
systems of linear equations, performing matrix
transformations, factoring matrices, and so forth. In
addition, it has a variety of graphical capabilities,
and can be extended through programs written in its
own programming language.
MATLAB is supported on Unix, Macintosh, and Windows
environments.
MATLAB Advantages

It simplifies the analysis of


mathematical models
It frees you from coding in lower-level
languages (saves a lot of time - with
some computational speed penalties)
Provides an extensible
programming/visualization environment
Provides professional looking graphs
The main window on the right is called the Command Window. This
is the window in which you interact with MATLAB. Once the
MATLAB prompt >> is displayed, all MATLAB commands are
executed from this window. In the figure, you can see that we
execute the command:
>> a = 7
In the top left corner you can view the Workspace window and the
Current Directory window. Swap from one to the other by clicking on
the appropriate tab. Note that when you first start up MATLAB, the
workspace window is empty. However, as you execute commands
in the Command window, the Workspace window will show all
variables that you create in your current MATLAB session. In this
example, the workspace contains the variable a.
In the bottom left corner you can see the
Command History window, which simply gives a
chronological list of all MATLAB commands that
you used.
Changing Directory
During the MATLAB sessions you will create files to
store programs or workspaces. Therefore create an
appropriate folder to store the lab files.
Set the current directory to be your H drive by clicking on
the Browse button next to the Current Directory path. Go
to My Computer (at the top), click on the + and select H.
THE MATLAB HELP SYSTEM
MATLABs help system provides information to assist you while
using MATLAB. Select Help -> MATLAB Help to open the help
system.
You can then browse the commands via the Contents window, look through
the Index of commands or Search for keywords or phrases within the
documentation. This is the most comprehensive documentation for
MATLAB and is the best place to find out what you need. You can also start
the MATLAB Help using the helpwin command.
A faster way to find out information about commands is via the help
command. If you just type help and hit return, you will be presented
with a menu of help options. If you type help <command>, MATLAB
will provide help for that particular command. For example, help hist
will call up information on the hist command, which produces
histograms. Note that the help command only displays a short
summary of how to use the command you are interested in. For more
detailed help documentation, use the doc command. For example, doc
hist will display the full documentation of the hist command, including
examples of how to use it and sample output.
Another very useful MATLAB command is the lookfor command. This will
also search for commands related to a keyword. Find out which
MATLAB commands are available for plotting using the command
lookfor plot in the command window. It may take a bit of time to get all
commands on the screen. You can stop the search at any time by
typing ctrl-c (the ctrl and the c key simultaneously).
Command Line

The last ans (the result of the last


calculation) is a quantity expressed in
scientific notation. MATLAB uses scientific
notation for very large numbers and very
small numbers.
The "e" notation is used for very large or very small numbers:
-1.3412e+03 = -1.3412x103 =-1341.2
-1.3412e-01 = -1.3412x10-1 =-0.13412
All computations in MATLAB are done in in double precision, which means 15
significant figures. The format - how MATLAB prints numbers - is controlled by the
"format" command:
>> a=pi (pi is a built-in constant)
a = 3.1416

>> format short e


>> a
a = 3.1416e+00

>> format long e


>> a
a = 3.141592653589793e+00

>> format long


>> a
a = 3.14159265358979
>> format short {this is the default format}
>> a
a = 3.1416
The basic arithmetic operators are + - * / ^ and these are
used in conjunction with brackets ( ) . The symbol ^ is used
to get exponents (powers): 2^4 = 16.
Example:
>> 2+3/4*5
ans = 5.7500
Note that in this calculation the result was 2+(3/4)*5 and not
2+3/(4*5) because MATLAB works according to the
priorities
1. quantities in brackets
2. powers
3. * / working left to right
4. + - working left to right
Common MATLAB functions
Assigning Variables
To create a (scalar) variable in MATLAB simply enter a valid
variable name at the command line and assign it a value using =.
Once a variable has been assigned a value it is added to the
MATLAB Workspace and will be displayed in the Workspace
window. For example, after entering:
>> height = 5
height =
5
>> width = 4
width =
4
the Workspace window will contain both height and width as
scalars:
There are a number of rules as to the variable names you
can use:
1. must start with a letter, followed by letters, digits, or
underscores. X12, rate_const, Flow_rate are all
acceptable variable names but not vector-A (since -
is a reserved character);
2. are case sensitive, e.g., FLOW, flow, Flow, FlOw are
all different variables;
3. must not be longer than 31 characters;
4. must not be a reserved word (i.e., special names which
are part of MATLAB language);
5. must not contain punctuation characters;

Be careful not to confuse the number 1 with the letter l, or


the number 0 with the letter O.
You should choose variable names that indicate quantities they
represent, e.g., width, cableLength, water_flow. You can use
multiple words in the same variable name either by capitalizing
the first letter of each word after the first one, e.g., cableLength,
or by putting underscores between words, e.g., water_flow.
You can see the variables in your workspace by looking at the
Workspace window or by using the whos command:

If you want to remove a variable from the MATLAB you can use the
clear command, e.g.,
>> clear width

removes width from the workspace. If you want to get rid of all the
variables in your workspace just type:
>> clear
SUPPRESSION OF OUTPUT
Up till now you have always seen the results of each command on the
screen. This is often not required or even desirable; the output might
clutter up the screen, or be long and take a long time to print.
MATLAB suppresses the output of a command if you finish the
command with a semi-colon - ; .
Enter:
>> height = 5
You will notice that MATLAB prints out the result of this command after
you press enter:
height =
5
Now enter:
>> height = 7;
MATLAB will not print the result of the command. With
scalar variables this doesnt make much difference -
but it can make a great deal of difference when dealing
with large lists of numbers.

Você também pode gostar