numerical methods for use in root discovery
TRANSCRIPT
-
8/8/2019 Numerical Methods for Use in Root Discovery
1/15
Numeri
DanCook
Abstract
Theneed
engineeri
engineeri
bisection,
Introduc
Functions
physical
approxim
bedeter
Theconc
Witte wr
published
functions
Today m
mathema
Physical
Solving t
economic
engineeri
Takethe
calMetho
:
tofind
ar
ng. In this
ng, andmor
modifiedfal
tion:
areoneoft
phenomeno
atemanyva
inedfrom
a
ptofaroot
itten in 155
La geomet
[7].
ny methods
ticaltoolsin
Analysis:
e roots of
s, engineeri
ng.
ollowingdia
sforusei
otof
afun
eport, roots
e specifically
sepositive,fi
hemostbasi
. Once a f
uesoverthe
functionis
it
datesbackt
7, the equal
ie in 1637
of root find
ludingtheu
a function i
g, and biol
ramasane
RootDis
tionis
of
vi
of a functio
circuitdesi
xedpointite
ideasinma
nction is d
rangeofth
sroot(s),
or
aboutthef
to sign wa
hich broug
ing have be
eofsigncha
s important
ogy. One s
ample:
overy
talimportan
n will be fo
n.Various r
ration,Newt
thematics.F
eveloped fo
function.
herethe
fu
ifteenthcent
s first intro
t about the
n develope
nge,derivati
in many di
uch exampl
cein
almos
nd that ha
ot finding
onRaphson,
nctionscan
r a physical
neofthem
ctionchang
ury. InRobe
uced. Shor
use of the
. These me
es,andtan
ferent area
is related
tall
aspects
its physical
ethods will
andsecant
beusedmod
l process, it
ost importan
ssign.
rtRecordes
tly after th
xy graph
thods rely o
entlinesto
including b
to circuit d
anddiscipli
basis in ele
be used incl
ethods.
elawidera
can be us
tvalue(s)th
TheWhetst
t Rene Des
to plot and
a wide ran
ameafew.
ut not limit
esign in ele
esin
trical
uding
geof
d to
tcan
neof
artes
solve
ge of
ed to
trical
-
8/8/2019 Numerical Methods for Use in Root Discovery
2/15
Whentheswitchisclosedattimet=0,resultingintheremovalofthebattery,thecircuitwillexperience
a transient response. During thisperiod thecurrent flowing in thecircuitwilldecreaseexponentially
withtimeaccordingtothefollowingequation:
2/2+/+(/=0WhereListheinductorvalue,Ristheresistorvalue,andCisthecapacitorvalue
Forthegivenproblemwewanttoimplementacircuitwhosetransientresponseallowsthechargeinthe
circuittodissipateto1percentof its initialvalue inatimeof.05seconds. GivenarethevaluesofL
andC,soonlytheresistorvalueRneedstobesolvedfor. TheRfunctionisgivenas:
()=(2)cos(1/(/2)2)/0
Tofindthecorrectresistorvaluetherootofthisequationmustbesolvedfor. Thebisection,modified
falsepositive,fixedpointiteration,NewtonRaphson,andsecantmethodswillbeusedtofindthisvalue.
NumericalAnalysis
BracketMethods
Thefirsttwomethodsdescribedhereareknowncollectivelyasbracketingmethods.Thesemethodsare
basedonthefactthat inthe localareaofarootthefunctionwillchangesignsonthe leftandrightof
theroot.
BisectionMethod
The first root findingmethod thatwillbediscussed is thebisectionmethod. With thismethod two
guessesareenteredsuchthattheroot isbetweenthem(i.e.thefunction ispositiveforonevalueand
negativefor
the
other
value).
Itthen
takes
the
midpoint
of
these
values
as
an
approximate
root.
Ifthe
signsof themidpointandupperguessareof thesame sign then it isknown that the root lies in the
lowersubinterval.Ifthesignsaredifferentthenitisknowthattherootliesintheuppersubintervaland
anewmidpointbetweentheupperguessandtheoldmidpoint istakenandtheprocessrepeatsuntil
therootisfound.Thiscanbeshownas[1]:
2
Whenf(xl)f(xr)0therootisintheupperinterval;setxl=xr
Whenf(xl)f(xr)=0
the
root
isxr
-
8/8/2019 Numerical Methods for Use in Root Discovery
3/15
Figure1:
Bi
FalsePos
Thefalse
method
whereth
Thisappr
oneend
thefuncti
theuppe
ectionMethod
tionMethod
positionmet
onstructs a
approximat
oximationof
fthelinewi
onuntilitsi
andlowerg
.www.leda
tut
hodrelieso
line betwee
erootwillbe
theroot,Xr
llalwaysbe
terceptiseq
uess[1].This
orial.org
themagnit
f(xupper)
.Thisinterse
oot,thenre
ixedonone
ualtothero
processcon
desoftheu
nd f(xlower
ctionofthe
lacesthegu
oftheguess
ot. Thisisu
inuesuntila
pperandlo
and where
axiscanbe
essthathas
esandtheo
efulasther
rootisfoun
erguessest
this line cr
foundusing
thesamesi
therendpoi
otwillalwa
.
ofindaroot
ssed the x
1]:
gnasXroot.
twillmove
sbecontain
. This
xis is
Thus
along
edbe
-
8/8/2019 Numerical Methods for Use in Root Discovery
4/15
Figure
2:
Fa
OpenMe
The next
equation
twogues
FixedPoi
Thismeth
Whenav
f(x)=0.Th
Thisisco
lse
Position
Me
thods
three root
,andtypical
esthatcont
tIteration
odis
based
alueofxisf
ismethodfin
tinueduntil
thod.
www.wik
inding meth
lyonlyrequir
intheroot[
ethod
nchanging
t
undsuchth
dsanewval
g(xi)=x,mea
ipedia.org
ds used ar
eoneguessi
1].
heequation
tx=g(x),iti
eofxbased
ingthereis
known as
ncompariso
(x)=0to:
equivalentt
ontheoldv
rootatthe
pen metho
ntothebrac
ofindingas
aluesofx.Th
xvalueused
s. These m
ketingmeth
olutiontoth
isisseenas[
inthefuncti
thods depe
dswhichre
originaleq
1]:
n.
d on
uired
ation
-
8/8/2019 Numerical Methods for Use in Root Discovery
5/15
Figure3:Fi
Newton
TheNewt
guess is
function.
Theform
Theproc
edPointMeth
aphsonMet
onRaphson
selected and
Thexinterc
ladescribin
ssoffinding
d.www.pathfi
od
methodhasi
a tangent l
ptoftheco
thisis[1]:
thetangentl
nder.scar.utoro
tsbasisinus
ine at that
putedtang
ineintercept
nto.ca
ingthetang
guess point
ntlineisfou
iscontinued
ntline(deri
is construct
nd,andthis
untilthetan
ative)ofthe
d using the
isthenewr
gentlineint
givenfuncti
derivative
otapproxim
rceptsther
on. A
f the
ation.
ot.
-
8/8/2019 Numerical Methods for Use in Root Discovery
6/15
Figure4:N
SecantM
The seca
linesand
usesthe
compute
Theproc
wtonRaphson
ethod
tmethod is
their xinter
unctionsder
atangentlin
ssfromthis
Method.www.
very similar
cept toappr
ivativetoco
.Thismetho
pointonward
gilkalai.wordpr
to theNew
oximate the
structtheta
dusesthefo
isthesame
ess.com
onRaphson
root.Them
ngentline,
rmula[1]:
astheNewt
method in i
aindifferenc
hilethesec
nRaphson
tsapproach.
e is that the
ntmethodu
ethod
Bothuse ta
NewtonRa
sesadiffere
ngent
hson
ceto
-
8/8/2019 Numerical Methods for Use in Root Discovery
7/15
Figure5:Se
Results
Firstthe
charge. T
cantMethod.
differentiale
hisisdoneb
ww.jwilson.co
quationdesc
:
.uga.edu
ribingthe transientresp
nsegivenabovemustbesolved for qor
-
8/8/2019 Numerical Methods for Use in Root Discovery
8/15
Secondly, the resistor function is plotted to give a general idea of the shape and the root of the
equation.Theequationwasthis:
()=(2)cos(1/(/2)2)/0
Theplotdoneinmatlabisasfollows:
Figure6:PlotoftheResistorfunction
Fromthis
plot
itcan
be
seen
that
the
root
isin
the
range
of
550 2*epsilon)
'Calculate midpoint of domain
midpoint = (right + left) / 2
'Find f(midpoint)
If ((f(left) * f(midpoint)) < 0) Then
'Throw away right half
right = midpoint
ElseIf ((f(right) * f(midpoint)) < 0)
'Throw away left half
500 550 600 650 700 750 800 850 900 950 1000-0.03
-0.02
-0.01
0
0.01
0.02
0.03
0.04
0.05
0.06
-
8/8/2019 Numerical Methods for Use in Root Discovery
9/15
left = midpoint
Else
'Our midpoint is exactly on the root
Exit Do
End If
Loop
Return midpoint
TheresultingrootisR=605.8899after14iterationsofthecode.
FalsePositionMethod
Thepseudocodeusedforthismethodisasfollows[3]:
User inputs: a,b, max, epsilon
Initialize: iteration = 0
xold = b //to start out
Inside a loop with condition that iteration
-
8/8/2019 Numerical Methods for Use in Root Discovery
10/15
WHILE
K :
Pold
Pterm
Pnew
Dg :Delta
RelErr
Dx
Slope :
Theresul
Newton
Thepseu
es=eo
xr=x0
iter =0
ea=999
% Begin
while
xro
xr=
ite
if
end
end
root=xr
Therooti
RelErr
= K+1
:=
:=
:= g(P
= Pnew:=
:
:= Pte
= Dg/Dx
ingrootisgi
aphsonMet
ocodeused
iteratio
a=
Pterm
Pnew
term)
-|
rm -
venas605.7
od
forthismet
loop
old)/f'(x
r-xrold)/
R=605.8841
Tol
Ptermg|
*Delta/(|
Pold
64with14i
odisasfoll
initial
initial
initial
% test
stopp
% update
old)
% update
% update
r)*100
% fin
after14iter
and
{
Pnew|+Sma
{g'(Pk)
erations.
ws[5]:
ze the ro
ze the it
ze the re
to see
ng criter
old value
% calcu
the iter
absolute
% calcula
l error
ations
K
{Increme
{Previou
{Curren
ompute
{Di{
ll) (
{
}
ot with s
eration c
lative er
f relati
ia
of root
late new
ation cou
relative
te relati
-
8/8/2019 Numerical Methods for Use in Root Discovery
11/15
SecantMethod
Thepseudocodeisasfollows[6]:
Input xk, xkMinus1, xTol, maxiters
iters = 1
yk = (xk) (* invokes function f *)ykMinus1 = f(xkMinus1)
root = (xkMinus1*yk - xk*ykMinus1)/(yk - ykMinus1)
ykPlus1 = f(root)
While( (Abs(root - xk) > xTol) and (iters < maxiters) )
xkMinus1 = xk
ykMinus1 = yk
xk = root
yk = ykPlus1
root = (xkMinus1*yk - xk*ykMinus1)/(yk - yk Minus1)
ykPlus1 = f(root)
iters = iters + 1
Endofwhile
ThisgivesarootofR=605.8841after14iterationsofthecode
Summaryofresults(allafter14iterationsofthecode):
Method Bisection FalsePosition FixedPoint Newton Secant
RootValue 605.8899 605.8841 605.7264 605.8841 605.8841
Thistabledemonstratesthat,afterfourteen iterations,thefalseposition,newtonraphson,andsecant
root methods all yield results that are equivalent to four decimal places. For a fixed number of
iterations
the
newton
raphson
method
is
generally
considered
the
most
accurate
method
[1].
It
is
also
worthnotingthedifferencebetweenthemethodstohelpfurtheranalyzetheresults.
Method Input Converge ConvergeRate
Bisection Upper,lower Yes Linear
FalsePosition Upper,lower Yes Better
FixedPoint Anyvalue Usually Better
Newton Anyvalue Usually UsuallyBest
Secant Anytwovalues Usually Better
Fromcomparingtheprevioustwotablesitcanbedeterminedthatforarelativelysimpleequationwith
a derivative that can be taken and that has only one root, the false position is the best bracketing
method, and the newtonraphsonmethod is the best openmethod. While the secant and newton
methodsgavethesameresult,foramorecomplexfunctionthenewtonmethodwillmostlikelyfindthe
rootmorequicklythanthesecantmethod.
-
8/8/2019 Numerical Methods for Use in Root Discovery
12/15
Conclusion
When comparing the various methods available to find the root of a function, there is no definite
method
that
stands
out
above
the
rest.
For
most
general
equations
it
seems
that
the
newton
method
wouldbethepreferedoptioninthatitconvergesthequickestandonlyoneguesshastobemadethat
canbeaboveorbelowtheroot.However ifthefunctionsderivative istaxingtofind,thismethodmay
notbethebest,andthesecantmethodmayworkbetter. Thebracketingmethodsholdonesignificant
advantage inthattheyalwaysconvergeontheroot.As longasthetwoguessesbrackettheroot,the
rootcanandwillbefoundwiththesemethods. Howeverthisisalsothedisadvantagewithbracketing
methods. Ifagraphingutility isnotavailable itmaybedifficult topicka rangewhere the root falls
makingthebracketingmethodsuseless. Whileacomputercanrunalloftheseprogramsefficiently, it
requiresaknowledgeablepersontounderstandwhichofthesemethodsbestfitstherequirementsof
theproblem.
References
[1]Chapra,Steven.NumericalMethodsforEngineers.Pg130150.2010
[2]Wikipedia.BisectionMethod.2010
[3]www.physics.arizona.edu.FalsePositionMethod.2010
[4]www.physics.arizona.edu.FixedPointMethod.2010
[5]www.cs.purdue.edu.NewtonRaphsonMethod.2010
[6]www.vortex.bd.psu.edu.TheSecantMethod.2010
[7]Wikipedia.NumericalAnalysis.2010
AppendixMatlabCode
Functioncode
function y = fcn(x)
y = exp(-x.*(.05)./(2.*5)).*(cos(sqrt((1./(5.*10.^-4))-
((x./(2.*5)).^2).*.05)))-.01;
FunctionandDerivativecode(forusewithNewtonMethod)
-
8/8/2019 Numerical Methods for Use in Root Discovery
13/15
function [y, deriv] = fcn_nr(x)y = exp(-x.*(.05)./(2.*5)).*(cos(sqrt((1./(5.*10.^-4))-
((x./(2.*5)).^2).*.05)))-.01;deriv=exp(-0.005.*x)*((-0.005*cos(sqrt(2000 - 0.0005.*x.^2)) +
(0.0005.*x.*sin(sqrt(2000 - 0.0005*x.^2)))/sqrt(2000 - 0.0005* x.^2)))
Bisection
Code
functiony=bisection(f,a,b,TOL)
sfa=sign(feval(f,a));
Nmax=floor(log((ba)/TOL)/log(2.0))+1
fori=1:Nmax
p=(a+b)/2.0;
sfp=sign(feval(f,p));
if((ba)
-
8/8/2019 Numerical Methods for Use in Root Discovery
14/15
rel_error = 0;
elseif test < 0xu = xr;
elsexl = xr;
end
end
xr
FixedPointCode
function y=op(maxerror, guess, it)count = 0;
error = 1;
maxit=itx=guesswhile (error > maxerror) & (count < maxit)
count = count + 1xnew=feval('fcn',x)error = abs((xnew - x)/xnew) * 100x=xnew
end
NewtonRaphsonCode
function y=newrap(itermax, errmax, guess)
x = guess;
iter = 0;
error = 1;
while error > errmax & iter < itermax
iter = iter + 1;[f fprime] = fcn_nr(x);xnew = x - f / fprime;
error = abs((xnew - x)/xnew) * 100;
x = xnew;
end
SecantCode
function y=secan(guess1, guess2, itermax, errmax)
-
8/8/2019 Numerical Methods for Use in Root Discovery
15/15
x1 = guess1;x2 = guess2;
iter = 0;
error = 1;
while error > errmax & iter < itermax
iter = iter + 1;f1 = fcn(x1);f2 = fcn(x2);
x3 = x2 - f2*(x2-x1) / (f2 - f1);
error = abs((x3 - x2)/x3) * 100;
x1 = x2;
x2 = x3;endx3