Home Technology Iteration

# Iteration

## Relatedconcepts

### Function

Inmathematics,iterativefunctionistheobjectofin-depthstudyinfractalanddynamicsystem.Aniterativefunctionisafunctionthatrepetitivelycomposeswithitself.Thisprocessiscallediteration.

### Model

IterativemodelisacyclemodelrecommendedbyRUP(RationalUnifiedProcess,unifiedsoftwaredevelopmentprocess,unifiedsoftwareprocess).

## RUPmodel

### Understanding

Ifyouthinkthisexplanationisdifficulttounderstand,youcanthinkofitlikethis:

Inthisway,afewmonthshavepassed,andnoonecanseeaproductuntilthelastdayofrelease.

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

### Method

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>

}

}

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

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

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

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)