Roots of Functions MATLAB
Roots of Functions MATLAB
>>helpfplot
fplotPlotfunction
Commontypicalsyntaxis:
fplot(FUN,LIMS)plotsthefunctionFUNbetweenthexaxislimits
specifiedbyLIMS=[XMINXMAX]
FUNhastobespecifiedasastringthatiswithsinglequotes;therange
LIMSforxhastobespecifiedasrangeusingarrayformat
i.e.LIMS=[XMINXMAX]
e.g.generateaplotofsin(x)intherangeof0<=x<=2
>>fplot('sin(x)',[02*pi])
sin(x)isawellrecognizedfunctionbyMATLAB.Ifyouhaveauser
definedfunctionsaveitinthepathforMATLABandcallitasastringas
shownabove.
Anacceptableshortcutusesthefunctionhandle@
>>fplot(@sin,[02*pi])
Bothfplotcommandswillgeneratethesameplot
1|P a g e
Intervalbisection
Theintervalbisectionapproachfordeterminingrootsofequations
dependsonaninitialguessataroot,bracketedbyanintervalsuchthat
rl<r<ru,whereristheguessedroot,rlandruarethelowerandupper
limitsofthebracketinginterval.Therootisthenimprovedinan
iterativemannerbyhalvingtheintervalateachiterativestepand
determiningwhetherweareapproachingtherootfromtheleftorfrom
therightaccordingtothefollowingflowchartfromMustoetal.
Seeacopyofthebisect.mfunctionfromtheNMMtoolboxbelowthe
flowchart
2|P a g e
3|P a g e
Intervalbracketingapproach
functionr=bisect(fun,xb,xtol,ftol,verbose)
%bisectUsebisectiontofindarootofthescalarequationf(x)=0
%
%Synopsis:r=bisect(fun,xb)
%r=bisect(fun,xb,xtol)
%r=bisect(fun,xb,xtol,ftol)
%r=bisect(fun,xb,xtol,ftol,verbose)
%
%Input:fun=(string)nameoffunctionforwhichrootsaresought
%xb=vectorofbracketendpoints.xleft=xb(1),xright=xb(2)
%xtol=(optional)relativextolerance.Default:xtol=5*eps
%ftol=(optional)relativef(x)tolerance.Default:ftol=5*eps
%verbose=(optional)printswitch.Default:verbose=0,noprinting
%
%Output:r=root(orsingularity)ofthefunctioninxb(1)<=x<=xb(2)
ifsize(xb,1)>1,warning('Onlyfirstrowofxbisusedasbracket');end
ifnargin<3,xtol=5*eps;end
ifnargin<4,ftol=5*eps;end
ifnargin<5,verbose=0;end
xeps=max(xtol,5*eps);%Smallesttolerancesare5*eps
feps=max(ftol,5*eps);
a=xb(1,1);b=xb(1,2);%Usefirstrowifxbisamatrix
xref=abs(ba);%Useinitialbracketinconvergencetest
fa=feval(fun,a);fb=feval(fun,b);
fref=max([abs(fa)abs(fb)]);%Usemaxfinconvergencetest
ifsign(fa)==sign(fb)%Verifysignchangeintheinterval
error(sprintf('Rootnotbracketedby[%f,%f]',a,b));
end
ifverbose
4|P a g e
fprintf('\nBisectioniterationsfor%s.m\n',fun);
fprintf('kxmfm\n');
end
k=0;maxit=50;%Currentandmaxnumberofiterations
whilek<maxit
k=k+1;
dx=ba;
xm=a+0.5*dx;%Minimizeroundoffincomputingthemidpoint
fm=feval(fun,xm);
ifverbose,fprintf('%4d%12.4e%12.4e\n',k,xm,fm);end
if(abs(fm)/fref<feps)|(abs(dx)/xref<xeps)%Truewhenrootis
found
r=xm;return;
end
ifsign(fm)==sign(fa)
a=xm;fa=fm;%Rootliesininterval[xm,b],replaceaandfa
else
b=xm;fb=fm;%Rootliesininterval[a,xm],replacebandfb
end
end
warning(sprintf('rootnotwithintoleranceafter%diterations\n',k));
5|P a g e
Usingthebisectfunctiontolocaterootsoffunctions:
Sayyouwanttozeroinonarootfory=7x6
Bysolvinganalytically,7x=6=0;therootisatx=6/7
Usethefplotcommandtolocatetheapproximatelocationoftheroot
Createanmfilethatdefinesthisfunction:
functionf=ftry(x)
%ftryisasimpledemofunctionforrootlocation
f=7*x6;
>>fplot('ftry',[0.61])
>>grid
6|P a g e
Clearlytherootislocatedbetween.85<x<0.9
Forafunctionwhoserootyoudontknow,usethefplotsystem
experimentingwithvariousintervalstillyoulocateanapproximate
root.
ImplementationoftheBisectAlgorithm:
Nowcallthebisectfunctiontocomputetheroot
Thesyntaxis:
r=bisect(fun,xb,xtol,ftol,verbose)
funisafunctionnamepassedintothebisectfunctionasastring;the
secondargumentisarangeofxthatcontainstheroot.Arguments3,4,
and5havedefaultsdefinedwithinthefunction.
>>r=bisect('ftry',[01])
r=
0.8571
Now,toseethestepbystepcomputationsitisnecessarytoprovide
theotherarguments,specificallytheverboseargument.Hereisone
possibility:
>>r=bisect('ftry',[01],.0001,.0001,1)
7|P a g e
Bisectioniterationsforftry.m
kxmfm
15.0000e0012.5000e+000
27.5000e0017.5000e001
38.7500e0011.2500e001
48.1250e0013.1250e001
58.4375e0019.3750e002
68.5938e0011.5625e002
78.5156e0013.9063e002
88.5547e0011.1719e002
98.5742e0011.9531e003
108.5645e0014.8828e003
118.5693e0011.4648e003
128.5718e0012.4414e004
Shortcomingsofthebisectalgorithm
1. Theinitialguessfortheroothastobeintheprovidedrange.
2. Iterativeprocedurecandivergeiftheinitialguessisfarofffrom
theactualroot,roundoffdamagecancausedivergence
3. Takenoteoftheconvergencecheck
8|P a g e