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: [mg42683] Re: Simple recursion problem: there must be a simple answer!
  • From: Bill Rowe <listuser at earthlink.net>
  • Date: Sun, 20 Jul 2003 06:21:02 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

On 7/19/03 at 3:19 AM, gwgilc at wm.edu (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]]]]

If I correctly understand what you want, replacing the last two lines of code with

NestList[freqP, 0.7, 10]

will do what you want.

> Any suggestions of Mathematica books that would be helpful for figuring
> something like this out in a few minutes instead of a few days? 

I don't have any specific books to recommend and I suspect the issue is more a matter of working with Mathematica than anything else. At least, that is how it worked for me. 

One suggestion, whenever you feel tempted to use the proceedural programming constructs such as For take a look at the functional programming constructs instead, Generally, Mathematica seems to do things much more efficiently using functional programming. It may take awhile before you get used to this. But if you use Mathematica much at all, the time invested will prove worthwhile in the long run.


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