You are on page 1of 6

Thefzerofunction

AnotherpopularrootfindingfunctioninMATLABisthefzerofunction.

Definition:fzeroisMATLABssinglevariablenonlinearzerofindingfunction.

fzerohasmanyusefulsyntaxforms,obtaincompletelistbygettingonlinehelp

>>helpfzero

x=fzero(FUN,X0)

FUNisafunctionnamepassedintofzeroasastringcorrespondingtoanexisting
functionfilecontain

X0isascalarvalueusedforaninitialguessattheroot

Hereisalistingofafunctiontobeusedwithfzero

function f = fx3(x)
% fx3 Evaluate f(x) = x - x^(1/3) - 2
f = x - x^(1/3) - 2;
Locate the root graphically (approx.)
fplot('fx3',[05])

Thisshowsthatthereisarootclosetox=3.5
Nowrepeatfplotbyrefiningtherangeofx
Usebisectwiththeappropriateinterval
Rewritefx3toonethathasaderivativeofftheusetheNewtonRaphson
function

1|P a g e

Herearesomecommonusefulsyntaxexamples:

>>r=fzero('fx3',2)
r=
3.5214

ThisissimilarsyntaxtotheoneusedinbisectandNewtonfunctions

>>r=fzero(@fx3,2)
r=
3.5214
Herethe@signreplacesthestringsyntax

>>r=fzero(@sin,2)
r=
3.1416

Here@sintellsMATLABtoevaluatetherootofthesinefunctionclosetoaninitial
guessof2.SoMATLABcorrectlyconvergestothevalueof

Experimentwithothersyntaxformsoffzero.

2|P a g e

ComputingrootsofpolynomialswithMATLAB

>>helproots
ROOTSFindpolynomialroots.
ROOTS(C)computestherootsofthepolynomialwhosecoefficients
aretheelementsofthevectorC.IfChasN+1components,
thepolynomialisC(1)*X^N+...+C(N)*X+C(N+1).

Note:LeadingzerosinCarediscardedfirst.Then,leadingrelative
zerosareremovedaswell.Thatis,ifdivisionbytheleading
coefficientresultsinoverflow,allcoefficientsuptothefirst
coefficientwhereoverflowoccurredarealsodiscarded.Thisprocessis
repeateduntiltheleadingcoefficientisnotarelativezero.

Considerthreebasicsecondorderpolynomialsp1,p2andp3.

p1=x23x+2
p2=x210x+25
p3=x217x+72.5

Sincethesearesecondorderfunctionstheirrootscanreadilybecomputedusing
thequadraticformula.ComputationofrootswithMATLABispretty
straightforward.Defineanarrayconsistingofthecoefficientsofxindescending
orderoftheexponentsofx.Ifthereisamissingcoefficientofthepowerofx,
insertazeroinitsplace.

e.g.z=x6x4+11x33x2+37isrepresentedasfollowsinMATLAB
z=[101113037];zerosforthenonexistent5thordertermandthelinear
term.

Herearethearraysforp1,p2andp3.

>>p1=[132];
>>p2=[11025];
>>p3=[11772.5];
3|P a g e

Computationoftherootsisaccomplishedwiththeaidofthebuiltinroots
function.

>>roots(p1)
ans=
2
1
>>roots(p2)
ans=
5
5
>>roots(p3)
ans=
8.5000+0.5000i
8.50000.5000i

InverserootoperationswithMATLAB

Giventherootsofapolynomialestablishthecorrespondingpolynomialusingthe
builtinpolycommand.Usetherootsofp3asanexample

>>r=[8.5000+0.5000i,8.50000.5000i]
r=
8.5000+0.5000i8.50000.5000i
>>pr=poly(r)
pr=
1.000017.000072.5000

Determinetheresidualobtainedwhenyoucomputethevalueofthepolynomial
bysubstitutingbackitsrootswiththebuiltinpolyvalfunction.

4|P a g e

Syntaxispolyval(p,r);pisthearraywithcoefficientsthatdefinethepolynomial
andristhearraywithrootsofp.

>>res=polyval(p3,r)
res=
0 0

Residualshouldbezerosincebynecessitywearesolvingf(x)=0;however
residualmaybeasmallnonzerovaluethatresultsduetoroundoffdamage.

Illustration:

Giventheexpression:2x2+10x=144/x,determinethevalueofxthatmakesthe
expressiontrue.

Solution:

Rewritetheequationintothestandardformandseektherootsofthefunctionin
theformf(x)=0

Transformedequationthenis:

f(x)=2x3+10x2144

>>p4=[2100144]
p4=
2100144

>>r4=roots(p4)
r4=
4.0000+2.8284i
4.00002.8284i
3.0000

5|P a g e

>>polyval(p4,r4)
ans=
1.0e012*
0.05680.0853i
0.0568+0.0853i
0.1137

Theresidualisasmallcomplexnumberwhichispracticallyzero

Otherinclassdiscussionsanddemonstrations

6|P a g e

You might also like