You are on page 1of 12

MATLAB INTRODUCTION

The Advantages of MATLAB


1. Ease of use - 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 pre-written programs.
2. Platform independence - The language is supported on windows
NT2!!!"#, Linux, several versions o$ %N&", and the 'acintosh. #rograms
written on any plat$orm will run on all the other plat$orms, and data $iles
written on any plat$orm may be read transparently on any other plat$orm.
(. Predefined functions - ')TL)* comes complete with an extensive library o$
prede$ined $unctions that provide solutions to many basic technical tasks. &n
addition to the large library o$ $unctions built into the basic ')TL)*
language, there are many special-purpose toolboxes available to help some
complex problems in speci$ic areas.
+. Device-Independent Plotting - %nlike most other computer languages,
')TL)* has many integral plotting and imaging commands. The plots and
images can be displayed on any graphical out put device supported by the
computer on which ')TL)* is running. This capability makes ')TL)* an
outstanding tool $or visuali,ing technical data.
-. Graphical User Interface GUI! - ')TL)* includes tools that allow a
programmer to interactively construct a ..%.& $or his or her program.
Disadvantages of MATLAB
1. "lo# - ')TL)* programs are compiled into a device-independent p-code and
then p-code instructions are interpreted at runtime. There$ore ')TL)* executes more
slowly than compiled languages. This problem can be mitigated by the use o$ the
')TL)* compiler.
2. $ost - ) $ull copy o$ ')TL)* is $ive to ten times more expensive than a
conventional / or 012T2)N compiler.
MATLAB Applications
'ath and computation.
)lgorithm development.
3ata ac4uisition.
'odeling, simulation, and prototyping.
3ata analysis, exploration, and visuali,ation.
5cienti$ic and engineering graphics.
1
)pplication development, including ..%.& building.

The MATLAB "%stem
The ')TL)* system consists o$ $ive main parts 6
1 Development Environment - ')TL)* desktop and command window, a command
history, an editor and debugger, and browsers $or viewing help, the workspace, and the
search path
2 The MATLAB Mathematical &unction Li'rar% - This is a vast collection o$
computation algorithms ranging $rom elementary $unctions like sum, sine, cosine, and
complex arithmetic, to more sophisticated $unctions like matrix inverse, matrix eigen
values, *essel $unction, and $ast 0ourier trans$orms.
( The MATLAB language - This is the high level matrixarray language with control $low
statements, $unctions, data structures, inputoutput, and ob7ect-oriented programming
$eatures.
+ Graphics - ')TL)* has extensive $acilities $or displaying vectors and matrices as
graphs, as well as annotating and printing these graphs.
- The MATLAB Application Program Interface API! 8 This is a library that allows you
to write / and 012T2)N programs that interact with ')TL)*

&ile t%pes -
')TL)* have three types o$ $iles $or storing in$ormation6
M-files( 5tandard )5/&& text $iles, with a )m extension to the $ilename. There are two types o$
these $iles6 script $iles and $unction $iles. )ll built-in $unction in ')TL)* are '-$iles, most o$
which reside on computer in precompiled $ormat.
Mat-files( They are binary data-$iles, with a )mat extension to the $ile name. 'at - $iles are
created by ')TL)* when we save data with the save command. 'at 8 $iles can be loaded into
')TL)* with the load command.
Met-files( They are ')TL)* callable 012T2)N and / program, with a )me* extension to the
$ile name.
2
"tarting and +uitting MATLAB
"tarting MATLAB( 1n windows plat$orms, to start ')TL)* double 8 click the ')TL)*
shortcut icon on windows desktop.
+uitting MATLAB( To end ')TL)* session, select E*it MATLAB $rom the file menu in the
desk top, or type 4uit in the /ommand 9indow.
1n %N&" #lat$orms, to start ')TL)*, type ')TL)* at the operating system : prompt. The
')TL)* command prompt is ;3% << $or educational version, << $or the pro$essional version
The MATLAB Des,top
9hen we start ')TL)* version =, a special window called the ')TL)* desktop
appears. The de$ault con$iguration o$ the ')TL)* desktop is shown in $igure 1.
Des,top Tools
(
The ma7or Tools which are accessible $rom the ')TL)* desktop are6
- The /ommand 9indow.
- The /ommand >istory 9indow
- The 5tart *utton
- The >elp *rowser
- The /urrent 3irectory *rowser
- The 9orkspace *rowser
- The ;ditor3ebugger
- The )rray ;ditor
The $undamental unit o$ data in any ')TL)* program is the array. )n array is a collection o$
data values organi,ed into rows and columns and known by a single name. ;ven scalars are
treated as arrays by ')TL)* - they are simple arrays with only one row and one column.
Edit -indo#s - The edit windows is a programming text editor with the ')TL)* languages
$eatures highlighted in di$$erent colors. /omments in an '-$ile appear in green, variables and
numbers appear in black, complete character strings appear in magenta, in complete character
strings appear in red.
General $ommands
help topic provides help on topic
who list variables currently in the workspace
whos lists variables currently in the workspace with their si,e.
what lists m-, mat-, and mex. $iles on the disk.
clear clears the workspace, all variables are removed
clear x,y,, clears only variables x, y and ,
clear all clears all variables and $unctions $rom workspace
clc clears command window, command history is lost.
cl$ clears $igure window
cd change the current working directory
dir list contents o$ the current directory
mkdir creates a directory
Never use a variable with the same name as a ')TL)* $unction or
command. &$ you do so, that $unction or command will become inaccessible.
Never create an M-file with the same name as a ')TL)* $unction or
command
.aria'les
')TL)* does not re4uire any type declarations or dimension
statements
9hen ')TL)* encounters a new variable name, it automatically
creates the variable and allocates the appropriate amount o$ storage.
price?mango @ 1-
+
creates a 1 - by - 1 matrix named price?mango and stores the value 1- in its single
element.
Aariable name consist o$ a letter, $ollowed by any number o$ letters,
digits, or underscores. ')TL)* uses only the $irst B( characters o$ a variable
name.
')TL)* is case sensitiveC it distinguishes between upper case and
lowercase letters.
/eserved #or, list - $or, end, i$, while, $unction, return, elsei$, case, otherwise,
switch, continue, else, try, catch, global, persistent, break.
0um'ers-
')TL)* uses conventional decimal notation, with an optional decimal
point and leading plus or minus sign.
5cienti$ic notation uses the letter e to speci$y a power-o$- ten scale $actor
&maginary numbers use either i or 7 as a su$$ix.
)ll numbers are stored internally using the long $ormat speci$ied by the
&;;; $loating point standard. 0loating 8 point numbers have a $inite precision o$
roughly 1B signi$icant decimal digits and a $inite range o$ roughly 1!
-(!D
to 1!
E(!D

;xamples o$ legal numbers6
(, -FF, !.!!!1, F.B+12, 1.2 e-1!, B.2 e11, 1i, -(.1-7, Be-i
$omple* 0um'ers6 ')TL)* recogni,es the litters i and 7 as the imaginary number 1 . )
complex number 2E-i may be input as 2E-i or 2E-Gi in ')TL)*. The $ormer case is always
interpreted as a complex number whereas the later case is taken as complex only i$ i has not been
assigned any local value. The same is true $or 7.

Displa% &ormats
3isplay numeric value o$ H using di$$erent $ormats
-
1perators
')TL)* o$$ers the $ollowing basic arithmetic operations
1peration "%m'ol E*ample
)ddition E -E1!
5ubtraction - 1!-+
'ultiplication G 2G(
3ivision or I 1!2 or 2I1!
;xponentiation J (J(
/omplex con7ugate transpose `
;xpressions are evaluated $rom le$t to right with the exponentiation having the highest
precedence, $ollowed by multiplication and division having e4ual precedence and then by addition
and subtraction, having e4ual precedence.
#arentheses can be used to alter, in which case there rules o$ precedence are applied within
each set o$ parentheses, by starting with the inner most set and preceding outward.
&unctions(
')TL)* provides a large number o$ standard elementary mathematical $unctions
>elp el$un
"aving the .aria'les "tored in Memor%(
%se the ')TL)* menus and use the 0ile and 5ave 9orkspace )s menu selection or use
the ')TL)* save command.
The de$ault $ile is matlab.mat
%se the L1)3 command to load the de$ault $ile.
&$ you wish to same the variables in a $ile with a di$$erent name, you can use the L1)3
and 5)A; commands $ollowed by a $ile name
22 save *%3
5)A; command place the $iles in directory c6ImatlabIbin and automatically attaches the
extension K)matL to the $ile name.
B
sin4sind4sinh4asin4asind4asinh4e*p4log564log74s8rt4a's4angle4con94imag4real
"$/IPT M-files
To create a script '-$ile, click on the blank page icon on the ')TL)* desktop toolbar, or
choose new $rom the $ile menu and select '-$ile
M 5cript '-$ile example 1.m
erasers@+C M number o$ each item
pads@BC
taps@2C
&tems@erasersEpadsEtapes
cost@erasersG2-EpadsG-2EtapsGFF
average?cost@costitems
This $ile can be saved to disk and executed immediately by
1. choosing save and run $rom the debug menu,
2. pressing the save and run button on the editor toolbar,
(. simply pressing the $unction key 0-
+. by choosing save $rom the $ile menu, then at the ')TL)* prompt type the name o$ the
script $ile without the )m extension
=
M 5cript '-$ile example 1-m
erasers@+C M number o$ each item
pads@BC
tape@input NOenter the number o$ rolls o$ tape purchased<PQC
items@ erasersEpadsEtape
cost@erasersG2-EpadsG-2EtapeGFF
average?cost@costitems
1. ;nter the number o$ roll o$ tape purchased < round Ns4rt N1(QQ-1Q
2. To see the e$$ect o$ the echo command add it to the script $ile and execute it
/ectangular and Polar $oordinates
2ectangular /oordinates, x@aEbi
#olar /oordenates,
&$ NaEb7Q@ , then
2 2
b a M +

'

< +

! Q N tan 1D!
! Q N tan
1
1
a if a b
a if a b

a@' /os NQ b@ ' 5in NQ


aEb7@' /os NQ E 7 ' 5in NQ
) second way to convert $orm polar notation to rectangular notation
radians in is Me M
i



D
;xample6 /alculate the result o$ the e4uation6
a@2.-E(.(iC
b@=GexpNiG(!GN2Gpi(B!QQC
c@+.+GexpNiG2-GN2Gpi(B!QQC
d@2.1D-1.BiC
x@NaGbQNcEdQ
x@!.==F+ E +.B2F+i
F
Q B . 1 1D . 2 N Q 2- + . + N
Q (! = N Q ( . ( - . 2 N
i
i
X
+
+

MATLAB EXERCISE
1. /ompute the $ollowing 4uantities.
aQ
1
- -
-
2
1
1
1 2
2

,
_

with Compare and


bQ
( )
1
1 -
1 -
(
2

,
_

cQ
1
(
1
2
r with r Area
2. /alculate the $ollowing 4uantities6
aQ
Q 1! N log Q, N log Q, N ln ,
-
1!
(
1!
( (
and e e e
bQ
1B(
e
(. /alculate the $ollowing 4uantitiesC
NaQ sin
2
tan , cos ,
B

and

NbQ cot (!
o
, cosec +-
o
NcQ sin
B
cos
B
2 2

+
NdQ y@cosh
2
x 8 sinh
2
x with x @(2H

+. /ompute the $ollowing 4uantities
NaQ
i
i
( 1
( 1

+
NbQ e
+

i
NcQ ;xecute the commands exp Npi2G iQ and exp Npi2iQ
/an you explain the di$$erence between the two resultsR
-. The distance traveled by a ball $alling in the air is given by the e4uation
x @ x E v t E
2
1
at
2
%se ')TL)* to calculate the position o$ the ball at time t @ -
x
!
@ 1!m, v @ 1- ms, and a @ -F.D msec
2
B. 5uppose that x@ ( and y@ +. %se ')TL)* to evaluate the $ollowing expressions.
2
2 2
Q N y x
y x

=. 5uppose that u@ 1 and v@ ( evaluate the $ollowing expressions using ')TL)*


1!
NaQ
v (
+ u
NbQ
2
2
Q N
2
v u
v
+

NcQ
( (
(
u v
v

NdQ
2
(
+
v
D. The $ollowing ')TL)* statement, plots the $unction yNxQ @2e
x 2 . !
$or the range
! S x S1!
x@ !6 !.161!C
y@ 2Gexp N-!.2GxQC
plot Nx, yQC
%se the ')TL)* edit window to create a new empty m-$ile, type-there statements into
the $ile, and save the $ile with the name test1.m. Then execute the program by typing the
name test1 in the command windows. 9hat result do you getR
F. /hange the current directory to mynewdir. Then open an edit window and add the
$ollowing lines.
M /reate an input array $rom 8 2Gpi to 2Gpi
t @ -2 G pi6 pi1!6 2GpiC
M /alculate Tsin NtQT
x @ abs Nsin NtQQC
M #lot result
plot Nt, xQC
5ave the $ile with the name test2.m and execute it by typing test2 in the command
window. 9hat happensR
1!. #lot the $ollowing $unctions using the #L1T $unction6
aQ sinNxQ $or -1! x 1!
bQ sinNxQcosNxQ $or -1! x 1!
cQ cosNxQsinNxQexpNx1!Q $or -1 x 1
11. 9rite a script $ile in ')TL)* that displays the results o$ the $ollowing calculations. Uou
should have a single script $ile that displays all result. The script $ile should clearly
identi$y each problem and pause a$ter each answer. )nswer should be displayed only a$ter
the user presses the key or clicks the mouse.
NaQ N-EBQ2 in short $ormat.
NbQ The value o$ -

to 1B digits
NcQ The value o$ e displayed as a $raction Nratio o$ two whole numbersQ.
NdQ %se the ')TL)* $unction dec2hex to $ind how the number B--(- would be
displayed in base 1B NhexadecimalQ
NeQ The result o$ N(EiQ N1EiQ in rectangular coordinates Nnumeric $ormat shortQ.
N$Q The value o$
j 2

NgQ The result o$ N(EiQ G N2E(iQ as a magnitude and a phase.
NhQ The angle o$ N1EiQ Nnumeric $ormat shortQ displayed in degrees.
11
NiQ
- . B
+ (

+ i
N7Q 0ind the real and imaginary parts, magnitude, and angle o$ (E+i.
12

You might also like