Lindo Systems

! A simple linear population model  (PoplnMod.lng);
! Keywords: Eigenvalue, Population growth;
SETS:
  GROUP: X0;
  TIME;
  GXG( GROUP, GROUP): A;
  TXG( TIME, GROUP): X , RATIO;
ENDSETS
DATA:
  GROUP = BUNNIES CLOVER FOXES;
! Initial population in time period 0.
  You can get different growth rates, depending
  upon initial configuration;
       X0 =
	! 1st eigenvector;
              !73507  33817 58763;
	! 2nd eigenvector;
              !82067  37069 43485; 
	! 3rd eigenvector;
              83970  43307 32765;  

! A(i,j) = effect on GROUP i of GROUP j in each time period.
  Notice, Clover is very helpful to Bunnies, Foxes not so much;
  A =! Bunnies Clover  Foxes;
       0.612   2.291  -1.200
      -0.295   2.321  -0.560
       0.500  -0.050   0.110 ;
! Number of time periods we will simulate;
  TIME = 1..9;
ENDDATA

CALC:
  @SET( 'TERSEO',2);    ! Output level (0:verb, 1:terse, 2:only errors, 3:none);
! Compute population of GROUP s in time period 1 based on period 0;
  @FOR( GROUP( s):
    X(1,s) = @SUM( GROUP(i): X0(i)*A(s,i));
      );

! Compute populations in period t based on period t-1;
  @FOR( TIME( t) | t #GT# 1:
    @FOR( GROUP( s):
      X(t,s) = @SUM( GROUP(i): X(t-1,i)*A(s,i))
         );
       );

  @WRITE('   ');
  @FOR( GROUP(s):
    @WRITE('               Growth');
      );
  @WRITE(@NEWLINE(1));
  @WRITE(' Period');
  @FOR( GROUP(s):
    @WRITE(' ',@FORMAT( GROUP( s),"9s"),'   Rate    ');
      );
  @WRITE(@NEWLINE(1));

  @FOR( GROUP(s):
    @WRITE('    ',@FORMAT(X0(s),"11.1F"),'    ');
      );
  @WRITE(@NEWLINE(1));
  @FOR( TIME(t):
    @WRITE(@FORMAT(t,"3.0F"));
  @FOR( GROUP(s):
    @IFC( t #EQ# 1:
      RATIO = X(t,s)/X0(s);
     @ELSE
      RATIO = X(t,s)/X(t-1,s);
        );
    @WRITE('  ', @FORMAT(X(t,s),"11.1F"), '  ',@FORMAT(RATIO,"6.3F"));
      );    
   @WRITE(@NEWLINE(1));
 );
ENDCALC