Upload
rajahazmi-bin-rajaaznoor
View
240
Download
0
Embed Size (px)
Citation preview
8/11/2019 Matlab Assignment Answers
1/25
EAB 2113 NUMERICAL METHOD
Question 1
Develop an M-file to implement the i!e"tion metho#$ U!in% thi! p&o%&am !olve the
follo'in% p&olem$
The velo"it( of fallin% pa&a"h)ti!t i! %iven a!
)1()( )/( tmc
ec
gmtv =
$
*he&e
)(tv
+ velo"it( of pa&a"h)ti!t +
sm /40
,
g+ %&avitational "on!tant +
2/8.9 sm
,
m+ the ma!! of the pa&a"h)ti!t +
kg1.68
$
in# the #&a% "oeffi"ient, cat the time
10=t
!e"on#! )!in% the initial &a".et
of the &oot a! /13, 10 an# ite&ate )ntil
001.0a$
1
% Data obtain from the questionf=inline('((9.81*68.1)/x)*(1-ex(-(x/68.1)*1!))-"!'#'x')$xl=1$xu=16$
es=!.!!1$%&omutationxr=xl$ %initiationhile(1) %sin&e e ont no ho man interation ill tae la&e
xrol=xr$ %ee the re+ious xr xr=(xl,xu)/$ %formula for bise&tion metho
ifxr=!# ea=abs((xr-xrol)/xr)*1!!$ en%&al&ulate ea if xr not realanser
test=f(xl)*f(xr)$
iftest! xu=xr$ elseiftest0! xl=xr$ elseea=!$ en
ifea=es# brea#en%en loo if the situation is satisfieenformat lon$is('the root for this equation is 2 ')is(xr)
8/11/2019 Matlab Assignment Answers
2/25
EAB 2113 NUMERICAL METHOD
2
8/11/2019 Matlab Assignment Answers
3/25
EAB 2113 NUMERICAL METHOD
Question 2
Develop an M-file to implement the fal!e po!ition metho#$ !olve the follo'in%p&olem$
The velo"it( of fallin% pa&a"h)ti!t i! %iven a!
)1()( )/( tmc
ec
gmtv =
$
*he&e
)(tv
+ velo"it( of pa&a"h)ti!t +
sm /40
,
g+ %&avitational "on!tant +
2/8.9 sm
,
3
8/11/2019 Matlab Assignment Answers
4/25
EAB 2113 NUMERICAL METHOD
m+ the ma!! of the pa&a"h)ti!t +
kg1.68
$
in# the #&a% "oeffi"ient, cat the time
10=t
!e"on#! )!in% the initial &a".et
of the &oot a! /13, 10 an# ite&ate )ntil
001.0a$
f=inline('((9.81*68.1)/x)*(1-ex (-(x/68.1)*1!))-"!'#'x')$xl=1$xu=16$es=!.!!1$
xr=xlhile(1)
xrol=xr$ xr=xu-(((f(xu)*(xl-xu))/f(xl)-f(xu)))$
ifxr=!# ea=abs((xr-xrol)/xr)*1!!$ en test=f(xl)*f(xr)$ iftest! xu=xr$ elseiftest0! xl=xr$ elsetest0! ea=!$ enifea=es# brea# enenformat lon$is ('the root for this equation is 2 ')is(xr)
8/11/2019 Matlab Assignment Answers
5/25
EAB 2113 NUMERICAL METHOD
4
8/11/2019 Matlab Assignment Answers
6/25
EAB 2113 NUMERICAL METHOD
Question 3
Lo"ate the &oot of
xxxf = )sin(2)(
5a6 U!in% the MATLAB f)n"tion fzero'ith an initial %)e!! of
20 =x
$
56 U!in% Ne'ton-Raph!on metho# ( '&itin% a f)n"tion M-file$ U!e an
initial %)e!! of
5.00 =x
an# ite&ate )ntil
001.0a$
a6
0
8/11/2019 Matlab Assignment Answers
7/25
EAB 2113 NUMERICAL METHOD
7
fun&tionroot=nerah1(f#f#xr#es)%f=inline('*sin(sqrt(x))-x')%f=inline('-&os(sqrt(x))/sqrt(x)-1')hile(1) xr3ol=xr$ xr=xr-(f(xr)/f(xr))$ ifxr=!#ea=abs((xr-xr3ol)/xr)*1!!$ en
ifea=es#brea#enenfrintf('4n4nthe root for this question is %.6f4n'#xr)$
8/11/2019 Matlab Assignment Answers
8/25
EAB 2113 NUMERICAL METHOD
Question 4
Develop an M-file to implement the mo#ifie# !e"ant metho#$ U!in% thi!
p&o%&am #ete&mine the loe!t po!itive &oot of
1)sin(8)( = xexxf
'ith an
initial %)e!! of
3.00 =x
an#
01.0=
$ Ite&ate )ntil
%000001.0=a$
8
f=inline('8*sin(x)*ex(-x)-1'#'x')5a=1$xo=!.$frintf('he initial uess is %.f'#xo)hile(5a0!.!!!!!1) xol=xo$ xo=xo-(!.!1*xo*f(xo))/(f(xo,!.!1,xo)-f(xo))$
5a=((abs(xo-xol))/xo)*1!!$enfrintf('4nhe loest ositi+e root of fun&tion is %1!.6f ith aroximateerror %7."f'#xo#5a)
8/11/2019 Matlab Assignment Answers
9/25
EAB 2113 NUMERICAL METHOD
Question 5
in# the !ol)tion of the follo'in% !et of linea& al%e&ai" e9)ation!
:
8/11/2019 Matlab Assignment Answers
10/25
EAB 2113 NUMERICAL METHOD
2332
1433
132
=++
=++=++
zyx
zyx
zyx
a6 U!in% the left #ivi!ion
6 U!in% ;a)!!ian elimination
1
8/11/2019 Matlab Assignment Answers
11/25
EAB 2113 NUMERICAL METHOD
"6 U!in% LU #e"ompo!ition
11
8/11/2019 Matlab Assignment Answers
12/25
EAB 2113 NUMERICAL METHOD
Question 6
Develop a f)n"tion M-file T&i#ia%$m to !olve the follo'in% t&i#ia%onal !(!tem'ith the Thoma! al%o&ithm$
=
n
n
n
n
nn
nnn
r
r
r
r
r
x
x
x
x
x
fe
gfe
gfe
gfe
gf
1
3
2
1
1
3
2
1
111
333
222
11
.
.
.
.
.
.
...
...
...
Thoma! Al%o&ithm=
5i6 De"ompo!ition=
12
8/11/2019 Matlab Assignment Answers
13/25
EAB 2113 NUMERICAL METHOD
1
=k
kk
f
ee
an#
1. = kkkk geff, 'he&e
nk ,,4,3,2 =$
5ii6 o&'a !)!tit)tion=
1. = kkkk rerr, 'he&e
nk ,,4,3,2 =$
5iii6 Ba". !)!tit)tion=
n
nn
f
rx =
an#
k
kkkk
f
xgrx
).( 1+=
, 'he&e
1,2,,2,1 = nnk$
U!in% (o)& p&o%&am, !olve the follo'in% t&i#ia%onal !(!tem$
01475.2020875.0
020875.001475.2020875.0
020875.001475.2020875.0
020875.001475.2
4
3
2
1
x
x
x
x
+
0875.2
0
0
175.4
13
fun&tionx=ri(e#f##r)%e=inut('e= ')$%f=inut('f= ')$%=inut('= ')$%r=inut('r= ')$e=! -!.!!87 -!.!!87 -!.!!87:$f=.!1"7 .!1"7 .!1"7 .!1"7:$=-!.!!87 -!.!!87 -!.!!87 !:$
r=".17 ! ! .!87:$n=lenth(f)$for=2n fa&tor=e()/f(-1)$ f()=f()-fa&tor*(-1)$
r()=r()-fa&tor*r(-1)$frintf('fa&tor=%."f4t f()=%."f4t r()=%."f4n'#fa&tor#f()#r())enx(n)=r(n)/f(n)$for=n-12-121 x()=(r()-()*x(,1))/f()$ frintf('x()=%."f4n'#x())en
8/11/2019 Matlab Assignment Answers
14/25
EAB 2113 NUMERICAL METHOD
1
8/11/2019 Matlab Assignment Answers
15/25
EAB 2113 NUMERICAL METHOD
Question 7
Q7. Develop a MATLA s!"ip# $ile #o %e#e"&ine #'e sol#ion o$ #'e $olloin* s+s#e& o$
linea" ea#ions sin* #'e -assei%el i#e"a#ion &e#'o% + pe"$o"&in* $i"s# seveni#e"a#ions.
2110232
5.1241123
143282
5.542329
4321
4321
4321
4321
=+++
=++
=++
=++
xxxx
xxxx
xxxx
xxxx
14
i=1$x1=!$x=!$x=!$x"=!$is(' i x1 x x x"')frintf('%.!f %8.f %8.f %8.f %8.f4n'#i#x1#x#x#x")fori=28 x1=(".-(-*x,*x,*x"))/9$ x=(-1"-(*x1-*x,*x"))/8$ x=(1.-(-*x1,*x-"*x"))/11$ x"=(-1-(-*x1,*x,*x))/1!$ frintf('%.!f %8.f %8.f %8.f %8.f4n'#i#x1#x#x#x")en
8/11/2019 Matlab Assignment Answers
16/25
EAB 2113 NUMERICAL METHOD
Question 8
. Develop a s!"ip# M$ile #o es#i&a#e
)75.2(f
sin* La*"an*e in#e"pola#in* pol+no&ials o$
o"%e" 1, 2 an% 3 $o" #'e $olloin* %a#a.
x 0 1 2 3 4 5
f(x)
0 0.5 0.8 0.9 0.941176 0.961538
o& ea"h e!timate fin# the t&)e pe&"ent &elative e&&o& if the t&( f)n"tion i!
%iven (
)1()(
2
2
x
xxf
+=
$
10
fun&tionfint=;arane=12n if>=i ;(i)=;(i)*(xint-x(>))/(x(i)-x(>))$ en enenf=(xint?)/(1,xint?)$et=abs((f-sum(.*;))/f)/1!!$frintf('4nx=%.8f4n'#sum(.*;))$frintf('5rror=%6f%%4n'#et)$
8/11/2019 Matlab Assignment Answers
17/25
EAB 2113 NUMERICAL METHOD
Question 9
$ The fo&"e on a !ailoat ma!t "an e &ep&e!ente# ( the follo'in% f)n"tion=
+=
HHz dze
z
zF
0
/5.2
7200
'he&e=z
the elevation aove the #e". an#
=Hthe hei%ht of the ma!t$
Comp)te F fo& the "a!e 'he&e
30=H
)!in%
17
8/11/2019 Matlab Assignment Answers
18/25
EAB 2113 NUMERICAL METHOD
5i6 the M-file fo& T&ape>oi#al &)le 'ith the !tep !i>e
1.0=h
$
the MATLABtrapzf)n"tion$
Question 10
18
fun&tione+aluate = trae@oial1(f#A#h)%f=inline('!!*(@/(7,@))*ex(-*@/!)')%(for the tra@ fun&tion)=!!*(@./(7,@)).*ex(-.*@./!)a=!$hile(1)n=(A-a)/h$t!=f(a)$t1=!$fori=12n-1 t1=t1,f(a,h*i)$ent=f(A)$+alue=h/*(t!,*t1,t)$breaenfrintf('he rae@oial
8/11/2019 Matlab Assignment Answers
19/25
EAB 2113 NUMERICAL METHOD
Develop an M-file to implement ?imp!on@! 13 &)le$ U!in% (o)& p&o%&am !olvethe follo'in% p&olem$
The velo"it( of fallin% pa&a"h)ti!t i! %iven a!
)1()( )/( tmcec
gmtv =
$
*he&e
)(tv
+ velo"it( of pa&a"h)ti!t,
g+ %&avitational "on!tant +
2/8.9 sm
,
m+ ma!! of the pa&a"h)ti!t +
kg45
,
c+ the #&a% "oeffi"ient +
skg/5.32
$
If the #i!tan"e, #, t&avele# ( the pa&a"h)ti!t i! %iven (
=6
0
)( dttvd
,
fin# the #i!tan"e )!in% ?imp!on@! 13 &)le fo& the !e%ment! 1
8/11/2019 Matlab Assignment Answers
20/25
EAB 2113 NUMERICAL METHOD
2
8/11/2019 Matlab Assignment Answers
21/25
i5t6E
EAB 2113 NUMERICAL METHOD
Question 11
Develop an M-file fo& E)le&@! metho# to !olve a fi&!t oe& oina&( #iffe&entiale9)ation 5ODE6$
The ")&&ent a&o)n# the "i&")it at time
t
i! %ove&ne# ( the follo'in%
#iffe&ential e9)ation
tei
dt
di 2323 +=
,
2)0( =i$
U!in% (o)& p&o%&am, !olve the aove initial val)e p&olem ove& the inte&val
f&om
0=t
to 2 'ith the !tep !i>e
1.0=h
$
21
8/11/2019 Matlab Assignment Answers
22/25
EAB 2113 NUMERICAL METHOD
22
fun&tiont#i:= 5uloe(it#tsan#i!#h)%inut2%it= name of the fun&tion f(t#i)%tsan= ti#tf: here ti an tf= initial an final +alus of ineenent%+ariable%!= initial +alue of the eenent +ariable%h=ste si@e%outut2%t#i: here t= +e&tor of the ineenent +ariable% i= +e&tor of the solution for the eenent +ariabletx=tsan(1)$t=tsan()$t=(tx2h2t)'$n=lenth(t)$
i=i!*ones(n#1)$forx=12n-1 i(x,1)=i(x),fe+al(it#t(x)#i(x))*h$en
8/11/2019 Matlab Assignment Answers
23/25
EAB 2113 NUMERICAL METHOD
23
8/11/2019 Matlab Assignment Answers
24/25
EAB 2113 NUMERICAL METHOD
Question 12
Develop an M-file fo& o)&th-Oe& R)n%e-)tta metho# to !olve a fi&!t oe&
oina&( #iffe&ential e9)ation 5ODE6$
U!in% (o)& p&o%&am !olve the follo'in% initial val)e p&olem ove& the inte&val
f&om
0=x
to 2 'ith the !tep !i>e
2.0=h
$
8.0)0(,2 =+= yyxdx
dy
$
2
fun&tionx#:=r"!e(x#xsan#!#h)xi=xsan(1)$
xf=xsan()$x=(xi2h2xf)'$n=lenth(x)$=!*ones(n#1)$fori=12n-1 1=fe+al(x#x(i)#(i))$ =fe+al(x#x(i),(1/)*h#(i),(1/)*1*h)$ =fe+al(x#x(i),(1/)*h#(i),(1/)**h)$ "=fe+al(x#x(i),h#(i),*h)$ (i,1)=(i),(1/6)*(1,*,*,")*h$en
8/11/2019 Matlab Assignment Answers
25/25
EAB 2113 NUMERICAL METHOD
24