MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Looping- Programming

  • To: mathgroup at smc.vnet.net
  • Subject: [mg101455] Re: Looping- Programming
  • From: dh <dh at metrohm.com>
  • Date: Wed, 8 Jul 2009 07:07:23 -0400 (EDT)
  • References: <h1l3up$2b9$1@smc.vnet.net>


Hi Osman,

I packed your procedure in a module and use "NestList" for looping.

Daniel

========================

step[poc_] := Module[{new,t},

   new = If[RandomReal[] <= 0.1

       , t = Round[RandomReal[] #]; {t, # - t}

       , #

       ] & /@ poc;

   new = Flatten[new];

   Round[ new  RandomReal[{1, 2}, Length[new]] ]

   ]



  NestList[step, {1000, 300, 7000, 60, 550}, 20]

=============================================



Osman Tuna wrote:

> We want to make a demonstration about population and cities. Firstly we chose an initial condition, let's say we have 5 cities with population {1000,300,7000,60,550} respectively. After that we give a decision population growth rate with selection random number. After we find new population of cities we decide whether a city divide or not, and how they will divide. We again give a random number to these cities population,

> and if this random number  0.0<=x<=0.1 it will divide, if 0.1<x<=1.0 it will not divide. We decided which ones will be divided, now we decide how they divide, again select random number for cities -we know that they divide- and cross these random number and cities population. So at the end of this process our number of cities increase and our total population increase. But now we want to repeat this process again and again until 20th process. And we can see how we will have cities and population with our world model. I have studied on this demonstration, I success to write code for one process but I can't make the repetition of these processes until 20th step.



> 

> (*poc: population of cities,

> cwbd: cities will be divided,

> cwNbd: cities will NOT be divided,

> pocwbd: population of cwbd 

> pocwNbd:population of cwNbd,

> pocadF+pocadS:pocwbd *)

> 

> 

> poc[0]  = {1000,300,7000,60,550};

> 

>  s= Table[Random[], {Count[poc[0], _Integer]}];

> 

>  p = Partition[Riffle[s, poc[0]], 2];

> 

> cwbd = Cases[p, {x_, y_} /; 0.0 <= x <= 0.1];

> 

>  cwNbd = Cases[p, {x_, y_} /; 0.1 < x <= 1.0];

> 

>  pocwbd = Cases[cwbd, {x_, y_} -> y];

> 

>  pocwNbd = Cases[cwNbd, {x_, y_} -> y];

> 

>  pocadF =

> 

>   IntegerPart[Cases[cwbd, {x_, y_} -> y]*

> 

>    Table[Random[], {Count[pocwbd, _Integer]}]];

> 

>  pocadS = pocwbd - pocadF;

> 

> poc[1]= Join[pocwNbd, pocadF, pocadS];

> 

> poc[2]=poc[1]+poc[1]*IntegerPart [Table[Random[],{Count[poc[1],_Integer]}]];

>  

> s= Table[Random[], {Count[poc[2], _Integer]}];

> 

>  p = Partition[Riffle[s, poc[2]], 2];

> 

>  cwbd = Cases[p, {x_, y_} /; 0.0 <= x <= 0.1];

> 

>  cwNbd = Cases[p, {x_, y_} /; 0.1 < x <= 1.0];

> 

>  pocwbd = Cases[cwbd, {x_, y_} -> y];

> 

>  pocwNbd = Cases[cwNbd, {x_, y_} -> y];

> 

>  pocadF =

> 

>   IntegerPart[Cases[cwbd, {x_, y_} -> y]*

> 

>    Table[Random[], {Count[pocwbd, _Integer]}]];

> 

>  pocadS = pocwbd - pocadF;

> 

> poc[3]= Join[pocwNbd, pocadF, pocadS];

> 

> poc[Y]=poc[Y-1]+IntegerPart[poc[Y-1]*Table[Random[],{Count[poc[Y-1],_Integer]}]];

>  

> s= Table[Random[], {Count[poc[Y], _Integer]}];

> 

>  p = Partition[Riffle[s, poc[Y]], 2];

> 

>  cwbd = Cases[p, {x_, y_} /; 0.0 <= x <= 0.1];

> 

>  cwNbd = Cases[p, {x_, y_} /; 0.1 < x <= 1.0];

> 

>  pocwbd = Cases[cwbd, {x_, y_} -> y];

> 

>  pocwNbd = Cases[cwNbd, {x_, y_} -> y];

> 

>  pocadF =

> 

>   IntegerPart[Cases[cwbd, {x_, y_} -> y]*

> 

>    Table[Random[], {Count[pocwbd, _Integer]}]];

> 

>  pocadS = pocwbd - pocadF;

> 

> poc[Y+1]= Join[pocwNbd, pocadF, pocadS]

> 

> Y's initial value is 4, how can we repeat this loop until 20th step. I want to find poc[5],poc[7],poc[9] etc...

> 

> Osman Tuna

> 




  • Prev by Date: Re: put some elements to zero
  • Next by Date: Re: equation expression problem
  • Previous by thread: Re: Manipulate not working
  • Next by thread: compile related issue