for loop help - Newton's method
8 views (last 30 days)
Show older comments
Joshua Scicluna
on 11 Jan 2021
Commented: Joshua Scicluna
on 12 Jan 2021
Hi, I am trying to find the root/s of a polynomial using Newton's method, underneath is what I got till now. I wish to improve this code by somehow modifying the for loop to keep iterating until a fixed root is found i.e., the answer converges to a specified accuracy. Any ideas? Thanks !!
DP = 16;
DP_old = digits;
digits(DP);
DP_current = digits;
syms x
f_x = 2*x^5 - 6*x^4 -12*x^3 + 20*x^2 + 42*x + 18; % roots = [-1 -1 -1 3 3]
d_f_x = diff(f_x);
n = 77;
x_k = zeros(1, n);
for i = 1:n %keep looping until for example x_k(i+1) = -1
disp(i);
x_k(i+1) = x_k(i) - (subs(f_x, x_k(i))/subs(d_f_x, x_k(i)));
disp(vpa(x_k(i+1)));
end
root = round(min(x_k));
fprintf("root = %d\n\n", root);
0 Comments
Accepted Answer
David Hill
on 11 Jan 2021
I would avoid symbolic unless you need to.
f = [2,-6,-12,20,42,18];
df = polyder(f);
tol=1e-10;
error=1;
x=[-2,4];%initial root guesses
for i = 1:length(x)
while error>tol
a = x(i) - polyval(f,x(i))/polyval(df,x(i));
error=abs(a-x(i));
x(i)=a;
end
error=1;
end
More Answers (1)
John D'Errico
on 11 Jan 2021
Edited: John D'Errico
on 11 Jan 2021
I agree with much of what David has written (except for one thing). If you wish, feel free to accept his answer, as long as you read my answer and make an effort to understand what I have said.
The only real exception I have is his use of error as a variable. Since error is itself already a useful function in MATLAB, while you CAN name a variable error, it is a good idea to learn to NOT do so. Naming variables with the same name as useful and important function names in MATLAB will cause you a great deal of grief one day. Maybe not today, maybe not tomorrow, but one day you will be happy you learned this lesson. And if you do learn the lesson today, then you will never need to experience the frustration of having to post a question on Answers, like "Why does the function error not work for me?"
But the point of not needing symbolic at all is an important one. Symbolic tools will only slow down your code. Use of subs is a poor and terribly inefficient way to evaluate an expression. It is especially poor in the code written in the original question, because there the numbers will become nastier and nastier, with more an more digits per iteration.
syms x
f_x = 2*x^5 - 6*x^4 -12*x^3 + 20*x^2 + 42*x + 18;
d_f_x = diff(f_x);
x_k(1) = sym(0);
for i = 1:10
x_k(i+1) = x_k(i) - (subs(f_x, x_k(i))/subs(d_f_x, x_k(i)));
end
x_k =
[0, -3/7, -9/14, -771/1001, -4621311/5436431, -74926899848343/83125166507383, -18581774083263136027457247627/19877527069259662209650910667, -1103319036541955809733816496515862383910895524139551341307/1153152790849676866377462019365820183982571984391568595067, -3805003198411584763071384595557020036783999204314124440401876921207856379118208705553806362983095031784762708934827/3917451661060113742522646647537708639977123244673765496048702056639847645722154249423498286801610805686924121201067, -44620265102425008438012206333772824282049114426102429636348887463032675153767570323339066109358111926782720359725693430762620655429210467799918876780596522930457686880362040431435938995030976223217915710812997971094098647247299307/45488643279245262836365573480880936426081773497468026618864429885112829609473977128504444926981915594681060887104619555566363613432127462604242164604262149425041580599335897332318096132621625271163335574355553292820283224955717867, -6080024711187465087473536404281988463194668261279871712733491242335920152554876810216579819091334458537201335450796854363122897508965813858632386672195213087533854500292501701587998151562657451181909984926226403588546123069244963775319411517264603307627835295994600646346393260488898527391752951803412155442625175070416889083851494965630801249701089202160553280055752428007624314384061908291074693265082479777068343378520120334306774741352817239549602215725227/6158273320763203257907091406734885630732908549023632199869057264774673048456502605509733573253289180413405123173766306495486437618081563402435561978259653128334804244002915150914911636257590561691019017778968217664655243265656012379563580437164996154041745088913796570501905564349152154717100965794733827363104495692525414965370636687387470968971689448613634324494392506212052025146558761887229941739996068878113661326381376079458104500058125060640454276074667]
double(x_k')
ans =
0
-0.428571428571429
-0.642857142857143
-0.77022977022977
-0.850063396371627
-0.901374433237233
-0.934813169591886
-0.956784777608697
-0.971295507289527
-0.980910000513986
-0.987293742011756
Are the iterations converging to -1? Well, it appears they are. A bit slowly. That is a characteristic of my about multiple roots in rootfinding problems. Rootfinders tend to be slowly convergent, if they converge at all for multiple roots.
But the point I want to make is EVERY iteration, we will find the iteration actually takes more time to compute, with no real gain. Do you see that each number in that sequence has become longer and longer. Lots more digits in the numerator and denominator, yet it is clearly slowly convergent. In fact, I was going to allow that iteration to iterate for 100 iterations, but I finally stopped it after 19 iterations, because nothing was happening on my computer. Do you want to see the 19th iterant?
x_k(19)
ans =
-2113500518605829274696263566312951920641824241697325989967877781200219390478310614680793557883465688487834534073860633497308087326957014914717705538532743064636884437783441096250913265777983618933153286249637059398975968740018468641816119078680559174235882629184367246540082905566454220247343300845059462579949432493821512247230014010730490775702072982527395766060588931998277028500012122338983514371669785411591943581009739238334800545621005947719969950635323387219016931363262658614217265556307409090664902562437732373749231322038556429749980994755943029041890493359154645111589549031122608043470012332502719403709489112445070519711308702165956930731486248347411680239951346586689051161164305342291114339383046837433704171516936347762885239693742718741103790560170311743913873535204262475248016049337785392670101697169560006506420866499669507399034609954695754328914054040400343511614272231605787413615275097370818940497516379947604027531314382176888803401322238725232136342087738229944758055548801316563874024591489066171063977429013807357612530823979863826338105217581318626810339216768993168633159161457130025074810527829386969223695675434228994243708014572404984941154002582483338009886684251552529897755139792910810245949950284676452638297154179146756140731232031276430324853561220212650930432958716503733143285886707593661245456965042954292270662024644557730533733028353951710354495508016714370873295223943582034606690271317285155479036456250015511286172293787128884348632017355242108738152745017121314189631418688729177361842998062701254016244716062269297211322751567120143437254557995315810495364425647685883126662826762719924605380130711174425614620145069012528262160511090346506564542844226472516034551563279330230133332331300556227982124106397562977786038857039854262706334727303773757490569932161148732831215540568096662258720901726901328126974729913878266840427210948374548616383010599881771946701938699730655989120266258171819572511053485088433124419563422874531705300609885512377951704306302808028881564007310749313975017704545637096154783835868536766580825666973077924222650027836270083398909575392732136942273465360980548625643704939532401450191254377841289418901267087796210481687089267808734708687983366000956349395016677917023390982317789270028744971217283849076131088610365168136959865780299901854403010185562074248897758754692711839554461598914529006609092564087957940805755498336263637023363518749318450554364318761832745516590205868723182977432635760339871297973620224183630902486792066030730351777899993626282891871623370321984598027689366041522575899748730674023729899201996165605049583107521650575037664145753101249631105343370663502272262370225848519201876466232376531374820105449272274318087684063032275929475439480908999375422302736158616294773069848180692206466597794279101342914062388467611663565218219106145858139561390199445143758906849650210523261774847036245056366312308214266499615126491984525078062620774139249561514192019863536977382561003599164925517881823413834660548143480111394302847140918707097804604556484136532005079735514817400179157220157756814191026943089725889617411486888182396745183822266122204149254713624059765643522862860401783094492035555689750779326043775697019939551561647598903912129372039952629947678687322606017782202881477902357811903674460035645303614173685461766499712478328263760525013722763192098507311113477410424916463201823784899977873235115442348924374787781051933776980552122333057958993804575866532374433211753459307804198892918668028231787534904695253085650231852275344036522504437342173594540195906915533924551768793790213969434755792518382435555515678605548265519945701482369534741825176898493332376073538312982971596309782737620553772163936380843495399900560494693515088407491331382212199454972896765418791794607330918648083917600281786857539811528256072403914917304876493959976970837382651039162992104251206669483430704302521422890234974490838838090341347739020624320933029298060363672080960643230715027058334709344392298883608264586608761983838198872472393370077599016931198538050785510952060275470273845496041637488289457296904886141891884460749779397015702445806664388357765001577898803213666374398646355088029439808111029333591500166267741002921375034908573773779100445572683795510102822768620448773765434114275784776964085957422814409413936240482871603580186457633855372725276570905996455820338591366510055925449874687060201150783720086621752716338945586931280240665170062368098061080150082080672298243900977162456572325745452315726874158701429000672244853534914791471831504055881026976792010954443260504715372530724310798036984528011465004994081832575151543560827768638286173879878292534471077015446170521102397522945722461959204054016968047845390099262757438028399166847658047096276274421854510633386156562302683718537032188641631053536132323678413838004233448125367057825809790887124451420029581756776128376003103861697118979026546694111692450923431085956061876565823773246904577596060993625822079106396803060958647006713252639259389011189546802648096368309828273851549964574366720257989885926672007936038483312728854817458883116479214695906374351202033251825196774647794836431841824385970506602988469308769566297676392835652662594021630347408899284870606528492016127555071081449470009041271207274193941105429058319109062735881602690308713787294090253885903524096765302041799463112618296334086066915164534724672516275599728451828800388080146388606707165823340044727486909786602230702116829683810696921059763495582887689298952700934628776598703258891900940348201865950288213408257190127047315687642454655680570960501353611581471481031823262644770674266964882219384359291831655813527008760657871586664492865637533272083358438317123606633801722013168551343977530954510767936737035910422201804967828444487538094169656025304350777325601580802705778435903459204703186519894668370387719836446020683892441781083710143365641457034350176856932343943403146474892528101519942622168393030540681475354455044797816076248616379528303061251964168818984874884258063951386028301296413496973974305848430141328031608806199562802854249283815287674082489508813899825646409247516497829683184061305495614888169276610085570273025554035049088073020515132268060430525380729739220035502263375875000642456174287814490633189537975625715973963582352873907318796094740328784660983281657619739082107464780504842407199017136251315924987999357605729539279381540782420779866450135335550776154056810317235288255326035998715730834145437578138185906127578420005838857875764934536609917200351181770122809502429641658835310358580939959775290340172097753020493929307374163751534126440420299122987924326632642457266480537313462338431677456938729784852599691336499850838680314561022196212842389484137810469507193265951758234425011521081264840408473424029499171026187524840816013173138427865757686079272659515444435991243087259659657578981077223009563306253364425668618228879554249215519143493327018870923173823949551712516013442110670904276673558385971365293271557964172032129810164165236967798537873739064445293108372237052877464374811369563982440870821065301459205448748846317553984915079347851329229720634298084744586945986537176073513314706659604862909335758698835137759481064236498795201860519397251547950601107383741165334929263600801045353160453912801177547657820932730883656455285509766673933846067649751242175892023738054024498316627378439803650763609796496273508639013895496298547279120902143000051020348602917210145345627325593236308174443339843346132825262915481533001571821839271714416070291203737856297473633633630712805697192668536986021120618836695035462974537849032468486472349629629095320644149358476170749744926730126861589226724215923831992458926967986144052414016301078536973610511858414146687865738048663208487152137245443597977732437365783291825185672830551207622058170661917311826759830489178630274015718742094427721301456983591245914052889873254204120079783696414379647778863304450449897828787030859316261129642219215543142007058338870566178659996806915701314549847977602997125495741182054129455399152601954203233061617721017332086843747330833885149594556540550559237130747138638223827513488768212225177204516163990048656605739463597600481558963457379162475798913462390721248425956731229225855078058082961507529310368365259330455331549600705491409436654304267269077953074663199829052599635146922939601026766751968703845640840274411632495228252781721331471267369241760927212418846265888336960179613513721947332549859069812790887262554207915133851109072646927732639553423400166825659229033752501366463269966745715958371776219741223391273998727289177116516385215043333014625116484446676650988735187587952343064710388122664925645740262020441488795775075343389012177260754050701784960073005171293236333847865761556578889706833536017545167788192373996854753639341012021554964473005789653307773328927800834301201004092437967119641011700470148299879098700972027843367383444309460515274682610790190866544103641161669897735665280220031647760567837910230675497227560987917642530167474867120088783827357470625548294882837055740975863901038209541518768562277043756015813652712494265325003945349171940611537951801769893667787610049145135231243207870628449628734838431143624057803640730535277686151549069745296821209178672958997376773679666795605165964626502613028486437150789059992475626258532686422078780091238506321058055201725486596662414004223912358111257182757044485289624792457989941542735282201380047904157765135526477011566994478352422194905900850503746199434204240409629069800318821351709815620907504910763645277110935412268789837461353564508538564151143126901638725927190702531444813907616528693086103034844001454665452915171274921809511103767391220299077338561723008426284367966349903643552394937792981827500500633044636928173782818502983385124543546698721982957848987926974687424782908642710485434557310711477349494986737528696329301838805072769694633489118599219337496200580564513981460045166709643025040274743776798401633109755827787336931883309842698915089231541525314251473617210496328613083903683824842935705073305082126325569425057445074675235645623701985285367083088980605197327373319221104943753442311815554157916500946603697390640349987411491830450306403410201212127536737851315785873951696343461697662108041791251529274629395565879918838784074431953750552542951604260912732643946025772884306740702553055986748135247385071780387799356204167684329150051546658453628016476283760885061791263654712200436104324483131580404307503458219519645623107800710726806779479975795311720431732153068890285122417835768275834147853925603925059077130013781053386639408814045181869031019241530572206767025147249239173047130122060487944752951631615323264359253510907883810797745807926143506663876810226589630263725332489034925779701101608595629456736574142588462097109689907265845792820954692945471436013492237274607188735359774736017338359358139755372239438867478391844859590996081705353000122384292858511813509859794027110700131384847660005504945892402819437186306712022718528596493198775298238031062422257784877284291287197260293585997798417385338211078442579230476115376208213189219157945964896744786087714941643270392679244204971654920886440585197696596810938809368633333710433641467971581161602573726983235511896302686006103569090024697009858313253975632055721678837574386180307405681876603933132443475670699041995158592707929463186881975911031967125247499710657138084918803322297135950423345794909532328370599810485044204368848342608444574154835532186040439211694743588765465777242057170881693917562510382964977474658701851086334055905853413094894198693806003757181922440712922485552708352450376956810027913138105933837026002006069703778710824785431100948035524068271659271702219649993477137442361665957451536228978345868331295752779982997542055315825526826252764244964858841984240961786775142720967631055020918113726973120641776630322011153929415544954564398893290717388136358899865336846247110381888245893425458666303007354022640511055882055339144332058709843350481568040054605788262834979943594822703909979712861721643815396018049351028747172936809707319061029177864170457421779272617459175422607645348531589572326449667224935542649851974612004881236224510118019379697204394451945868440754779099940000659778996899223125683962195337621495514725857993170600790985441418965865933456116990392725438853987795961851138184590864279463399633207901362040927640929206890946326166188882599774425513380709438709095532700371196057592420211672029763665901096659031024548266368911795971990491747514830430032158835485632070059129799610095594745221299631383593750981665800208457372783492779802386906648544743092417198178699884712630720900822808882281335041484834529670536612361401494595971579331926963317074285182578739862262427201511719926147801940492777993552748132702954314239871739605924829657638590140003714353166264220186944117110270263518436834037771411166790598210459505318592612802116029990611342369652056517765889065080397505963894151440182789392411941896044349189843106774917335792407599957263887515935448609432803536958086809019758252272029519351598363222651655904154404718732356985447955357290726355189505116277957212662318438805784888754235170903778107346988403290416337284236450808087725230248754384583302450824900823255388910805733713924457546266541177956970574289006292481423610600714624549451978683826459086281197793483266691986511250438490450223437918241918167309669726046399047837839310098884085719491990378773545143637369660486739364584718625512751875396297377658143987745853890355997825346222024593377411996863257799170598092853978295994414395927783702724507899042100228073904522330327712994669619979932227263376779187536721823330883806481452882804159198786360180771732968442821132095573662330005832861733966227905546591413323703610613742088220755456461543291269439554579233373483785218704065180078833884344094753697265556014577150708225900943659730973287853634955819098615032403129804850922330034555090274507378131539510801027006067782718829389709393656965974054814762052648295640774521992284646587193719185345148950284752943951476633240177274260421205509852381946160003734218087240025215931359918950892311646914503801523541673381977812556521221970039994572948669108127532910698482252736632885670125805842562602149193679889259293129968768712365683751192275920617440072647646673723450945712006399855685584510099566275025412615897329668847874835270143415501939580698402036715457346633406849331550685487184429773978791152762144551754444309965245058441993980409617676233778341130065691057824423530645550380523868696069710185284929811818647092382252744528511925117908954345957936177438417271474841917054977727759772479004099855977140323631454626340857470083772327046444385068951077179509201693734903253639731708028469380185171790051206217891830771600809912135044856691841132372981820678446670548963610172250049507902903631303142875988095284562505769558605487585785706467781850182230166192277223694590897220251621489892543067116434079897646350594692467687972445166144278695266293185109807040863838824197017163747191009989795825630505106564897640419673003797653859123004231102893059886959192992267556867877114649212723952039286835063592563274691507372057651297200908344006959056387556469900901346074686635910971917859392966426229798232463302844367671265616000989449761060579009386983564863112401189456274892356408154031825137650090898913759014596348276758586246705451533745516662450462051471829801226211044291822713378888378143030068345995589496404274065052691173011782783834629137322407857299469088310333717931464830678021175875734801565132550611714999439253226201127073981334604875494091732968327986399781654079320066052614812717701722670196158447836718746077193038722660064503123118429024530304846813265460478997901993636726130447403383996458293243278732832121373399924307548567127513437293824269587465359672732928750720058805743277654352956726641676806556395050844972231859707112676626839381373610518119935983444698692021302222211303417705052287459402022451070872683099042146793630324053281631327243725699034315225013503101021463023843214150690606215790650026476474268548224624442782668620577344435706003151648632710849092329947697871616034729903592021300829685389602121018375321458067488774142550380731301434937221084093448352214728087812854478865681649225573272341345716252608853107743013258518124190660023559774180593736286088795988346327625188320802080105769017344472839727914369093153975023376525750315984179977793395507739589973493223083552397945147357676079172465145982352891949982118222684845852620258342448275321995248845500525400564454978721505383629553628263845029833795630148591666241846254834835782238234024422508798629597754651795658112028153418741060905972003920906891288210825812961306463037790127199185844819626611727621895963599251268099955834545026941408686390739834159150456073283516920769674450014962403540911357605028722436451311876251364354481464686962366984630006963144281653579664285335642984790913286431402189487304634436505222641255413823138838617468470107768843173893274428810044895979974857296772943644142963149489264878568376537270823961120728806679113693812255410911410999134193969342529891309368736911990957852224166495981298661897237576010559967776424472600580389899749150923490919249950342922055176898507554416985282566971560292286598460047581883505620420555512043107455753327304661242176979183084660296203266468700175074086494133510397639966624630617198274574626140340344484124679122340356864973489911042191372753245886807093766377027834664079249641596593828009962806108741120867325243185756948644179340476980160452009740517399373311590028112713963794866160574518718154763430061944317355405984751545778826866308918402319941808286186403419063562928105748955121872779024344623241982803582583971533728287345347768914904184088758509090129417523750120687027735934754998484982838654596960150135154357605266050162679583137515455722776530344715482844784903776186841125586172101924815411479508798332929299508600488431409092596477233893214453912019861622126859717551705621313704916598183840620567235019101987775438936605661334334284615039741015603977662392137100938283894927527360031886091612740935676731374280970475638149604272703527350525821289439536247223931421484837383627843174837656099680338691543394812159955296956248363265780561820903926291357388759568924055120863134593195098477813289614862728177150838994025864260033010741741085706987863745461613294103238719525369676154802818841072208737316199574110972745118229919419917143273083015719638432065226435861612904957232845487339921038233173074371369102533890900188966275837806430375330636225985701269304656113643712865175140872561977939102292560900839718656032310051687320832567162618565794083403961446273338373086784482270126457011641460421065794850823309481125457531017941649050109335322525332736692626987287464262197404448126816767781902809990293341752094019833872009236579903211530580883732095292496290067263672267441806106333409167992093787109742594760749831582347550520417859599302156209951475513989313031064439821568443375945998507969256811820516271359162009311969598687722202394590092356685959338813366142644414149782763254436544864975141852447663634558371503654368754178511990763726475351741721311355784082266772309351779034451171655423630828734464705962467930352116549061574516549488629196137723075184425053174948363049402128371784888223111796228878062256279857415261306765646853277495583249886005088265567582915013969798333190075265608495325853632962164111550491088650467791979480603473049328715509443853273521677982523643585849660239029614838482533935536493005981168622263957397964650522958233553072804479133769195002740044823673799406751480776569864446788761992138310005706648410915290492465545204345489402585822325740409370175512037336519863207187621811214472258609316951147887588575964735142403032634578060471055981459761197616633413665113717281452161441545305355229895808828215520164978141176470859121415026954234827622970790235929557786768399072321220325337112870019696721422769045892218702004284152591882149147310385399193577391928400330895559451529436821469020954426757710077802649246037528112293833498785206760790293349814083713773761985501771871300122989074732673898137988078781254049781719709929155987510544692266844829194225831159915585834339786927827202074880294026776096530764618345610480140632093704742739607587178193921503152844099535434543357198940076939266499721428287368202850684786674768812428307486572290519677719649614368334351081037684659987373422221476234036653841126125297235968472932135426230645075159027343651741165813003327010045595634775920962708793134985016444331125778338704636139945757980181514198263945301056122795159170247737992574969109441219653026108724748586877243263725885098335704916562601358826481665390694585595417939928586941818755413772216422494213924140962354584711024194099886594128519249506377859955907150226796321610180397699096787559931438092894320053979490470230539085494867522270344628476422992990323816127759512129184749946095831243778833171514220574759782853831489927651888366606054329849158941602926342313548277685261243292291300711137493962993874884308711122512747701543106439306886360198372743699956634267520929509359558203015939351368874553667291265546339672015962830172121003171208867644780099166858991579102049701130466902960937852964867487326648383935529524190598527843683435457963353986458357763241707634511048852548718661065450227585918172957827147949413420858684803431408361439063233114865940554576608210062404444411087419350293303408057732377717627189934250048348373689761782110609837501562684617622675535340217671118271191408060460053664120585056354973888457863671870922733888071886343429682082781357740614340934084130306757230040705902680639275787794105716188120016465056795608246966607410493845281212896540335907419026822141683844011566145017999663431440935403730304402055116132475825213769096815311478766445514084900228297827575718644264585830339004140184845952783274317539341768473255136983190342059455935228771263018990686942111896099012968799539768524351608534274056822976645108227819825619675982708438026622061484969567667653580369277139417717934444334535576048439862898352003521813500832918536864627715087288786923415181452971206497433873244800951366681849765308352191067017224708138150662676655065739737060865536608730345422503845062316221411119525225318992449753519811087658953837481253798974488826689187114233460286707610302739094277494363399395136863867826995232384472042561756755539927525829079687514998105111686486146661542263569942605477513287919238045966876696034230421639934306265072432428020021062696000680139082789644592601523182996880324599095169233652565345091381203287262601319912470814981498888631432258108853761438673755880120342516624426977962712888977588547128464077449190042454887369134206507527476387304395933715341096530722168033067945853302171074254198061315318336445685001502553248911092609179042212952312393175301009095642847250130102399534322756525367577491676780991743053103112041630803435440783094956477277333066797248804549336580576244705549854054018362882213141963396743560097071619745729499708368247984800983898441044840710993644328311939696978074153361277691512159059770838442650547843366701069170310331164938497866042860648874368694633090124643236293541292135575281104505734683240579668781637863256775630424586777610683034496566729834957620232877314634893736504442344060302083655166225660082584895496733308188684180690646451352720717483677993768845503975645637682181286780516205203912034014643283549985505196483839918979905541477446449564079558049718673357735226283739078029691209902670008913334049079014658615356956102145394969692817517965649781699049271979013062259207828593135123318561488527000758796794798167634052095956420797023145260648062037352792818116201540515066894589786199290010113167375237344394559359824165457150040330734776732646709874672331076719843872139309087400104655615861260516989557888328555950965141094917635542167089487694926382633639089643078265721130976648929159351441316594839277470395401896564238626833546095816148166624757470856930046570982446626581240028678668872070177013393520179335942056878114695403770378391877943246473210444970557992428220625160145220738949672656111040849883066541848366732155308268788374081519945802786347258578371362931466529980217143217886388949701031357207483377648646181351737260003929034406304292227464028791240879756157059667367655765299989229678429389679008656763149331521597512730854920887373986373017818796695395720099081862731534986619094040244641191661781709517224627929303666067003944249281392096970440756238957444762018112194382567872717996479328955004426610949072952433930654133062646547501728410067042746008602219019405086812179047735889518866275538050187468416073698028104922542827317163626230023940238306034753624370508501784640466973041007091443159374424948009056592024936012752893898462393284243931726356021150467702413294982342299851430190254499460455645290882924510243937025306576406489682429696595829696997805053184259666035129309356533324728041247217333571620589911642316890629321530047146703836720107196697207841590272836365895043937524920753506202647893815631246609507971989195454321081699377341654544147550625932212950485872400792926404210731213957756515740142994879368569763551704631625337418310680462781936122124350323614133091905654595324153039824071346048028195301244145406561851924165772488970341668510514642288841158897013047749010488830104851145538119915152581077282271278058291485255657791453403343834337091996505423334552895715575877730812785682405799580903775278349876640397014574729748302788515620070990366313998057791774447502817568578179440084246834658446780681790214481932389121395522621445101215099643264783498334535274183813559290170310888610737155560945605160242415773670231450546109787211613674843499111657530294976159340361477386287852229775105747609653148335787862508661921408824889663564459256360133826716384224214641991814679052302465857840003590325911929875085120313800404810471805964854404056863781795265378338898550937032681398771853820331793168727587227216000223715142838733131341264607426205159381451192591442239796405362304872256040417901568311708261727434275387951728501313833297130505678017254932851874838217625833379702486583706437332445661890337841436644418893056273171676747400691487241807489702804971162597645542302596743071576995692936599373744406393734065612321905564039983866472173921535234198580641027772282645949216184986857294004378539949761073449390891597016728141944609718893983581822641464995643471890297130279593087553003944612938380543583175174468629643444569616276208566334493241151249352962785888700488252510660424651577844553535686141557109316060140207605235802067167288480585822751411600172968513527799196622594319626431680904740322422005629074350758289740542170164906357533618857098062432500547510955452331882052806586488978872855216018184084253630256208133825565410052331983888444730551857018478059142136652919290069980542683668216518357017674521922320566519702044659943174572476214931977377298716012615434539900524624458142557277198052111227403396303061105092046872515461891090369902256000651754136569645913757077727710800674823043963680100777443755310890763897268592343669383831535959255734996420490221137658307726968097761007926491996664280871725753511608091535402128279904788888522980988643061494736184708383148974915982607807906592638629921035287543924955067329807477612907994595884797759954028724249571788906794603968613323372404824494355133858607533553622561843304789470938086311911185203710348681862310205205153266814979658287687097000809631316053900313479896824379334031887709045395063835630403844258399993314607560283360685226321373537429231712536706640584638741372568348894648999238386994561348606622611650899963642053730163817933594205582253538278529695894210253512458071165589461851944643316758011428098180203642806440100369736066307989051850267712881179982698015620783648784179014523138844267620294819207383849821343935101513356381454424707838352352726959426298641454493472551918400080235802016294085028918950301133243647200025055816675961356426193635910499685614819567477846919357926260873435940652924476820004540382850745425938823555534114038473664883888004268103900587375728657936476553436712230304156323559781705757852214049539966369731553443636906401525786514845812338535557434066628594844117587290604625516104816274988635278986213686743483769939592855915546321907520941885072311046454147045425675429503478436480396929381020028332327704939884032430801245043680424530886736716084590235801139253971768211600877700922930120290122695165297889237218717027490119365780752664228662758700108121379798779768956867115606050808872353802988482499968017369834283591603381506646420397985616156859676430688116380255377927761710902821945912220357434751847851541447854012188573354064675613761097465278061683843410039119048260028083351567545635896686891148152390395786641164294264743550601646018295727041789192695847421816173956736395935123116768159411823591319503216839703354103547258718484568071207433384824093272041903321245118178541304691539817720180375093383360501678127897636709127359051985406265455821785771311860881599223058490071851648451874402787843263626757731564178753250521040109138182385190499417886167343414269009287372890388791865018465715585779105356563832015485865550820871413397654370692918535575410493112455633025098990391987583593111230037072525132611956705403051305240114277841369331902805919082684193390215084686817139965200287866152525225228367920225803224999005251732528201123182233136424750742135834999027136129160783389394332329367346721243241963172473077589191964625001144923262473117461232378477178618653536355755507209383148484269724765040452092062570046703024065364667853896191140080715072129484165299528957473640930956221059962992366510834674530088949885948534870777509036515738520346613793593814574030192527729619202996793890563659585626607999963600816077296261098024499604406292537738381132429130679115285267476477195103657609662361904468525131173120133185224660499685691083951913705833021316061088793302920616909614404475688541780055047094555496119089646328586159150286826791282034033088987199459532038078805019160316348021343448617123668056101162648547431160287657457601087228799931214640147973057764545514617880115395774121977376898806206256248671582099311354620122260058437978428662549507644677982408719020915352660813457665578183807160028677074836296493506602118628764043461007917560891795867721995940506268600936572473308315212964748246737988290975726719021853950873807844134835509420787535074224810759295224243489123429573406281347670279380663827439868289373355220626335307209314679350406867943293011092704000104110074840260926936202314536575694938845231820883105814242562154031199256724073337039697226904568386539395331778000627825387366469063149540763230345736238754256837837451701646493865586152171136954351257820275836599750293372022329393672298531299326665180880610558696391217054983230408255957696820548090247126850809697609137218787462378947138017009435591316055576906130359715242671947566365325107515755196027886845220145864285939059994296510644954125571088962422964584271787349528739741371435173138143327713800384581346428496534342654806047732221299854620524569582810717871034884511138668123426021551253264928417003631113177196275721909613969532265373311632780071355165679123605658322323906594292972571799686687023725160783331092137949306652020357453402359936791039434998651891491002203157923396836807529765212154305957102859966900906071747339584265090750351270771639349144836975710558918869029445649745150337421523921564757223417195874824529605919269507388855936059304345771338173128145910194123575727416423826864496263942168712398505782196279097123741860074368872417851531196468394998295062731449346953918933704224112808109160967006113580267357896515340839992476194696300716069754048333688017032490441273352171869784133040575718982173464498670802479631392002769577059011713864792208245155602264323385629108519059688284918713724292829346680495574538741745331060578945128237056388562511931824093786526143415217704496205263840803946131152706204347482498310940831859397944209877676749228907737660371341676953616412396940290530078520666341060343890908889414795427893892980442436430244982963951965826776632625867065788075889310923121323173893951867879344901600305253630170143817363336187260564701915328445329979584176867495883013316869263887995536377612196364458788652081328749248777547293194413509174480338543418853563904627555450165739907592367688215345581886098054255850184710192210762182428562186890466696032636465942194615244373018334454025568415778344745277231233280877614111190499167120432712364007900942623653616195279... Output truncated. Text exceeds maximum line length for Command Window display.
If you look at the end of that "number", it will tell you
... Output truncated. Text exceeds maximum line length for Command Window display.
How well has it done so far?
double(x_k(17:19))
ans =
-0.9988877350343 -0.999258558784364 -0.999505736406519
As I said, slowly convergent. Right up there with watching grass grow and nails rust.
Far better to use numerical tools directly on this problem, as there is no need ot use symbolic computations just because you don't know the value of x. For example, using fzero, we see:
f = [2,-6,-12,20,42,18];
tol=1e-10;
[xroot,fval,exitflag,output] = fzero(@(x) polyval(f,x),[-2,0])
xroot =
-0.999997431947064
fval =
0
exitflag =
1
output =
struct with fields:
intervaliterations: 0
iterations: 50
funcCount: 52
algorithm: 'bisection, interpolation'
message: 'Zero found in the interval [-2, 0]'
So 50 ityerations, with 52 function evaluations. The default tolerance on x for fzero here was 2.22044604925031e-16, but note that you can only get a root to within the cuberoot of eps for triple roots. So setting a super high tolerance on x is meaningless here. Again, multiple roots are highly problematic.
Again though, we actually got a fast solution, far better than Newton's method would provide using syms as you wanted to run it.
f = [2,-6,-12,20,42,18];
df = polyder(f);
tol = 1e-10;
err = 1;
x_n = 0;%initial root guess
iter = 1;
maxiter = 100;
while (iter < maxiter) && (err>tol)
x_n(iter+1) = x_n(iter) - polyval(f,x_n(iter))/polyval(df,x_n(iter));
err = abs(x_n(iter)-x_n(iter+1));
iter = iter + 1;
end
But even worse, look at what happened.
semilogy(x_n - -1,'o')
Around iteration 30, the estimate happend to jump across the root. And when it did, the NEXT iteration suddenly moved FAR away from the root.
x_n(29:35)
ans =
Columns 1 through 4
-0.999991384766257 -0.999994376401644 -0.999996716814727 -1.00000015003179
Columns 5 through 7
-0.998355413189684 -0.998903759174153 -0.999269239572119
What you need to understand is for this function, the Newton's iteration will probably NEVER converge to the tolerance you want to see.
And this is why we use better tools for rootfinding! Did you notice this algorithmic comment from fzero?
algorithm: 'bisection, interpolation'
fzero uses guarded methods, switching methods internally when a problem is discovered.
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!