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

# Copyright 2010. The McGraw-Hill Companies, Inc.

Introduction to MATLAB
for Engineers, Third Edition
William J. Palm III
Chapter 1
An Overview of MATLAB

PowerPoint to accompany
The Default MATLAB Desktop, Fig. 1.1-
The Default MATLAB Desktop, Fig. 1.1-
1, page 5
1, page 5
1-1
1-2 1-2
Entering Commands and Expressions
MATA! retains yo"r pre#io"s \$eystro\$es.
%se the "p-arrow \$ey to scroll &ac\$ &ac\$ thro"gh
the comman's.
(ress the \$ey once to see the pre#io"s entry, an'
so on.
%se the 'own-arrow \$ey to scroll )orwar'. *'it a
line "sing the le)t- an' right-arrow \$eys the
Ba!spae \$ey, an' the "e#ete \$ey.
(ress the Enter \$ey to e+ec"te the comman'.
\$a#ar arithmeti operations
Ta&le 1.1,1, page -
1-% 1-%
An Examp#e \$ession& 'ages (-)
>> 8/10
ans =
0.8000
>> 5*ans
ans =
4
>> r=8/10
r =
0.8000
>> r
r =
0.8000
>> s=20*r
s =
16
1-* 1-*
Order of preedene
Ta&le 1.1,2, page .
1-+ 1-+
>> 8 + 3*5
ans =
23
>> 8 + (3*5)
ans =
23
>>(8 + 3)*5
ans =
55
>>4^2-12-8/4*2
ans =
0
>>4^2-12-8/(4*2)
ans =
3
1-, 1-,
*+amples o) (rece'ence, (age .
>> 3*4^2 + 5
ans =
53
>>(3*4)^2 + 5
ans =
149
>>27^(1/3) + 32^(0.2)
ans =
5
>>27^(1/3) + 32^0.2
ans =
5
>>27^1/3 + 32^0.2
ans =
11
1-( 1-(
*+amples o) (rece'ence, (age . Contin"e'
Commands for managing the wor! session
Ta&le 1.1,/, (age 12
1-) 1-)
\$peia# varia-#es and onstants
Ta&le 1.1,0, (age 10
1-. 1-.
Comp#ex /0m-er Operations& 'ages 1*-1+
The n"m&er c
1
1 1 , 2i is entere' as )ollows2
c1 = 1 2i.
An asteris\$ is not nee'e' &etween i or 3 an'
a n"m&er, altho"gh it is re4"ire' with a
#aria&le, s"ch as c2 = 5 i*c1.
!e care)"l. The e+pressions
y = 7/2*i
an'
x = 7/2i
gi#e two 'i))erent res"lts2
y = (7/2)i = 3.5i
an'
x = 7/(2i) = 3.5i.
1-11 1-11
/0meri disp#a2 formats3 Ta&le 1.1,5, (age 15
1-11 1-11
The 6es\$top Men"s an' Tool&ar. 7ig"re 1.2-1, page 18
Arra2s
The n"m&ers 0, 0.1, 0.2, 9, 10 can &e assigne' to the
#aria&le " &y typing u = 0:0.1:10.
To comp"te w 1 5 sin u )or u 1 0, 0.1, 0.2, 9, 10, the
session is:

>>u = 0:0.1:10;
>>w = 5*sin(u);
The single line, w = 5*sin(u), comp"te' the )orm"la
w 1 5 sin u 101 times.
1-12 1-12
Arra2 4ndex

>>u(7)
ans =
0.6000
>>w(7)
ans =
2.8232
%se the length )"nction to 'etermine how
many #al"es are in an array.
>>m = length(w)
m =
101
1-1% 1-1%
'o#2nomia# 5oots& 'age 21

To )in' the roots o) x
/
, ;x
2
< 00x , /0 1 0, the session
is
>>a = [1,-7,40,-34];
>>roots(a)
ans =
3.0000 + 5.000i
3.0000 - 5.000i
1.0000
The roots are x 1 1 an' x 1 / = 5i.
1-1* 1-1*
\$ome ommon#2 0sed mathematia# f0ntions
Ta&le 1./,1, (age 21
1-1+ 1-1+
>hen yo" type problem1,
1. MATA! )irst chec\$s to see i) pro&lem1 is
a #aria&le an' i) so, 'isplays its #al"e.
2. I) not, MATA! then chec\$s to see i)
pro&lem1 is one o) its own comman's, an'
e+ec"tes it i) it is.
/. I) not, MATA! then loo\$s in the c"rrent
'irectory )or a )ile name' problem1.m
an' e+ec"tes problem1 i) it )in's it.
0. I) not, MATA! then searches the
'irectories in its search path, in or'er,
)or problem1.m an' then e+ec"tes it i)
)o"n'.
1-1, 1-1,
\$2stem& diretor2& and fi#e ommands
Ta&le 1./,2,(age 2/
1-1( 1-1(
A graphis window showing a p#ot3 6ig0re 13%-1& page 2*3
1-1) 1-1)
\$ome MATLAB p#otting ommands
Ta&le 1./,/, (age 25
1-1. 1-1.
Linear A#ge-rai E70ations& 'age 2,
6x + 12y + 4z = 70
7x 2y + 3z = 5
2x + 8y 9z = 64

>>A = [6,12,4;7,-2,3;2,8,-9];
>>B = [70;5;64];
>>Solution = A\B
Solution =
3
5
-2
The sol"tion is x = 3, y = 5, an' z = 2.
1-21 1-21
?o" can per)orm operations in MATA! in two
ways2
1. In the interacti#e mo'e, in which all
comman's are entere' 'irectly in the
Comman' win'ow, or
2. !y r"nning a MATA! program store' in
script )ile. This type o) )ile contains
MATA! comman's, so r"nning it is
e4"i#alent to typing all the comman's@
one at a time@at the Comman' win'ow
prompt. ?o" can r"n the )ile &y typing its
name at the Comman' win'ow prompt.
1-21 1-21
COMME/T\$
The comment sym&ol may &e p"t anywhere in the
line. MATA! ignores e#erything to the right o) the
A sym&ol. 7or e+ample,
>>% This is a comment.
>>x = 2+3 % So is this.
x =
5
Bote that the portion o) the line &e)ore the A sign is
e+ec"te' to comp"te +.
1-22 1-22
The MATLAB Command window with the The MATLAB Command window with the
Editor8"e-0gger open3 6ig0re 13*91& 'age 2) Editor8"e-0gger open3 6ig0re 13*91& 'age 2)
1-2/
Ceep in min' when "sing script )iles2
1. The name o) a script )ile m"st &egin with
a letter, an' may incl"'e 'igits an' the
"n'erscore character, "p to 8/
characters.
2. 6o not gi#e a script )ile the same name
as a #aria&le.
/. 6o not gi#e a script )ile the same name
as a MATA! comman' or )"nction. ?o"
can chec\$ to see i) a comman', )"nction
or )ile name alrea'y e+ists &y "sing the
exist comman'.
1-2* 1-2*
"e-0gging \$ript 6i#es
(rogram errors "s"ally )all into one o) the
)ollowing categories.
1. Dynta+ errors s"ch as omitting a parenthesis
or comma, or spelling a comman' name
incorrectly. MATA! "s"ally 'etects the
more o&#io"s errors an' 'isplays a message
'escri&ing the error an' its location.
2. *rrors '"e to an incorrect mathematical
proce'"re, calle' runtime errors. Their
occ"rrence o)ten 'epen's on the partic"lar
inp"t 'ata. A common e+ample is 'i#ision &y
Eero.
1-2+ 1-2+
To locate program errors, try the )ollowing2
1. Test yo"r program with a simple #ersion o)
the pro&lem which can &e chec\$e' &y han'.
2. 6isplay any interme'iate calc"lations &y
remo#ing semicolons at the en' o)
statements.
/. %se the 'e&"gging )eat"res o) the
*'itorF6e&"gger.
1-2, 1-2,
'rogramming \$t2#e
a. The name o) the program an' any \$ey
wor's in the )irst line.
b. The 'ate create', an' the creatorsG names
in the secon' line.
c. The 'e)initions o) the #aria&le names )or
e#ery inp"t an' o"tp"t #aria&le. Incl"'e
'e)initions o) #aria&les "se' in the calc"lations
an' units of measurement for all input and all
output variables!
d. The name o) e#ery "ser-'e)ine' )"nction
calle' &y the program.
1-2( 1-2(
2. Input section Incl"'e inp"t 'ata
an'For the inp"t )"nctions an'
3. Calculation section
4. Output section This section might
contain )"nctions )or 'isplaying the
o"tp"t on the screen.
'rogramming \$t2#e :ontin0ed;
1-2-
\$ome 4np0t8o0tp0t ommands
7rom Ta&le 1.0,1, (age /1
1-2. 1-2.
Examp#e of a \$ript 6i#e& 'age %2
(ro&lem2
The spee' v o) a )alling o&3ect 'roppe' with no initial
#elocity is gi#en as a )"nction o) time t &y v 1 gt.
(lot v as a )"nction o) t )or 0 H t H t
final
, where t
final
is the
)inal time entere' &y the "ser.
1-%1 1-%1
Examp#e of a \$ript 6i#e :ontin0ed;
% Program falling_speed.m:
% Plots speed of a falling object.
% Created on March 1, 2009 by W. Palm
%
% Input Variable:
% tfinal = final time (in seconds)
%
% Output Variables:
% t = array of times at which speed is
% computed (in seconds)
% v = array of speeds (meters/second)
%
1-/1
Examp#e of a \$ript 6i#e :ontin0ed;
% Parameter Value:
g = 9.81; % Acceleration in SI units
%
% Input section:
tfinal = input(Enter final time in
seconds:);
%
1-/2
Examp#e of a \$ript 6i#e :ontin0ed;
% Calculation section:
dt = tfinal/500;
% Create an array of 501 time values.
t = 0:dt:tfinal;
% Compute speed values.
v = g*t;
%
% Output section:
Plot(t,v),xlabel(t (s)),ylabel(v m/s))
1-%% 1-%%
<etting =e#p 6rom the Text-oo!
<etting =e#p 6rom the Text-oo!

## Thro"gho"t each chapter margin notes i'enti)y where \$ey

terms are intro'"ce'.

## *ach chapter contains ta&les s"mmariEing the MATA!

comman's intro'"ce' in that chapter.

## At the en' o) each chapter is a s"mmary g"i'e to the

comman's co#ere' in that chapter.

## Appen'i+ A contains ta&les o) MATA! comman's,

gro"pe' &y category, with the appropriate page
re)erences.

## There are )o"r in'e+es. The )irst lists MATA! comman's

an' sym&ols, the secon' lists Dim"lin\$ &loc\$s, the thir'
lists M"(A6 comman's, an' the )o"rth lists topics.
1-/0
<etting =e#p 6rom MATLAB> <etting =e#p 6rom MATLAB>
The 60ntion Browser after The 60ntion Browser after plot plot has -een se#eted has -een se#eted
:6ig0re 13+-1& page %%; :6ig0re 13+-1& page %%;
1-/5
The MATLAB =e#p Browser The MATLAB =e#p Browser
6ig0re 13+-2& page %* 6ig0re 13+-2& page %*
1-/8
The =e#p /avigator
The =e#p /avigator
6ig3 13+-%& page %+
6ig3 13+-%& page %+
1-/;
MATLAB =e#p 60ntions&
6rom Ta-#e 13+-1& page %)

## help funcname: 6isplays in the Comman'

win'ow a 'escription o) the speci)ie' )"nction
funcname.

## lookfor topic: oo\$s )or the string topic in the

)irst comment line Ithe H1 lineJ o) the H*( te+t
o) all M-)iles )o"n' on MATA!(ATH Iincl"'ing
pri#ate 'irectoriesJ, an' 'isplays the H1 line )or
all )iles in which a match occ"rs.

## doc funcname: Kpens the Help !rowser to the

re)erence page )or the speci)ie' )"nction
funcname, pro#i'ing a 'escription, a''itional
remar\$s, an' e+amples.
1-/-
\$teps in engineering pro-#em so#ving
Ta&le 1.8,1,
(age /.
1-%. 1-%.
\$!eth of the dropped-pa!age pro-#em3
7ig"re 1.8,1, page 01
1-*1 1-*1
\$teps for deve#oping a omp0ter so#0tion
Ta&le 1.8,2, page 02
1-*1 1-*1
A piston& onneting rod& and ran! for an interna#
om-0stion engine3 7ig"re 1.8,2, 0/
1-*2 1-*2
'#ot of the piston motion vers0s ran! ang#e3
6ig0re 13,9%& page *+
1-*% 1-*%
?e2 Terms with 'age 5eferenes

Arg"ment, - MAT-)iles, 20
Array, 1. Mo'el, /.
Array in'e+, 1. K#erlay plot, 20
ADCII )iles, 21 (ath, 22
Assignment operator, 10 (rece'ence, .
Comman' win'ow, 8 Dcalar, -
Comment, 2; Dcript )ile, 2;
C"rrent 'irectory, 18 Dearch path, 22
6ata )ile, 21 Dession, ;
6ata mar\$er, 25 Dtring #aria&le, /1
6e&"gging, 2. Laria&le, ;
6es\$top, 5 >or\$space, 11
Graphics win'ow, 2/
1-** 1-**
Homewor\$ (ro&lem 25, 7ig"re (25, page 50
1-*+ 1-*+
1-*, 1-*,
Homewor\$ (ro&lem 2., 7ig"re (2., page 51