Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2007
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2007

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

Search the Archive

Re: A program check

  • To: mathgroup at smc.vnet.net
  • Subject: [mg75523] Re: A program check
  • From: Szabolcs <szhorvat at gmail.com>
  • Date: Fri, 4 May 2007 04:11:22 -0400 (EDT)
  • Organization: University of Bergen
  • References: <f1c4nv$h6t$1@smc.vnet.net>

JGBoone at gmail.com wrote:
> i was wondering if someone could check this program and see if its
> running like the Description typed under each line says its suppose
> to.

What makes you think that it doesn't work as it should? It would be much 
easier to answer your question if you provided a minimal example that 
doesn't work as you expect it to.

Take all the commands one by one and test them. Take all the functions 
and see if they produce the desired result.

> 
> ogspecies=10000;(*set up sizes for communitysize and metacommunity*)
> 
> communitysize= Range[ogspecies];(*makes a vector called communitysize
> with sequencal elements 1 to ogspecies*)
> 
> metacommunity=Range[ogspecies];(*makes a vector called communitysize
> with sequencal elements 1 to ogspecies*)
> 
> unlikeliness= 6000;(*sets up chance of speciation.bigger number means
> less likely*)
> 
> metalikeliness=30;
> (*sets up chance of immigration. bigger number means less likely*)
> 
> chance:=Random[Integer,{1,unlikeliness}]==1;
> (*creates a function that tests if a random integer from 1 to
> unlikeliness is equal to 1. this makes the chance of speciation equal
> to 1/unlikeliness*)
>
> metachance:=Random[Integer,{1,metalikeliness}]\[Equal]1;(*creates a
> function that tests if a random integer from 1 to metalikeliness is
> equal to 1. this makes the chance of speciation equal to 1/
> metalikeliness*)
> 
> size:=Length[communitysize];(*gives size a value equal to the number
> of elements in communitysize*)
> 
> generations=1000000;(*time steps that the funtion runs which is given
> as size squared; this excepted time to monodomince*)
> 
> hubl[group_]:= group[[Random[Integer,{1,size}]]] ;(*takes a random
> element of group*)
> 
> metareplace:=(communitysize[[Random[Integer,
> {1,size}]]]=hubl[metacommunity]);
> (*takes random element of community size and replaces it with random
> element of metacommunity by way of the function hubl[group_]*)
> 
> community:= (communitysize[[Random[Integer,
> {1,size}]]]=hubl[communitysize]);
> (*takes a random element of communitysize and replaces it with another
> random element of community size by way of the function
> hubl[group_]*)
> 
> speciation:=(hubl[communitysize]=Random[Integer,{size+1,size+100}]);
> (*creates a function that produces a random integer from 11 to 110*)

Every time you evaluate the symbol speciation, hubl gets an additional 
definition depending on the value of communitysize. Maybe this is the 
problem?

> immigration:=If[metachance,metareplace,community];
> (*asks if metachance is true. if it is true, runs metareplace if not
> it runs community*)
> 
> changing:=If[chance,speciation,immigration ];
> 
> (*makes new vector by replacing random elements of communitysize by
> way of  the function speciation if the function chance is true or by
> way of the function immigration  if chance is false*)
> 
> SpeciesCount[lyst_]:= Length[Split[Sort[communitysize]]]; (*counts the
> number groupings of like values there are in communitysize. it does
> this by sorting elements together then Split takes identical elements
> and puts them in sublist then Length counts the sublist*)

It is good practice to use symbol names that start with small letters. 
This assures that built-ins are not redefined by accident (though 
SpeciesCount is not a built-in, of course.)

> 
> randomwalks:=Table[changing;{t,SpeciesCount[communitysize]},{t,
> 1,generations}];
> (*creates a table of the function SpeciesCount from time step 1 till
> generation*)
> 
> lyst = randomwalks;
> 
> averagespecies:=N[Mean[Map[Last,lyst]]];
> (*function produces the mean from SpeciesCount of communitysize over
> the
> total time steps*)
> 
> halfaveragespecies:=N[Mean[Last/@Take[lyst,{generations/2}]]];
> (*function produces the mean from SpeciesCount of communitysize over
> half the total time steps*)
> 
> Print[lyst,{averagespecies},
> {halfaveragespecies}];ListPlot[lyst,AxesLabel[Rule]
> {"Generations","Number of Species"}]
> 
> THANKS!
> 

You do not need to terminate commands with a semicolon in Mathematica. 
The semicolon suppresses the output. Only use it if you know that the 
output is too long. Look at the result of every evaluation: this way you 
can see if something went wrong.

Most of the time Print[] is not needed (I only use it for debugging).
Just evaluate a symbol to get its value.

Szabolcs


  • Prev by Date: Re: Gauss-Kronrod algorithm
  • Next by Date: Re: Re: Re: Re: Re: Re: RE: Re: Sometimes <space> means multiple
  • Previous by thread: Re: A program check
  • Next by thread: question (for Mathematica 6!)