MathGroup Archive 2003

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

Search the Archive

Re: Simple recursion problem: there must be a simple answer!

  • To: mathgroup at smc.vnet.net
  • Subject: [mg42686] Re: [mg42654] Simple recursion problem: there must be a simple answer!
  • From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
  • Date: Sun, 20 Jul 2003 06:21:07 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

On Saturday, July 19, 2003, at 09:19 AM, George W. Gilchrist wrote:

> I have spent the better part of a day trying to figure out how to keep  
> the
> following from going into an infinite loop. Such a simple thing to  
> program
> in other systems, but how the heck do you do it in Mathematica? It is  
> simply a
> case where the value of p in the next generation is a function of p in  
> the
> previous and the result is written to a vector for later reference.
>
> AA = 1.0;
> Aa = 1.0;
> aa = 0.5;
> freqP[0] = 0;
> freqP[1] = 1;
> freqP[P_] := (AA*P^2 + Aa* P*(1 - P))/
>     (AA*P^2 + Aa*2*P*(1 - P) + aa *(1 - P)^2);
> p = Table[0.7, {10}]
> For[i = 1, 1 < 10, i++, p[[i + 1]] = freqP[p[[i]]]]
>
> Any suggestions of Mathematica books that would be helpful for figuring
> something like this out in a few minutes instead of a few days? Thank  
> you
> for any help.
>
> ==================================================================
> George W. Gilchrist                        Email #1: gwgilc at wm.edu
> Department of Biology, Box 8795          Email #2: kitesci at cox.net
> College of William & Mary                    Phone: (757) 221-7751
> Williamsburg, VA 23187-8795                    Fax: (757) 221-6483
> http://gwgilc.people.wm.edu/
>
>
>
>

First of all: your program works fine as long as you replace the  
nonsensical
1<10 in your For loop by i<10:

In[1]:=
AA = 1.0;
Aa = 1.0;
aa = 0.5;
freqP[0] = 0;
freqP[1] = 1;
freqP[P_] := (AA*P^2 + Aa* P*(1 - P))/
     (AA*P^2 + Aa*2*P*(1 - P) + aa *(1 - P)^2);
p = Table[0.7, {10}];
For[i = 1, i < 10, i++, p[[i + 1]] = freqP[p[[i]]]]

In[9]:=
p

Out[9]=
{0.7,0.732984,0.76008,0.782604,0.801545,0.817646,0.831471,0.843449,0.853 
913,0.\
863123}

Secondly, this is a bad way to achieve this in Mathematica. The right  
way is:

In[10]:=
NestList[freqP,0.7,9]

Out[10]=
{0.7,0.732984,0.76008,0.782604,0.801545,0.817646,0.831471,0.843449,0.853 
913,0.\
863123}



Andrzej Kozlowski
Yokohama, Japan
http://www.mimuw.edu.pl/~akoz/
http://platon.c.u-tokyo.ac.jp/andrzej/


  • Prev by Date: FW: Simple recursion problem: there must be a simple answer!
  • Next by Date: Re: Eulerian angles
  • Previous by thread: FW: Simple recursion problem: there must be a simple answer!
  • Next by thread: Re: Simple recursion problem: there must be a simple answer!