Home technology Iteration

Iteration



Relatedconcepts

Function

Inmathematics,iterativefunctionistheobjectofin-depthstudyinfractalanddynamicsystem.Aniterativefunctionisafunctionthatrepetitivelycomposeswithitself.Thisprocessiscallediteration.

Model

IterativemodelisacyclemodelrecommendedbyRUP(RationalUnifiedProcess,unifiedsoftwaredevelopmentprocess,unifiedsoftwareprocess).

Algorithm

Iterativealgorithmisabasicmethodofsolvingproblemswithcomputers.Ittakesadvantageofthecomputer’sfastcalculationspeedandsuitabilityforrepetitiveoperations,allowingthecomputertorepeatedlyexecuteasetofinstructions(orcertainsteps).Eachtimethissetofinstructions(orthesesteps)isexecuted,theoriginalvalueofthevariableistakenfromtheoriginalvalue.Introduceanewvalueforit.

RUPmodel

Understanding

Ifyouthinkthisexplanationisdifficulttounderstand,youcanthinkofitlikethis:

Wedevelopaproduct,ifnotItiscomplicatedandwilladoptthewaterfallmodel.Simplyput,itistofirstdefinetherequirements,thenbuildtheframework,thenwritethecode,thentest,andfinallyreleaseaproduct.

Inthisway,afewmonthshavepassed,andnoonecanseeaproductuntilthelastdayofrelease.

Thismethodhasobviousshortcomings.Ifwearenotveryaccurateinjudgingtheneedsofusers-thisisaverycommonproblem,anditisnotuncommon-youhaveworkedforafewmonthsorevenInafewyears,whenyoushowyourproductstocustomers,customersareoftensurprised.IsthiswhatIwant?

Method

Theiterationmethodisdifferent.Ifthisproductrequires6monthsdelivery,Iwillcomeupwithaproductinthefirstmonth.Ofcourse,thisTheproductwillbeveryimperfect,therewillbemanyfunctionsthathavenotbeenadded,therearemanybugs,anditisnotstable,butafterthecustomerreadsit,theywillputforwardmoredetailedsuggestionsformodification.Inthisway,youwillknowhowfaryouarefromthecustomer’sneeds.AfterIgohome,Ispendanothermonthtofurtherimproveonthebasisofthedemandanalysis,frameworkdesign,code,testing,etc.Imadelastmonth,andthencomeupwithamorecompleteproducttoshowcustomersandletthemComments.

Inthisway,myproductcangraduallyapproachthecustomer'srequirementsintermsoffunctionandquality.ItwillnotappearthatafterIhavespentalotofeffort,Irealizedthatitwasnotwhatthecustomerwanteduntilthefinalrelease.Case.

Advantages

Thismethodisverygood,butitalsohasitsownshortcomings,thatis,thecycleislongandthecostishigh.Whendealingwithlargeprojectsandhigh-riskprojects,suchasthecontrolsystemofaspaceshuttle,thecostofiterationismuchlowerthantheriskofprojectfailure.Thismethodhasobviousadvantages.

IfyouaredevelopingasmallMISforyourownunit,andyouknowyourrequirementswell,theconstructionperiodwillonlytakeamonth,anditeratingisabitofasledgehammer.Itisstillawaterfall.Themodelismoreuseful,evenifitisdoneincorrectly,itwilltakeatmostonemonthtostartagain,whichisnothinggreat.

BasicAlgorithm

Someforeigntextbooks,suchastheChineseversionofthefourtheditionof"C++Primer",willtranslateiterativeintoiteration.

InJava,Iterativeisonlyusedtotraversethecollection,anddoesnotprovidetheabilitytocontainobjects.IfyouneedtocreateanIterativeobject,theremustbeaniteratedcollection.Iterativewithoutcollectionseemstohavenorootsandnovalueofexistence.Iterativemeansrepetition,sosometimes,iterativealsomeanscyclicexecution,repeatedexecution.

Usingiterativealgorithmstosolveproblemsrequiresthefollowingthreeaspects:

Determinevariables

Oftheproblemsthatcanbesolvedbyiterativealgorithms,atleastThereisavariablethatdirectlyorindirectlypushesoutnewvalues​​fromoldvalues,andthisvariableisaniterationvariable.

Establisharelationalexpression

Theso-callediterativerelationalexpressionreferstoaformula(orrelation)ofhowtodeducethenextvalueofavariablefromthepreviousvalueofthevariable.Theestablishmentoftheiterativerelationshipisthekeytosolvingtheiterativeproblem,anditcanusuallybedonebyusingrecursiveorbackwardmethods.

Processcontrol

Whendoestheiterativeprocessend?Thisisaquestionthatmustbeconsideredwhenwritinganiterativeprogram.Theiterativeprocesscannotberepeatedendlessly.Thecontroloftheiterativeprocesscanusuallybedividedintotwosituations:oneisthattherequirednumberofiterationsisacertainvalueandcanbecalculated;theotheristhattherequirednumberofiterationscannotbedetermined.Fortheformercase,afixednumberofloopscanbeconstructedtocontroltheiterativeprocess;forthelattercase,itisnecessarytofurtheranalyzetheconditionsusedtoendtheiterativeprocess.

Example1:FibonacciSequence

Thatissuchasequence:0,1,1,2,3,5,8,13......,inmathematics,thenumbersequenceisdefinedas:

F(0)=0,F(1)=1;F(n)=F(n-1)+F(n-2)(n≥2,n∈N*).

Generally,thissequencecanbeimplementedrecursively.ThefollowingisaniterativeimplementationinClanguage:

intfab(intn)

{if(n<3)

{return1;}

else

{intfirst=1,second=1,temp=0;

for(inti=0;i

{temp=first+second;

first=second;

second=temp;}

returntemp;

p>

}

}

Example1:Anewbreedofrabbitwasintroducedtoabreedingfarm.Thiskindofrabbitwillstartfromthenextmonthafterbirth,everymonthArabbitisborn,andthenewbornrabbitsreproduceinthisway.Ifalltherabbitsarenotdead,askhowmanyrabbitsthereareinthefarmatthe12thmonth?

Analysis:ThisisatypicalRecurrenceproblem.Letusassumethatthenumberofrabbitsinthefirstmonthisu1,thenumberofrabbitsinthesecondmonthisu2,andthenumberofrabbitsinthethirdmonthisu3,...accordingtothequestionMeaning,"Thiskindofrabbitwillgivebirthtoonerabbiteverymonthfromthenextmonthafterbirth",thenthereare

Thefollowingisaquotedfragment:

u1=1,u2=u1+u1×1=2,u3=u2+u2×1=4,……

Accordingtothisrule,thefollowingrecurrenceformulacanbeconcluded:

Thefollowingisaquotedfragment:

un=(un-1)×2(n≥2)*①

Correspondingtounandun-1,definetwoiterationvariablesyandx,theaboverecurrenceformulacanbeconvertedintothefollowingiterativerelationship:

Thefollowingisaquotedfragment:

y=x*2

x=y

Letthecomputerrepeatthisiterationrelationship11timestocalculatethenumberofrabbitsatthe12thmonth.Thereferenceprogramisasfollows:

Thefollowingisaquotedsnippet:

cls

x=1

fori=2to12

y=x*2

x=y

nexti

printy

end

Example2:Amoebareproducesinasimplewayofsplitting,eachtimeitsplitsIttakes3minutes.Putseveralamoebainacontainerfullofnutrientginsengliquid,andafter45minutesthecontainerisfilledwithamoeba.Itisknownthatthecontainercanholdupto20amoeba.MayIask,howmanyamoebawasputinthecontaineratthebeginning?Pleaseprogramittofigureitout.

Analysis:Accordingtothemeaningofthequestion,amoebasplitsevery3minutes,thenputtheamoebaintothecontaineratthebeginning,andfillthecontainerafter45minutes,youneedtosplit45/3=15Second-rate.And"thecontainercanhold220amoebaatmost",thatis,thenumberofamoebaobtainedafter15splitsis220.Thetitleasksustocalculatethenumberofamoebabeforethesplit.Wemightaswellusethebackwarddeductionmethod,from2to20afterthe15thsplit,backtothenumberbeforethe15thsplit(thatis,afterthe14thsplit),andthenFurtherinferthenumbersafterthe13thsplit,afterthe12thsplit,...beforethefirstsplit.

Supposethenumberbeforethefirstsplitisx0,thenumberafterthefirstsplitisx1,thenumberafterthesecondsplitisx2,...the15thsplitThefollowingnumberisx15,thenthereare

Thefollowingarequotedfragments:

x14=x15/2,x13=x14/2,...xn-1=xn/2(n≥1)

Becausethenumberx15afterthe15thsplitisknown,iftheiterationvariableisdefinedasx,theabovereverseformulacanbeconvertedintoThefollowingiterativeformula:

x=x/2(theinitialvalueofxisthenumberafterthe15thsplit:220)

Letthisiterativeformulaberepeated15times,Youcandeducethenumberofamoebasbeforethefirstsplit.Becausetherequirednumberofiterationsisacertainvalue,wecanuseafixednumberofloopstocontroltheiterationprocess.Thereferenceprogramisasfollows:

Thefollowingisaquotedfragment:

cls

x=2^20

fori=1to15

x=x/2

nexti

printx

end

Example3:VerifyKakutaniConjecture.TheJapanesemathematicianShizuoKakutanidiscoveredastrangephenomenonwhenstudyingnaturalnumbers:Foranynaturalnumbern,ifnisanevennumber,divideitby2;ifnisanoddnumber,multiplyitby3,andthenadd1.Afterafinitenumberofoperationsinthisway,thenaturalnumber1canalwaysbeobtained.PeoplecallthisdiscoverybyShizuoKakutanithe"KakutaniConjecture".

Requirements:Writeaprogram,inputanaturalnumbernfromthekeyboard,andprintoutthewholeprocessofnafterafinitenumberofoperations,whicheventuallybecomesanaturalnumber1.

Analysis:Definetheiterationvariableasn.AccordingtothecontentofKakutani’sconjecture,theiterativerelationshipintwocasescanbeobtained:whennisanevennumber,n=n/2;whennisanoddnumber,n=n*3+1.TodescribeitinQBASIClanguageis:

Thefollowingisaquotedfragment:

ifnisanevennumberthen

n=n/2

else

n=n*3+1

endif

Thisistheiterativeprocessthatneedstoberepeatedbythecomputer.Howmanytimesdoesthisiterativeprocessneedtoberepeatedtomaketheiterativevariableneventuallybecomeanaturalnumber1,whichissomethingwecannotcalculate.Therefore,itisnecessarytofurtherdeterminetheconditionsusedtoendtheiterativeprocess.Aftercarefulanalysisofthesubjectrequirements,itisnotdifficulttoseethatforanygivennaturalnumbern,aslongasthenaturalnumber1canbeobtainedafterafinitenumberofoperations,theverificationworkhasbeencompleted.Therefore,theconditionusedtoendtheiterativeprocesscanbedefinedas:n=1.Thereferenceprogramisasfollows:

Thefollowingisaquotedfragment:

cls

input"Pleaseinputn=";n

dountiln=1

ifnmod2=0then

remIfnisanevennumber,calltheiterativeformulan=n/2

n=n/2

print"—";n;

else

n=n*3+1

print"—";n;

endif

loop

end

Applicationexample

IterativemethodisusedAcommonlyusedalgorithmdesignmethodforfindingapproximaterootsofequationsorequations.Supposetheequationisf(x)=0,usesomemathematicalmethodtoderivetheequivalentformx=g(x),andthenperformthefollowingsteps:

⑴ChoosetheapproximaterootofanequationandassignittoVariablex0;

⑵Storethevalueofx0invariablex1,thencalculateg(x1),andstoretheresultinvariablex0;

⑶Whenthedifferencebetweenx0andx1Whentheabsolutevalueisstillgreaterthanthespecifiedaccuracyrequirement,repeatthecalculationinstep(2).

Iftheequationhasroots,andtheapproximaterootsequencecalculatedbytheabovemethodconverges,thex0obtainedbytheabovemethodisregardedastherootoftheequation.TheabovealgorithmisexpressedintheformofaCprogram:

[Algorithm]Iterativemethodtofindtherootoftheequation

Thefollowingisaquotedfragment:

{x0=initialapproximationRoot;

do{

x1=x0;

x0=g(x1);/*Calculateanewapproximaterootaccordingtoaspecificequation*/

}while(fabs(x0-x1)>Epsilon);

printf("Theapproximaterootoftheequationis%f\n",x0);

}

Iterativealgorithmsarealsooftenusedtofindtherootsofequations.Let

X=(x0,x1,…,xn-1)

SettheequationThegroupis:

xi=gi(X)(I=0,1,...,n-1)

Theiterativealgorithmforfindingtherootsoftheequationgroupcanbedescribedasfollows:

p>

[Algorithm]Iterativemethodtofindtherootsofthesystemofequations

Thefollowingisaquotedfragment:

{for(i=0;i

x=Initialapproximateroot;

do{

for(i=0;i

y=x;

for(i=0;i

x=gi(X);

for(delta=0.0,i=0;i

if(fabs(yx)>delta)delta=fabs(yx);

}while(delta>Epsilon);

for(i=0;i

printf("variablexTheapproximaterootof[%d]is%f",I,x);

printf("\n");

}

UseiterationPayattentiontothefollowingtwopossiblesituationswhenfindingroots:

⑴Iftheequationhasnosolution,theapproximaterootsequencecalculatedbythealgorithmwillnotconverge,andtheiterationprocesswillbecomeanendlessloop.Beforetheiterativealgorithm,checkwhethertheequationhasasolution,andlimitthenumberofiterationsintheprogram;

⑵Althoughtheequationhasasolution,theiterativeformulaisnotproperlyselected,ortheinitialapproximaterootoftheiterationisnotreasonable,Itwillalsocauseiterationfailure.

①Nisthenumberofrabbits,Misthemonth(N+N*1)^M-1=2N^M-1(Note)

This article is from the network, does not represent the position of this station. Please indicate the origin of reprint
TOP