Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
1
NU
ME
RIC
AL
ME
TH
OD
S F
OR
EN
GIN
EE
RS
C
OE
B 2
23
PA
RT
I : INT
RO
DU
CT
ION
& M
AT
LAB
Dr. H
an
im S
alle
h
De
pa
rtme
nt o
f Me
cha
nica
l En
gin
ee
ring
Un
ive
rsiti Te
na
ga
Na
sion
al
20
07
/20
08
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
2
Handout 1
: MATHEMATICAL MODELLING
1.2 In
trod
uctio
n
1.1 C
onserv
ation
law
1.3 A
n ex
amp
le of a sim
ple m
athem
atical mo
del
1.4 N
um
erical meth
ods: an
exam
ple
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
3
1.1
Introductio
n
Wh
at is math
ematical m
od
elling?
• It is a w
ay o
f exp
ressing
ph
ysical sy
stems (p
hen
om
ena,
pro
cesses, etc.) in m
athem
atical terms
• A
Ma
them
atica
l Mod
el is an ex
pressio
n o
r equ
ation
that sh
ow
s
the relatio
nsh
ip b
etween
ph
ysical q
uan
tities involv
ed in
the
pro
blem
s und
er consid
eration
. It usu
ally h
as the fo
llow
ing
form
:
Y =
f { X, P
, F }
Wh
ere:
Y is th
e dep
end
ent v
ariable
X is th
e ind
epen
den
t variab
les,
P is th
e param
eters and
F is th
e forcin
g fu
nctio
ns
Th
e abov
e equ
ation
imp
lies that if w
e are giv
en v
alues o
f X, P
an
d F
and
we k
no
w th
e relationsh
ips o
f these q
uan
tities with
Y, w
e can th
en
AT
TE
MP
T to
solv
e for Y
.
So
, first of all, h
ow
do
we g
o ab
ou
t findin
g su
ch relatio
nsh
ips am
on
g th
e
quan
tities?
1.2
Conservatio
n law
• B
asically, it’s a law
sayin
g th
at all the q
uan
tities (forces, m
ass,
etc.) in a p
articular sy
stem m
ust b
e balan
ced, i.e. p
ut sim
ply
: In
som
e special cases, w
hen
there is n
o ch
ange in
the sy
stem, w
e can say
that
Ch
an
ge =
Increa
ses - Decrea
ses T
RA
NS
IEN
T sy
stems
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
4
• S
om
e exam
ples o
f eng
ineerin
g sy
stems g
ov
erned
by co
nserv
ation
laws:
� F
luid
flow
in p
ipes: co
nserv
ation
of m
ass
� E
lectric circuit: co
nserv
ation
of en
ergy an
d ch
arge
� A
mo
vin
g b
od
y: co
nserv
ation
of fo
rce and
mo
men
tum
- an ex
amp
le follo
ws
1.3
An example of a
simple mathematical
model
Let u
s consid
er a simp
le mo
del to
determ
ine th
e ‘termin
al velo
city’ o
f a
freely fallin
g p
arachutist.
Fo
rces acting
on
the p
arachu
tist are –
1. F
D, th
e Grav
itation
al Fo
rce and
2. F
v the v
iscous fo
rce or th
e force d
ue to
air resistance.
Acco
rdin
g to
New
ton
’s Seco
nd
Law
F =
m.a i.e.:
M F
dt
dv
=
(1
)
vD
FF
F+
=
(2)
with
M
gF
D=
and
cv
Fv
−=
wh
ere c = D
rag co
efficient an
d
g =
Acceleratio
n d
ue to
grav
ity
M
cv
Mg
dt
dv
−=
(3
)
vM c
gd
t
dv
−=
(4
)
Ch
an
ge =
0 =
Increa
ses – D
ecreases;
∴∴∴ ∴ In
creases =
Decrea
ses
ST
EA
DY
-ST
AT
E sy
stems
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
5
Th
is can b
e integ
rated an
alytically
usin
g calcu
lus as:
)1(
)(
)/
(t
mc
ec gm
tv
−−
=
(5
)
Eq
uatio
n (5
) is in th
e form
usu
ally referred
to as th
e AN
AL
YT
ICA
L o
r
the E
XA
CT
solu
tion
for a g
iven
pro
blem
. Usin
g th
is equ
ation
, we can
also calcu
late v for an
y tim
e t. Th
e results can
be tab
ulated
and
a grap
h
of v ag
ainst t can
be p
lotted
, as sho
wn
in F
igu
re 1.1
. T
ime, t, s
Velo
city, v
m/s
0
0.0
0
2
16
.40
4
27
.77
6
35
.64
8
41
.10
10
44
.87
12
47
.49
∞
53
.39
In th
e abov
e pro
blem
, the situ
ation
is qu
ite simp
le that th
e solu
tion
can b
e
deriv
ed an
alytically
. Wh
at if there is n
o an
alytical so
lutio
n? F
or ex
amp
le,
wh
at if equatio
n (4
) cann
ot b
e integ
rated to
giv
e equatio
n (5
)? In
this case
we m
ust reso
rt to u
sing
NU
ME
RIC
AL
ME
TH
OD
S in
ord
er to so
lve th
e
pro
blem
. Th
is results in
a solu
tion
that w
e call a NU
ME
RIC
AL
solu
tion
.
1.4
Numerical solutio
n: an example
Lo
ok
ing
at equ
ation
(4), th
e solu
tion
requires an
app
roxim
ation
for th
e
deriv
ative d
v/dt. U
sing
a FIN
ITE
DIF
FE
RE
NC
E ap
pro
xim
ation
,
ii
ii
tt
tv
tv
t v
dt
dv
− −=
∆ ∆≅
+
+
1
1)
()
(
Th
erefore, su
bstitu
ting
into
(4), w
e hav
e
)(
)(
)(
1
1
i
ii
ii
tv
m cg
tt
tv
tv
dt
dv
−=
− −=
+
+
Rearran
gin
g th
e terms:
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
6
)
()
()
()
(1
1i
ii
ii
tt
tv
m cg
tv
tv
−
−
+=
++
No
te that th
is is actually
a solu
tion
techn
iqu
e kno
wn
as the E
UL
ER
’S
meth
od
– w
e will see th
is in m
ore d
etails later in th
e cou
rse. Startin
g w
ith
t1 = 0
and
v(t1 ) =
0 an
d lettin
g th
e step size b
e 2 s.
s
mt
v/
60
.1
9)
2(0
1.6
8
5.1
28.
90
)(
2=
−+
=
s
mt
v/
00
.3
2)
2()
60
.1
9(
1.6
8
5.1
28.
96
0.
19
)(
3=
−+
=
Con
tinuin
g fo
r the rest o
f the tim
e t, we th
en h
ave:
Tim
e, t, s V
elocity
, v m
/s
0
0.0
0
2
19
.60
4
32
.00
6
39
.85
8
44
.82
10
47
.97
12
49
.96
∞
53
.39
As
can b
e seen
, th
e so
lutio
n is
in th
e fo
rm o
f n
um
bers
at a
selected
nu
mb
er of d
iscrete po
ints - n
ot an
equatio
n as in
the an
alytical so
lutio
n in
equ
ation
(5). U
sing
the ab
ov
e table, w
e can p
lot th
e solu
tion
and
com
pare
it to th
e on
e ob
tained
usin
g th
e analy
tical solu
tion
. Th
e com
pariso
n is
sho
wn
in F
igu
re 1.2
.
An
imp
ortan
t poin
t to n
ote is th
at the n
um
erical results are clo
se to th
e
analy
tical resu
lts but
are no
t 1
00
%
accurate.
W
e can
fo
r ex
amp
le
imp
rov
e the so
lutio
n b
y red
ucin
g th
e step size fro
m 2
s to 1
s or 0
.5 s.
Th
e kno
wn
draw
back
s, ho
wev
er, are in term
s of th
e com
pu
ting
times an
d
effort. W
e will d
eal mo
re with
these issu
es as we g
o fu
rther in
this co
urse.
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
7
As a g
eneral ru
le, there is u
sually
a trade-o
ff betw
een effo
rt and
accuracy
and
as we can
exp
ect – th
e mo
re accurate th
e solu
tion is, th
e greater effo
rt
is need
ed to
ob
tain it.
Next to
pic - Chapter 2: P
rogramming
using MATLAB
Fig
ure 1
.1
Fig
ure 1
.2
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
8
2.1
Intro
du
ction
Handout 2
: Introductio
n to
MATLAB
MATLAB fu
ndamentals
2.2
MA
TL
AB
env
iron
men
t
2.3
Assig
nm
ent o
f valu
es to v
ariable n
ames
2.4
Use o
f bu
ilt-in fu
nctio
ns
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
9
2.1
In
trod
uc
tion
• T
he
nam
e M
AT
LA
B
stand
s fo
r M
AT
rix
LA
Bo
ratory
. It
is a
hig
h-
perfo
rman
ce lan
gu
age
that
integ
rates co
mp
utatio
n,
visu
alization
, an
d
pro
gram
min
g in
an easy
-to-u
se env
iron
men
t.
• T
he M
AT
LA
B en
viro
nm
ent u
ses basic d
ata elemen
t in th
e form
of array
s. Fo
r
this
reason
, p
rob
lems
inv
olv
ing
matrix
an
d
vecto
r fo
rmu
lation
s can
b
e
com
pu
ted accu
rately an
d efficien
tly.
• M
AT
LA
B
also
has
an
excellen
t grap
hics
represen
tation
cap
ability
, w
hich
assists greatly
in an
alysin
g an
d u
nd
erstand
ing th
e com
pu
ted p
rob
lems.
• M
AT
LA
B
com
es w
ith
a fam
ily
of
too
lbo
xes.
Th
ese are
collectio
ns
of
MA
TL
AB
fun
ction
s (M-files) th
at exten
d M
AT
LA
B cap
abilities, en
ablin
g th
e
user to
solv
e specific g
rou
ps o
f pro
blem
s. Ex
amp
les inclu
de sig
nal p
rocessin
g,
con
trol sy
stems, w
avelets, an
d cu
rve fittin
g am
on
g o
thers.
• S
om
e areas in w
hich
MA
TL
AB
is used
:
� G
eneral m
athem
atical calculatio
ns
� P
rogram
min
g - alg
orith
m d
evelo
pm
ent
� M
od
elling, sim
ulatio
n, an
d p
roto
typ
ing
� D
ata analy
sis, presen
tation
and
visu
alization
� A
pp
lication
d
evelo
pm
ent,
inclu
din
g
grap
hical
user
interface
(GU
I)
bu
ildin
g
2.2
T
he
MA
TL
AB
en
viro
nm
en
t R
efer to fig
ures 2
.1- 2
.4, to
geth
er with
the related
descrip
tion
s.
2.3
A
ss
ign
me
nt o
f va
lue
s to
va
riab
le n
am
es
A
ssign
men
t of v
alues to
scalar variab
les is similar to
oth
er com
pu
ter langu
ages, w
ith
on
e imp
ortan
t distin
ction, h
ow
ever. In
MA
TL
AB
, the m
athem
atical exp
ression
s
inv
olv
e entire m
atrices, and
MA
TL
AB
do
es no
t requ
ire any ty
pe d
eclaration
s or
dim
ensio
n statem
ents –
on
ce a new
variab
le is enco
un
tered, it au
tom
atically creates
the v
ariable an
d allo
cates the ap
pro
priate am
ou
nt o
f storag
e. Fo
r exam
ple, ty
pin
g
>>
A =
3;
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
10
creates a 1-b
y-1
matrix
nam
ed A
, wh
ich is th
en g
iven
a valu
e of 3
in its sin
gle
elemen
t. No
te that M
AT
LA
B treats n
ames in
case-sensitiv
e man
ner.
No
te that th
e use o
f semi-co
lon
at the en
d o
f a statemen
t is a characteristic o
f
MA
TL
AB
. It is used
to su
pp
ress the ‘ech
o’ o
f the statem
ent, i.e. if y
ou
remo
ve th
e
semi-co
lon
, yo
u w
ill hav
e
>>
A =
3
A =
3
>>
To
defin
e a row
vecto
r,
>>
A=
[1 2
3 4
5]
A =
1 2
3 4
5
>>
A co
lum
n v
ector can
be d
efined
usin
g
>>
A =
[1 ; 2
; 3 ;4
; 5]
A =
1
2
3
4
5
or sim
ply
by u
sing th
e transp
ose o
perato
r `’`,
>>
A =
[1 2
3 4
5]'
A =
1
2
3
4
5
In a sim
ilar fashio
n, w
e can w
rite a matrix
, row
by ro
w, u
sing th
e follo
win
g co
mm
and
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
11
>>
A=
[1 2
3; 4
5 6
; 7 8
9]
A =
1 2
3
4 5
6
7 8
9
We can
recall wh
at variab
les we h
ave p
ut in
to M
AT
LA
B b
y ty
pin
g ‘w
ho
’ or ‘w
ho
s’:
>>
B =
3;
>>
A =
[1 2
3; 4
5 6
; 7 8
9];
>>
wh
o
Yo
ur v
ariables are:
A B
>>
wh
os
Nam
e Size B
ytes C
lass
A 3
x3
72
do
ub
le array
B 1
x1
8 d
ou
ble arra
y
Gran
d to
tal is 10
elemen
ts usin
g 8
0 b
ytes
MA
TL
AB
can also
deal w
ith co
mp
lex n
um
bers. It is co
nv
enien
t to assig
n a v
ariable
nam
e, usu
ally as i o
r j, to th
e squ
are roo
t of –
1,
>>
i = sq
rt(-1)
i =
0 +
1.0
00
0i
>>
x =
3 +
2i
x =
3.0
00
0 +
2.0
00
0i
2.4
U
se
of b
uilt-in
fun
ctio
ns
M
AT
LA
B p
rov
ides a larg
e nu
mb
er of stan
dard
elemen
tary m
athem
atical fun
ction
s,
inclu
din
g ab
s, sqrt, ex
p, etc. It also
offers m
any ad
van
ced m
athem
atical fun
ction
s,
such
as the B
essel and
gam
ma fu
nctio
ns. M
ost o
f these fu
nctio
ns accep
t com
plex
argu
men
ts.
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
12
Tr
igo
no
me
tric
sin - S
ine.
sinh
- Hyp
erbo
lic sine.
asin - In
verse sin
e.
asinh
- Inv
erse hyp
erbo
lic sine.
cos - C
osin
e.
cosh
- Hyp
erbo
lic cosin
e.
acos - In
verse co
sine.
acosh
- Inv
erse hyp
erbo
lic cosin
e.
tan - T
angen
t.
tanh
- Hyp
erbo
lic tang
ent.
atan - In
verse tan
gen
t.
atan2
- Fo
ur q
uad
rant in
verse tan
gen
t.
atanh
- Inv
erse hyp
erbo
lic tangen
t.
sec - Secan
t.
sech - H
yp
erbo
lic secant.
asec - Inv
erse secant.
asech - In
verse h
yp
erbo
lic secant.
csc - Co
secant.
csch - H
yp
erbo
lic cosecan
t.
acsc - Inv
erse cosecan
t.
acsch - In
verse h
yp
erbo
lic cosecan
t.
cot - C
otan
gen
t.
coth
- Hyp
erbo
lic cotan
gen
t.
acot - In
verse co
tangen
t.
acoth
- Inv
erse hyp
erbo
lic cotan
gen
t.
Ex
po
ne
ntia
l
exp
- Ex
po
nen
tial.
log - N
atural lo
garith
m.
log1
0 - C
om
mo
n (b
ase 10
) logarith
m.
log2
- Base 2
log
arithm
and
dissect flo
ating
po
int n
um
ber.
po
w2
- Base 2
po
wer an
d scale flo
ating p
oin
t nu
mb
er.
sqrt - S
qu
are roo
t.
Co
mp
lex
abs - A
bso
lute v
alue.
angle - P
hase an
gle.
con
j - Co
mp
lex co
nju
gate.
imag
- Co
mp
lex im
agin
ary p
art.
real - Co
mp
lex real p
art.
un
wrap
- Un
wrap
ph
ase angle.
isreal - Tru
e for real arra
y.
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
13
cplx
pair - S
ort n
um
bers in
to co
mp
lex co
nju
gate p
airs.
No
te that th
ese fun
ction
s will o
perate d
irectly o
n v
ector an
d m
atrix q
uan
tities, as well
as a scalar valu
e. Fo
r exam
ple:
>>
B =
10
0;
>>
sqrt(B
)
ans =
10
>>
A =
[4 9
16
25
36
];
>>
Sq
rt(A)
??? Un
defin
ed co
mm
and
/fun
ction
'Sq
rt'.
>>
sqrt(A
)
ans =
2 3
4 5
6
• Y
ou
can alw
ays ask
for M
AT
LA
B assistan
ce wh
en y
ou
are no
t sure o
f the
com
man
ds. T
yp
e ‘help
co
mm
an
d’ o
r ‘loo
kfo
r com
ma
nd
’. Fo
r exam
ple:
>>
help
on
es
ON
ES
On
es array.
ON
ES
(N) is an
N-b
y-N
matrix
of o
nes.
ON
ES
(M,N
) or O
NE
S([M
,N]) is an
M-b
y-N
matrix
of o
nes.
ON
ES
(M,N
,P,...) o
r ON
ES
([M N
P ...]) is an
M-b
y-N
-by-P
-by-... arra
y o
f
on
es.
ON
ES
(SIZ
E(A
)) is the sam
e size as A an
d all o
nes.
……
……
…
>>
loo
kfo
r on
es
ON
ES
On
es array.
SP
ON
ES
Rep
lace no
nzero
sparse m
atrix elem
ents w
ith o
nes.
SL
UP
DA
TE
Rep
lace blo
cks fro
m a p
revio
us release w
ith n
ewer o
nes.
DR
AM
AD
AH
Matrix
of zero
s and
on
es with
large d
etermin
ant o
r inv
erse.
• H
avin
g
learned
h
ow
to
assig
n
valu
es to
v
ariables,
we
can
then
see
ho
w
MA
TL
AB
p
erform
s m
athem
atical o
peratio
ns
on
th
ese v
ariables
�
HA
ND
OU
T 3
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
14
Fig
ure 2
.1
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
15
F
igu
re 2.3
Fig
ure 2
.2
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
16
Fig
ure 2
.4
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
17
Handout 3
: Introductio
n to
MATLAB
MATLAB fu
ndamentals
3.1
Matrix
and
array o
peratio
ns
3.2
Plo
tting in
MA
TL
AB
3.3
Oth
er resou
rces
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
18
3.1
Matrix and array operatio
ns
Arra
y o
pe
ratio
ns
Th
e follo
win
g ex
amp
les dem
on
strate ho
w arra
y o
peratio
ns are carried
ou
t in
MA
TL
AB
:
• C
olo
n n
otatio
n
>>
x =
0:2
x =
0 1
2
>>
x =
0:0
.1:1
x =
Co
lum
ns 1
thro
ugh
5
0 0
.10
00
0.2
00
0 0
.30
00
0.4
00
0
Co
lum
ns 6
thro
ugh
10
0.5
00
0 0
.60
00
0.7
00
0 0
.80
00
0.9
00
0
Co
lum
n 1
1
1.0
00
0
• U
sing arra
y w
ith fu
nctio
ns
>>
x =
0:0
.1*
pi:p
i
x =
Co
lum
ns 1
thro
ugh
5
0 0
.31
42
0.6
28
3 0
.94
25
1.2
56
6
Co
lum
ns 6
thro
ugh
10
1.5
70
8 1
.88
50
2.1
99
1 2
.51
33
2.8
27
4
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
19
Co
lum
n 1
1
3.1
41
6
>>
y =
sin(x
)
y =
Co
lum
ns 1
thro
ugh
5
0 0
.30
90
0.5
87
8 0
.80
90
0.9
51
1
Co
lum
ns 6
thro
ugh
10
1.0
00
0 0
.95
11
0.8
09
0 0
.58
78
0.3
09
0
Co
lum
n 1
1
0.0
00
0
• A
dd
ressing arra
y co
nten
ts
>>
x(3
)
ans =
0.6
28
3
>>
y(1
:5)
ans =
0 0
.30
90
0.5
87
8 0
.80
90
0.9
51
1
>>
K =
x(3
:-1:1
)
K =
0.6
28
3 0
.31
42
0
• A
rray m
athem
atics
>>
A =
[1 2
3 4
5];
>>
B =
[1 3
5 7
9];
>>
A +
B
ans =
2 5
8 1
1 1
4
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
20
>>
D =
A.*
B
D =
1 6
15
28
45
>>
D =
A./B
D =
1.0
00
0 0
.66
67
0.6
00
0 0
.57
14
0.5
55
6
>>
A.^
2
ans =
1 4
9 1
6 2
5
Arra
ys in
vo
lvin
g co
mp
lex n
um
bers
>>
a = [1
2 3
4 5
];
>>
b =
a + a*
j
b =
Co
lum
ns 1
thro
ugh
3
1.0
00
0 +
1.0
00
0i 2
.00
00
+ 2
.00
00
i 3.0
00
0 +
3.0
00
0i
Co
lum
ns 4
thro
ugh
5
4.0
00
0 +
4.0
00
0i 5
.00
00
+ 5
.00
00
i
>>
c = b
'
c =
1.0
00
0 - 1
.00
00
i
2.0
00
0 - 2
.00
00
i
3.0
00
0 - 3
.00
00
i
4.0
00
0 - 4
.00
00
i
5.0
00
0 - 5
.00
00
i
>>
c = b
.'
c =
1.0
00
0 +
1.0
00
0i
2.0
00
0 +
2.0
00
0i
3.0
00
0 +
3.0
00
0i
4.0
00
0 +
4.0
00
0i
5.0
00
0 +
5.0
00
0i
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
21
Ma
trix
op
er
atio
ns
an
d m
an
ipu
latio
ns
>>
A =
[1 2
3; 4
5 6
; 7 8
9]
A =
1 2
3
4 5
6
7 8
9
>>
A(2
,1)
ans =
4
>>
A(2
,3)
ans =
6
>>
A(3
,3) =
0
A =
1 2
3
4 5
6
7 8
0
>>
A(2
,6) =
0
A =
1 2
3 0
0 0
4 5
6 0
0 0
7 8
0 0
0 0
>>
B =
A(1
:2,1
:3)
B =
1 2
3
4 5
6
>>
C =
B(:)
C =
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
22
1
4
2
5
3
6
>>
A(:,3
) = []
A =
1 2
0 0
0
4 5
0 0
0
7 8
0 0
0
>>
sum
(A)
ans =
12
15
0 0
0
>>
Ad
ash =
A'
Ad
ash =
1 4
7
2 5
8
0 0
0
0 0
0
0 0
0
>>
sum
(Ad
ash)
ans =
3 9
15
So
me u
seful co
mm
an
ds fo
r ma
trices op
eratio
ns
zeros - Z
eros arra
y.
on
es - On
es array.
eye - Id
entity
matrix
.
rand
- Un
iform
ly d
istribu
ted ran
do
m n
um
bers.
rand
n - N
orm
ally d
istribu
ted ran
do
m n
um
bers.
linsp
ace - Lin
early sp
aced v
ector.
logsp
ace - Lo
garith
mically
spaced
vecto
r.
size - Size o
f matrix
.
length
- Len
gth
of v
ector.
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
23
nd
ims - N
um
ber o
f dim
ensio
ns.
disp
- Disp
lay m
atrix o
r text.
isemp
ty - T
rue fo
r emp
ty m
atrix.
isequ
al - Tru
e if arrays are id
entical.
isnu
meric - T
rue fo
r nu
meric arra
ys.
islogical - T
rue fo
r logical arra
y.
logical - C
on
vert n
um
eric valu
es to lo
gical.
reshap
e - Ch
ange size.
diag
- Diag
on
al matrices an
d d
iago
nals o
f matrix
.
tril - Ex
tract low
er triangu
lar part.
triu - E
xtract u
pp
er triangu
lar part.
fliplr - F
lip m
atrix in
left/righ
t directio
n.
flipu
d - F
lip m
atrix in
up
/do
wn
directio
n.
flipd
im - F
lip m
atrix alo
ng sp
ecified d
imen
sion
.
rot9
0 - R
otate m
atrix 9
0 d
egrees.
: - Reg
ularly
spaced
vecto
r and
ind
ex in
to m
atrix.
find
- Fin
d in
dices o
f no
nzero
elemen
ts.
end
- Last in
dex
.
Mo
re exam
ples:
>>
A =
[1 2
3; 4
5 6
; 7 8
9]
A =
1 2
3
4 5
6
7 8
9
>>
size(A)
ans =
3 3
>>
F =
5*
on
es(3,3
)
F =
5 5
5
5 5
5
5 5
5
>>
G =
diag
(F) - 1
G =
4
4
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
24
4
>>
A=
on
es(2,2
)
A =
1 1
1 1
>>
B =
[A+
1 A
+2
A+
3]
B =
2 2
3 3
4 4
2 2
3 3
4 4
3.2
Plottin
g in MATLAB
In th
is han
do
ut w
e will o
nly
loo
k at th
e basic p
lottin
g facilities o
ffered b
y M
AT
LA
B.
We w
ill loo
k at so
me ex
amp
les on
ho
w to
create basic 2
D g
raph
s.
>>
x =
0:0
.1*
pi:2
*p
i;
>>
y =
sin(x
);
>>
plo
t(x,y
)
>>
xlab
el('x')
>>
ylab
el('sin(x
)')
>>
title('A sam
ple g
raph
')
Th
e grap
h is sh
ow
n in
Fig
ure 3
.1. M
AT
LA
B also
allow
s the u
ser to ch
ang
e the
pro
perties o
f a grap
h u
sing th
e mo
use, th
rou
gh
its GU
I. Refer to
the d
escriptio
n in
Fig
ure 3
.1.
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
25
To
plo
t mo
re than
on
e set of d
ata in o
ne g
raph
(See fig
ure 3
.2):
>>
y2
= sin
(x-0
.25
);
>>
y3
= sin
(x-0
.5);
>>
plo
t(x,y
,x,y
2,x
,y3
)
>>
legen
d('y
', 'y2
', 'y3
')
Mo
re
on
line
sty
les
an
d c
olo
ur
s
Th
e stand
ard sy
ntax
is
Plo
t(x, y
, ‘colo
urlin
ema
rker ’), wh
ere colo
urlin
ema
rker is:
• C
olo
r strings are 'c', 'm
', 'y', 'r', 'g
', 'b', 'w
', and
'k', co
rrespo
nd
ing to
cyan
,
mag
enta, y
ellow
, red, g
reen, b
lue, w
hite, an
d b
lack resp
ectively
.
• L
ine sty
le strings are '-' fo
r solid
, '--' for d
ashed
, ':' for d
otted
, and
'-.' for d
ash-
do
t. Yo
u can
om
it the lin
e style co
mm
and
if yo
u w
ant.
• T
he m
arker ty
pes are '+
', 'o', '*
', and
'x' an
d th
e filled m
arker ty
pes 's' fo
r
squ
are, 'd' fo
r diam
on
d, '^
' for u
p trian
gle, 'v
' for d
ow
n trian
gle, '>
' for rig
ht
triangle, '<
' for left trian
gle, 'p
' for p
entag
ram, 'h
' for h
exag
ram.
Fo
r exam
ple,
>>
plo
t(x,y
,'ko
') – S
ee figu
re 3.3
>>
plo
t(x,y
,'--ko
') – S
ee figu
re 3.4
3.3
Other resources
MA
TL
AB
com
es with
plen
ty o
f oth
er usefu
l too
ls. Yo
u can
bro
wse th
eir web
site,
ww
w.m
athw
ork
s.com
, to fin
d m
ore ab
ou
t MA
TL
AB
capab
ilities. We w
ill also learn
abo
ut m
ore ad
van
ced M
AT
LA
B co
mm
and
s/too
lbo
xes as w
e go
alon
g th
is cou
rse.
In th
e nex
t han
do
ut, w
e will see h
ow
MA
TL
AB
can b
e utilized
to so
lve en
gin
eering
pro
blem
s � H
an
do
ut 4
: MA
TL
AB
pro
gra
mm
ing
Fig
ure 3
.1
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
26
Fig
ure 3
.2
Fig
ure 3
.3
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
27
Fig
ure 3
.4
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
28
4.1
Inp
ut &
Ou
tpu
t
Handout 4 : I
ntroductio
n to
MATLAB
Programming with
MATLAB
4.2
Stru
ctured
pro
gram
min
g &
Flo
w co
ntro
l
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
29
4.1
Input &
Output
Th
is section
will d
emo
nstrate th
e inp
ut an
d o
utp
ut statem
ents in
MA
TL
AB
. As w
ell
as usin
g d
irect inp
ut an
d o
utp
ut fro
m th
e user, w
e will also
see ho
w M
AT
LA
B d
eals
with
data fro
m/to
extern
al files.
Pro
mp
ting
for u
ser in
pu
t
By d
efault, th
e inp
ut fu
nctio
n retu
rns a n
um
erical valu
e. If yo
u w
ant a strin
g in
pu
t, a
secon
d p
arameter ‘s’ m
ust b
e pro
vid
ed. S
ee exam
ples b
elow
.
Te
xt O
utp
ut to
Scre
en
Th
ere are two
fun
ction
s for tex
t ou
tpu
t; nam
ely d
isp an
d fp
rintf. T
he latter allo
ws y
ou
mo
re con
trol o
ver th
e disp
layed
info
rmatio
n (m
ore d
etails on
fprin
tf later). Th
e
fun
ction
disp
, on
the o
ther h
and
, takes o
nly
on
e argu
men
t, wh
ich can
be a strin
g o
r
nu
merical v
alue (see ex
amp
les belo
w).
In ad
ditio
n to
disp
and
fprin
tf, the fo
rma
t com
man
d co
ntro
ls the d
isplay
of n
um
erical
valu
es in th
e com
man
d w
ind
ow
.
Ex
am
ples w
ith in
pu
t, disp
an
d fo
rma
t : >
> N
om
bo
rID =
inp
ut ('E
nter y
ou
r user ID
nu
mb
er')
En
ter yo
ur u
ser ID n
um
ber 3
No
mb
orID
=
3
>>
Nam
a = in
pu
t ('En
ter yo
ur n
ame', 's')
En
ter yo
ur n
ame A
HM
AD
Nam
a =
AH
MA
D
>>
disp
('I am a stu
den
t')
I am a stu
den
t
>>
disp
(['Nam
a saya ialah
', Nam
a])
Nam
a saya ialah
AH
MA
D
>>
disp
(['My ID
nu
mb
er is ', nu
m2
str(No
mb
orID
)])
My ID
nu
mb
er is 3
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
30
>>
x =
0:0
.2:1
;
>>
y =
sin (x
');
>>
disp
([x' y
])
0 0
0.2
00
0 0
.19
87
0.4
00
0 0
.38
94
0.6
00
0 0
.56
46
0.8
00
0 0
.71
74
1.0
00
0 0
.84
15
>>
form
at sho
rt
>>
disp
(pi)
3.1
41
6
>>
form
at lon
g
>>
disp
(pi)
3.1
41
59
26
53
58
97
9
>>
form
at sho
rt e
>>
disp
(pi)
3.1
41
6e+
00
0
Wo
rkin
g w
ith e
xte
rna
l da
ta file
s
Let se
e th
e sa
ve
an
d lo
ad
com
ma
nd
.
>>
x =
4;
>>
y =
4;
>>
z = x
+y;
>>
save x
yztest
>>
load
xyztest
>>
wh
o
Yo
ur v
ariables are:
x y
z
>>
x
x =
4.0
0
>>
wh
os
Nam
e Size B
ytes C
lass
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
31
x 1
x1
8 d
ou
ble arra
y
y 1
x1
8 d
ou
ble arra
y
z 1x
1 8
do
ub
le array
Gran
d to
tal is 3 elem
ents u
sing 2
4 b
ytes
> >
x =
3;
>>
y =
2;
>>
z = 5
;
>>
save x
yztx
t.txt –
ascii
Mo
re com
man
ds �
See th
e fop
en, fsca
nf, fg
etl and
fclose co
mm
and
(to b
e discu
ssed
in lectu
res)
Th
e fp
rintf fu
nctio
n
Th
ere are three w
ays in
wh
ich th
is fun
ction
can b
e used
. Th
is is best d
emo
nstrated
thro
ugh
exam
ples.
>>
fprin
tf('Hello
, My n
ame is N
orsh
ah')
Hello
, My n
ame is N
orsh
ah>
>
>>
fprin
tf('Hello
, My n
ame is N
orsh
ah\n
')
Hello
, My n
ame is N
orsh
ah
>>
>>
nam
e = 'N
orsh
ah';
>>
age =
50
;
> fp
rintf('%
s is a %d
year o
ld m
an\n
', nam
e, age)
No
rshah
is a 50
year o
ld m
an
>>
x =
2;y
=3
; z = 4
;
>>
idfile =
fop
en('O
K.tx
t', 'wt');
>>
fprin
tf(idfile,'%
d %
d %
d' , x
, y, z);
>>
fclose(id
file);
>>
Fo
rmattin
g co
des are listed
in th
e tables b
elow
:
%s
Fo
rmat as a strin
g
%d
A
s Integ
er part
%f
As flo
ating p
oin
t valu
e
%e
Flo
ating p
oin
t in scien
tific no
tation
%n
In
sert new
line
Fo
r exam
ples:
Valu
e %
8.4
f %
12
.3e
2
2.0
00
0
2.0
00
e+0
0
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
32
sqrt(2
) 1
.41
42
1
.41
4e+
00
4.2
Structured programming & Flow control
MA
TL
AB
has sev
eral com
man
ds th
at enab
le the u
ser to co
ntro
l the flo
w o
f their
pro
gram
. We w
ill see the fo
llow
ing fiv
e basic co
mm
and
s for flo
w co
ntro
l.
• fo
r loo
ps
• if lo
op
s
• w
hile lo
op
s
• sw
itch lo
op
s
• b
reak statem
ents
Fo
r-en
d lo
op
s
Th
e for lo
op
repeats a g
rou
p o
f statemen
ts prescrib
ed n
um
ber o
f times. T
he sy
ntax
is
best d
escribed
thro
ugh
som
e exam
ples:
>>
for i =
1:3
disp
(i)
end
1.0
0
2.0
0
3.0
0
>>
>>
for n
= 1
:2:5
disp
(n)
end
1.0
0
3.0
0
5.0
0
>>
>>
for n
= 1
:5
A(n
) = n
+ 1
0;
disp
(A(n
))
end
11
.00
12
.00
13
.00
14
.00
15
.00
>>
>>
for n
= 1
:5
fprin
tf('Th
e po
sition
nu
mb
er %d
is %d
\n',n
, n)
end
Th
e po
sition
nu
mb
er 1 is 1
Th
e po
sition
nu
mb
er 2 is 2
Th
e po
sition
nu
mb
er 3 is 3
Th
e po
sition
nu
mb
er 4 is 4
Th
e po
sition
nu
mb
er 5 is 5
>>
An
imp
ortan
t con
cept is th
e NE
ST
ED
LO
OP
��� � B
asica
lly m
ean
s ‘ A L
oo
p in
an
oth
er loo
p’. U
sua
lly th
e mo
st com
mo
n a
pp
licatio
n is w
hen
yo
u a
re dea
ling
with
ma
trices. As u
sua
l – so
me ex
am
ples fo
llow
! >
> fo
r i = 1
:3
for j =
1:4
A(i,j) =
i + j;
end
end
>>
A
No
tice the in
den
tatio
n o
f the
statem
ents. In
pro
gram
min
g, y
ou
do
this to
mak
e yo
ur p
rogram
s
easier to ch
eck an
d so
that y
ou
can
easily see v
ariou
s nested
loo
ps.
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
33
A =
2.0
0 3
.00
4.0
0 5
.00
3.0
0 4
.00
5.0
0 6
.00
4.0
0 5
.00
6.0
0 7
.00
>>
If-en
d S
tate
me
nt
Th
e if statemen
t evalu
ates a logical ex
pressio
n an
d ex
ecutes a g
rou
p o
f statemen
ts
wh
en th
e exp
ression
is true. T
he o
ptio
nal elseif an
d else k
eyw
ord
s also en
able
execu
tion
of altern
ative statem
ents. A
n en
d k
eyw
ord
termin
ates the last g
rou
p o
f
statemen
ts.
Th
e gen
eral form
of th
e IF statem
ent is
So
me ex
am
ples:
>>
A =
5;
>>
B =
10
;
>>
if A <
B
disp
(A)
else
disp
(B)
end
5.0
0
>>
>>
for i =
1:3
for j =
1:3
if (i==
j) d
isp(A
(i,j))
end
end
end
1.0
0
5.0
0
9.0
0
>>
IF ex
pressio
n
statemen
ts
EL
SE
IF ex
pressio
n
statemen
ts
EL
SE
statemen
ts
EN
D
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223) : P
ART I
*N
ote
: Re
latio
na
l op
era
tors in
MA
TLA
B.
<
Is less than
<=
Is less th
an o
r equ
al to
>
Is greater th
an
>=
Is g
reater than
or eq
ual to
==
Is eq
ual to
~=
Is n
ot eq
ual to
Wh
ile-e
nd
loo
p
Th
e wh
ile statemen
t execu
te a gro
up
of statem
ents rep
etitively
un
der
con
trol o
f a logical co
nd
ition
. Fo
r exam
ples:
>>
i = 1
;
>>
wh
ile i < 1
0
disp
(i)
i = i+
3;
end
1.0
0
4.0
0
7.0
0
>>
Sw
itch-e
nd
loo
p
Th
e switch
structu
re is an altern
ative to
a lon
g seq
uen
ce of elseif b
lock
s in an
if-end
loo
p. T
he sy
ntax
can b
e un
dersto
od
usin
g th
e follo
win
g ex
amp
les:
>>
x =
5;
>>
switch
sign
(x)
case -1
disp
('x is n
egativ
e');
case 0
disp
('x is zero
');
case 1
disp
('x is p
ositiv
e');
oth
erwise
disp
('test fails')
end
x is p
ositiv
e
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
35
>>
Brea
k statemen
t
Th
is com
man
d (an
alogo
us to
GO
TO
fun
ction
in v
ariou
s old
er com
pu
ter cod
es) is
used
as an escap
e mech
anism
from
a wh
ile-end
loo
p (u
sually
in IT
ER
AT
ION
S
PR
OC
ED
UR
E). F
or ex
amp
le:, in th
e follo
win
g lo
op
:
Iter = 0
;
Iteration
cou
nter
Max
it =
S
et the n
um
ber o
f iteration
s
To
lerance =
…
Set th
e toleran
ce
xo
ld =
….
In
itial valu
e
x =
……
Initial v
alue
wh
ile Iter < M
axit
xo
ld =
x;
Sav
e for co
nv
ergen
ce check
x =
…..
D
o o
peratio
n o
n x
Iter = Iter +
1;
If ab
s ((x-x
old
)/xo
ld) <
To
leran
ce
brea
k;
end
end
Test fo
r con
verg
ence. If th
is
con
ditio
n is m
et, the b
reak
statemen
t will b
ring y
ou
ou
t of
the w
hile-en
d lo
op
– an
d g
o to
the n
ext statem
ent.
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
36
5.1
M-files : scrip
t m-file &
fun
ction
m-file
Handout 5
: Introductio
n to
MATLAB
M-file
s & Samples of M
ATLAB applicatio
ns
5.2
An
alysis o
f samp
le MA
TL
AB
pro
gram
s
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
37
5.1
M-file
s
So
far, yo
u h
ave learn
t ho
w to
enter co
mm
and
s and
valu
es in M
AT
LA
B; assig
n
variab
les/vecto
rs/matrices, carry
ou
t math
ematical o
peratio
ns o
n th
em an
d p
lot th
e
results. Y
ou
hav
e also learn
t ho
w to
app
ly M
AT
LA
B co
mm
and
s to co
ntro
l yo
ur
pro
gram
.
No
tice that in
prev
iou
s han
do
uts, w
e ask M
AT
LA
B to
do
op
eration
s for u
s by sim
ply
typ
ing th
e com
man
ds in
to th
e MA
TL
AB
screen v
ia the co
mm
and
pro
mp
t >>
, let
MA
TL
AB
do
its job
and
giv
e us th
e answ
er. Th
at is basically
trivial if y
ou
hav
e on
ly
single ap
plicatio
ns th
at yo
u w
ant M
AT
LA
B to
run
at on
e time. B
ut w
hat if y
ou
wan
t
MA
TL
AB
to carry
ou
t lots o
f fun
ction
s con
secutiv
ely? In
that case, y
ou
mu
st utilize
the M
-files. Basically
an m
-file is just a co
llection
of co
mm
and
s that y
ou
store in
a
plain
text file w
ith an
exten
sion
*.m
. Yo
u can
hav
e script m
-files and
fun
ction
m-
files. M-files can
be ex
ecuted
by ty
pin
g th
eir nam
es at the co
mm
and
pro
mp
t.
Sc
ript m
-files
Scrip
t m-files h
ave n
o in
pu
t or o
utp
ut p
arameters. T
hey
are usefu
l wh
en y
ou
wan
t to
store so
me co
nstan
t param
eters that y
ou
wan
t to u
se for m
any tim
es in y
ou
r pro
gram
.
Fo
r exam
ple, lets create a file to
store so
me co
nstan
ts. To
create an M
-file, in
MA
TL
AB
, click F
ile�
New
� m
-file. Typ
e the co
nstan
ts as belo
w, an
d sav
e the file
as myco
nstan
t.m fo
r exam
ple.
To
run
the file, ty
pe:
>>
myco
nstan
t
>>
grav
ity
grav
ity =
9.8
10
0
>>
avag
adro
avag
adro
=
% A
script m
file to sto
re con
stants th
at I will u
se
% later in
my p
rogram
grav
ity =
9.8
1; %
grav
ity acceleratio
n, m
/s2
avag
adro
= 6
.02
4e2
3; %
nu
mb
er of m
olecu
les in a m
ole
Rgas =
83
15
; % U
niv
ersal gas co
nstan
t J/km
ol/K
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
38
6.0
24
0e+
02
3
>>
Rgas
Rgas =
83
15
>>
Fu
nctio
n m
-files
Th
ese are mo
re usefu
l than
script m
-files in th
e sense th
at they
allow
yo
u to
com
mu
nicate w
ith th
e com
man
d w
ind
ow
and
oth
er fun
ction
s via a p
redefin
ed list o
f
inp
ut an
d o
utp
ut p
arameters. A
s yo
u sh
all see, fun
ction
m-files are th
e back
bo
ne o
f
MA
TL
AB
pro
gram
min
g.
Sim
ilarly, y
ou
can create a fu
nctio
n m
-file by click
ing F
ile�
New
� m
-File in
MA
TL
AB
.
Th
e first line o
f a fun
ction
m-file h
as the fo
rm,
wh
ere ou
tpu
tparam
eterlist and
inp
utp
arameterlist are co
mm
a-separated
list of
variab
les used
to p
ass data in
to an
d o
ut o
f the fu
nctio
n. L
et’s see exam
ples sh
ow
ing
differen
t usag
e of th
ese inp
ut an
d o
utp
ut p
arameters.
>>
m =
3; n
= 2
;
>>
two
sum
(m,n
)
ans =
5
>>
two
sum
(2,3
)
ans =
5
fun
ction
[ou
tpu
tparam
eterlist] = fu
nctio
nn
ame [ in
pu
tparam
eterlist]
% to
com
pu
te sum
of tw
o v
alues
fun
ction
a = tw
osu
m(x
,y)
a = x
+y;
two
sum
.m
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
39
>>
k =
[ 1 2
3];
>>
l = [ 1
2 3
];
>>
two
sum
(k,l)
ans =
2 4
6
>>
>>
x =
4; y
= 5
;
>>
sho
wlo
cal(1,2
)
1
2
ans =
3
>>
x
x =
4
>>
y
y =
5
>>
% to
com
pu
te sum
of tw
o v
alues an
d sh
ow
% th
e pro
perty
of lo
cal variab
les
fun
ction
a = sh
ow
local(x
,y)
disp
(x); d
isp(y
);
a = x
+y;
sho
wlo
cal.m
Dem
on
strating th
e imp
ortan
t con
cept o
f
LO
CA
L v
aria
bles
% sh
ow
ing tw
o o
utp
ut p
arameters
fun
ction
[a b] =
two
sum
2(x
,y)
a = x
+y;
b =
2*
x+
2*
y;
two
sum
2.m
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
40
>>
two
sum
2(1
,3)
ans =
4
>>
[g h
] = tw
osu
m2
(1, 3
)
g =
4
h =
8
>>
No
te: Th
ere are oth
er com
mo
nly
used
MA
TL
AB
com
man
ds to
evalu
ate fun
ction
valu
es � eva
l and
inlin
e. See ex
amp
les belo
w:
>>
x =
[1 2
3 4
5];
>>
fx =
1 +
x.^
2;
>>
eva
l ('fx',x
)
fx =
2 5
10
17
26
>>
fun
ction
baru
= in
line ('sin
(x)*
cos(x
)')
fun
ction
baru
=
Inlin
e fun
ction
:
fun
ction
baru
(x) =
sin(x
)*co
s(x)
>>
x =
1.4
;
>>
fun
ction
baru
(x)
ans =
0.1
67
5
>>
sin(x
)*co
s(x)
ans =
0.1
67
5
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
41
Handout 6
: Errors in Numerical M
ethods
CHAPTER 3 : A
pproximatio
ns and
Round-off E
rrors
3.3
Erro
r Defin
ition
s
4.1
Th
e Tay
lor S
eries 4
.2 E
rror p
rop
agatio
n
CHAPTER 4 : T
runcatio
n errors and
the Taylor series
4.3
To
tal nu
merical erro
r
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
42
CHAPTER 3 : Approximatio
ns and Round-off
Errors
Fro
m th
e exam
ple o
f the fallin
g p
arachu
tist, it was
seen th
at
nu
merical
meth
od
s are
no
t alw
ays
accurate.
Ho
wev
er, w
here
nu
merical
solu
tion
s are
need
ed,
we
sho
uld
b
e aw
are o
f th
e
mag
nitu
de o
f the erro
rs.
Th
ree main
typ
e of erro
rs:
1. ro
un
d-o
ff errors –
inaccu
rate nu
mb
er represen
tation
2. tru
ncatio
n erro
rs – u
se app
rox
imatio
ns in
form
ulas
3. h
um
an erro
rs – b
lun
ders, fo
rmu
lation
or m
od
el errors an
d
un
certainty
3.3
Erro
r Defin
ition
s T
rue erro
r,
=t
Etru
e valu
e-app
rox
imatio
n
To
qu
antify
the m
agn
itud
e, this eq
uatio
n can
be n
orm
alized. T
he
true p
ercent relativ
e error,
%100
valu
etru
e
error
true
t=
ε
Ho
wev
er in real-w
orld
app
lication
s, we w
ill ob
vio
usly
no
t
kn
ow
the tru
e answ
er a prio
ri. Th
us altern
atively
, the b
est
app
rox
imatio
ns can
be u
sed,
%1
00
ion
ap
pro
xima
t error
ea
pp
roxim
at
a=
ε
Certain
nu
merical m
etho
ds u
se an iterativ
e app
roach
to co
mp
ute
answ
ers. Th
us, th
e percen
t relative erro
r,
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
43
%100
ion
appro
ximat
curren
t
ion
appro
ximat
previo
us
ion
appro
ximat
curren
ta
−=
ε
Often
, w
hen
p
erform
ing
co
mp
utatio
ns,
we
are in
terested
in
wh
ether th
e percen
t abso
lute v
alue is lo
wer th
an th
e presp
ecified
percen
t toleran
ce, s
ε
sa
εε
<
It is also co
nv
enien
t to relate th
ese errors to
the n
um
ber o
f
sign
icant
figu
res in
th
e ap
pro
xim
ation
. It
can
be
sho
wn
(Scarb
oro
ug
h, 1
966
) that if th
e follo
win
g criterio
n is m
et, the
result is co
rrect to at least n
sign
ificant fig
ures
)%
10
5.0(
2n
s
−×
=ε
Ex
amp
le 3.2
pg
56
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
44
CHAPTER 4 : T
runcatio
n errors and th
e
Taylor series
Tru
ncatio
n
errors
–
du
e to
th
e u
se o
f ap
pro
xim
ate
math
em
atical pro
cedu
res in p
lace of ex
act on
es. Fo
r exam
ple,
in
the
case o
f fallin
g
parach
utist
pro
blem
, w
e u
sed
the
app
rox
imatio
n:
()
()
1
1
ii
ii
vt
vt
dv
v
dt
tt
t
+
+
−∆
≅=
∆−
and
this led
to erro
r in th
e final resu
lt. To
un
derstan
d h
ow
this
occu
red,
we
turn
to
m
athem
atical fo
rmu
lation
th
at is
used
wid
ely
to
exp
ress fu
nctio
ns
in
an
app
rox
imate
fashio
n
–
‘Ta
ylo
r Series’
4.1
Th
e Ta
ylo
r Series
An
y sm
oo
th fu
nctio
n can
be ap
pro
xim
ated as a p
oly
no
mial.
Tay
lor series p
rov
ides a m
eans to
pred
ict the v
alue o
f a fun
ction
at on
e po
int in
terms o
f the fu
nctio
n v
alue an
d its d
erivativ
es at
ano
ther p
oin
t.
• (
)1(
)i
if
xf
x+
≅
- zero o
rder a
pp
rox
ima
tion
, on
ly
tru
e if 1
ix
+ a
nd
i
x a
re very
close to
each
oth
er.
• (
)(
)1
1(
)(
)i
ii
ii
fx
fx
fx
xx
++
′≅
+−
- first ord
er
ap
pro
xim
atio
n, in
form
of a
straig
ht lin
e
•
n-th
ord
er
(
)2
1(
)(
)(
)2
!! n
n
ii
in
ff
fx
fx
fx
hh
hR
n+
′′′
≅+
++
++
K
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
45
w
here
1i
ih
xx
+=
−
()
()
1
1
1
n
n
n
fR
hn
ξ+
+=
+
Ex
am
ple 4
.1
Use 0
to 4
th ord
er Tay
lor series ex
pan
sion
s to ap
pro
xim
ate the
fun
ction
: 4
32
()
0.1
0.1
50
.50
.25
1.2
fx
xx
xx
=−
−−
−+
from
0
ix
= w
ith
1h
= (i.e. p
redict its v
alue at
11
ix
+=
).
Tru
e valu
e, (1)
0.2
f=
So
lutio
n:
for n
=0
, zero o
rder T
aylo
r Series ap
pro
xim
ation
giv
es
1(
)1
.2i
fx
+�
at 1
x=
, trun
cation
error,
1.2
0.2
1.0
tE
=−
=−
n=
1, first o
rder ap
pro
xim
ation
:
calc -(0
)0.2
5f ′
=−
, thu
s 1
()
1.2
0.2
5,
(1)0.9
5i
fx
hf
+−
=�
Et =
0.2
-0.9
5 =
-0.7
5
n=
2, seco
nd
ord
er app
rox
imatio
n:
(1)
(1)
()
(1)!
nn
n
fR
hn
ξ+
+=
+
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
46
?
n=
3, seco
nd
ord
er app
rox
imatio
n:
? n=
4, seco
nd
ord
er app
rox
imatio
n:
?
Su
mm
ary:
• If w
e take o
nly
the first term
of th
e series , and
neg
lect all
oth
er terms, th
en it is called
as 'zero o
rder ex
pan
sion
. Th
e
fun
ction
is no
w tak
en as a co
nstan
t since f(x
i+1 )=
f(xi ).
• If w
e take th
e first two
terms , th
en it is first o
rder
exp
ansio
n. T
his is w
hat w
e used
in th
e parach
utist
pro
blem
.
• U
sually
, add
ing
mo
re terms to
the series w
ill on
ly im
pro
ve
the series m
argin
ally.
• A
t som
e po
int, th
e imp
rov
emen
t beco
mes in
sign
ificant an
d
then
w
e can
sto
p ad
din
g term
s. If
the
fun
ction
w
e are
dealin
g w
ith is a p
oly
no
mial, th
en th
e Tay
lor series w
ill
hav
e finite n
um
ber o
f terms b
ut fo
r mo
st of th
e fun
ction
s,
we h
ave an
infin
ite series. Wh
en w
e cho
p o
ff the series
after a few term
s, then
we h
ave a 'tru
ncatio
n erro
r’
A
Dile
mm
a: T
he
error
is g
iven
b
y
the
rem
aind
er term
R
n .
Ho
wev
er, the term
ξ is n
ot k
no
wn
, hen
ce we can
no
t get th
e
correct
valu
e o
f R
n .
Also
w
e sh
ou
ld
kn
ow
all
the
n+
1
deriv
atives o
f f(n), w
hich
mean
s, we sh
ou
ld k
no
w f(n
). If we
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
47
kn
ow
f(n), th
en, th
ere is no
need
to p
erform
the T
aylo
r Series
analy
sis. B
ut,
we
still u
se th
e series
and
g
et th
e im
po
rtant
messag
e that
Rn =
O(h
n+
1)
O(h
n+
1) mean
s "of th
e ord
er of h
n+
1 i.e. th
e error is o
f the o
rder
of h
n+
1. If h is v
ery sm
all, then
error is also
very
small. It also
sho
ws th
at if :
- erro
r is O(h
) halv
ing
the h
will h
alve th
e error.
- erro
r is O(h
2) halv
ing
the h
will q
uarter th
e error.
Th
is is a very
usefu
l info
rmatio
n fo
r nu
merical an
alysis.
See
exam
ple
4.3
fo
r th
e effect
of
no
nlin
earity an
d step
size
on
th
e T
aylo
r
Series ap
pro
xim
ation
.
4.2
Erro
r Pro
pa
ga
tion
Pu
rpo
se is to stu
dy
how
errors in
nu
mb
er
pro
pag
ates th
rou
gh
m
athem
atical
fun
ction
s. E.g
. mu
ltiply
two
nu
mb
ers that
hav
e erro
rs, w
ou
ld
like
to
estimate
the
error in
the p
rod
uct.
4
.2.1
Fu
nctio
ns o
f a single v
ariable
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
48
Giv
en a fu
nctio
n
()
fx
. Assu
me th
at x % is an
app
rox
imatio
n o
f
x. W
e wo
uld
like to
estimate th
e discrep
ancy
()
()
()
fx
fx
fx
∆=
−%
%
bu
t since x
is un
kn
ow
n
()
fx
is un
kn
ow
n. T
hu
s if xx
≈%
can
use T
aylo
r series,
()
()
()(
)(
)()
2..
2
fx
fx
fx
fx
xx
xx
′′′
=+
−+
−+
%%
%%
%
dro
p 2
nd o
rder an
d h
igh
er term, an
d rearran
ge
()
()
()(
)f
xf
xf
xx
x′
−≅
−%
%%
or
()
()(
)f
xf
xx
x′
∆≅
−%
%%
eq
n (4
.25
)
F
igu
re 4.7
4.2
.2 F
un
ction
s of m
ore th
an
on
e va
riab
le If a fu
nctio
n o
f two
ind
epen
den
t variab
les uan
d v
, the T
aylo
r
series beco
mes
()
()
()
()
()
()(
)(
)
11
11
22
22
2
11
11
22
,,
..
12
..2
!
ii
ii
ii
ii
ii
ii
ii
ii
ff
fu
vf
uv
uu
vv
uv
ff
fu
uu
uv
vv
vu
uv
v
++
++
++
++
∂∂
=+
−+
−+
∂∂
∂∂
∂−
+−
−+
−+
∂∂
∂∂
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
49
wh
ere all partial d
erivativ
es are evalu
ated at th
e base p
oin
t i.
Dro
pp
ed h
igh
er ord
er terms, rearran
ged
()
,f
ff
uv
uv
uv
∂∂
∆=
∆+
∆∂
∂%%
%%
for n
ind
epen
den
t variab
les,
()
12
12
12
,,..
..n
n
n
ff
ff
xx
xx
xx
xx
x
∂∂
∂∆
≅∆
+∆
++
∆∂
∂∂
%%
%%
%%
eqn
(4.2
7)
Ex
am
ple 4
.6 E
rror p
rop
agatio
n in
a mu
ltivariab
le fun
ction
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
50
4.3
To
tal n
um
erical erro
r
- su
mm
ation
of tru
ncatio
n an
d ro
un
d-o
ff errors
- to
min
imiz
e rou
nd
-off erro
rs - increa
se the n
um
ber o
f
sign
ificant fig
ures o
f the co
mp
uters
- to
decrease tru
ncatio
n erro
r – d
ecrease step
size ; bu
t
increase co
mp
utatio
ns an
d in
crease rou
nd
-off erro
rs
Num
eric
al m
eth
ods fo
r engin
eers
(COEB223): P
ART I
Dr. H
anim
Salle
h, M
echanical D
epartm
ent, U
NIT
EN ©
2007/2
008
51
Dile
mm
a: d
ecrease the step
size to
decrease
trun
cation
error
bu
t at the sa
me tim
e increase
rou
nd
-off erro
rs!