Programming Assessed Work 2
Programming Assessed Work 2
1.1
C Code
visit =0;
// RANDOM WALK
for ( j =0; visit < L ; j ++) // loop t e r m i n a t e s when p a r t i c l e has
visited all L sites
{
pos [ i ]++;
if ( pos [ i ]==1) visit ++;
tempRan = ranInt (2) ; // the p a r t i c l e can move in two d i r e c t i o n s
if ( tempRan ==0) i = lHop (i , L ) ;
if ( tempRan ==1) i = rHop (i , L ) ;
}
tot += j -1; // j is the number of hops in total , i n c l u d i n g the
hop after the last site was visited
}
printf ( " % f \ n " , tot / M ) ;
}
// F U N C T I O N D E F I N I T I O N S
int ranInt ( int max ) // returns a random integer from 0 to max -1
{
return random () % max ;
}
int rHop ( int curSite , int numSites ) // i n c r e m e n t s current site
with p e r i o d i c b o u n d a r i e s
{
if ( curSite == numSites -1)
{
curSite =0;
}
else
{
curSite ++;
}
return curSite ;
}
int lHop ( int curSite , int numSites ) // d e c r e m e n t s current site
with p e r i o d i c b o u n d a r i e s
{
if ( curSite ==0)
{
curSite = numSites -1;
}
else
{
curSite - -;
}
return curSite ;
}
Figure 1: a line graph showing length of path taken for different environments
1.2
Figure 1 shows a few key properties expected in this problem. Firstly, the graph
intercepts at the origin, because it would take no time to explore an environment
of size zero
Secondly, there is an exponential relationship between the number of sites
L and the path length
Proof. To demonstrate why this must be true, consider how the mean path
length in this problem would increase as the environment grew larger
For example, between L1 = 10 and L2 = 20, the shortest possible paths
double, and therefore the mean length must increase by more than double
This means that it is reasonable to suggest that the path length J will satisfy
dJ
L.
dL
This is a property satisfied uniquely by the exponential function
These results were generated using a similar program that looped over L
from 1 to 50 and printed the results to a text file, which was then turned into
c 2015
a graph using the online service Plotly
Here the average path length is 10261.4 to explore the all 103 sites
2.1
C Code
visit ++;
}
tempRan = ranInt (6) ; // the p a r t i c l e can travel in six
directions
if
if
if
if
if
if
( tempRan ==0)
( tempRan ==1)
( tempRan ==2)
( tempRan ==3)
( tempRan ==4)
( tempRan ==5)
i1 = lHop ( i1 , L ) ;
i1 = rHop ( i1 , L ) ;
i2 = lHop ( i2 , L ) ;
i2 = rHop ( i2 , L ) ;
i3 = lHop ( i3 , L ) ;
i3 = rHop ( i3 , L ) ;
}
tot += j -1; // j is the number of hops in total , i n c l u d i n g the
hop after the last site was visited
}
printf ( " % f \ n " , tot / M ) ;
}
// F U N C T I O N D E F I N I T I O N S
int ranInt ( int max ) // returns a random integer between 1 and
max
{
return random () % max ;
}
int rHop ( int curSite , int numSites ) // i n c r e m e n t s current site
with p e r i o d i c b o u n d a r i e s
{
if ( curSite == numSites -1)
{
curSite =0;
}
else
{
curSite ++;
}
return curSite ;
}
int lHop ( int curSite , int numSites ) // d e c r e m e n t s curent site
with p e r i o d i c b o u n d a r i e s
{
if ( curSite ==0)
{
curSite = numSites -1;
}
else
{
curSite - -;
}
return curSite ;
}
Figure 2: a line graph showing length of path taken for different environments
2.2
Figure 2 has the same shape as Figure 1, but with a much steeper gradient,
because of the additional dimensions
The average path length for L = 3 sites per axis (giving 33 = 27 sites in
total) is 119. The corresponding value in the previous question, for a line of
length L = 27 was 354
The corresponding values are lower at every point in this question because
there are more directions to choose from, so the space is explored more quickly
A periodic line (a 1D object) can be happily represented in 2D as a circle,
but a periodic 3D cube cannot be visualised without intersecting itself
Despite this, it is certainly true that every point on the cube of side length
L can be mapped to a corresponding line of length L3 , which could be answered
completely using the tools in the previous question, provided that all six possible
neighbours are handled appropriately at each site.
Therefore, it is reasonable to expect that these questions would have similar
results
Diffusion on a Network
There are B sites in each of the A clusters in this network. The clusters are
connected in a periodic line, that can be moved along only when in sites i = 0
and i = 1 in each cluster, and only left and right respectively. Each site is
connected to every other within the same cluster
3.1
C Code
A =5; // number of c l u s t e r s
B =4; // number of sites in a cluster
pos [ A ][ B ]; // indexes sites and their c l u s t e r s
visit =0; // i n c r e m e n t s each time a site in a cluster is
visited for the first time
if ( pos [ c ][ i ]==1)
{
visit ++;
}
if (i >1) // then p a r t i c l e cannot change cluster
{
tempRan =0;
}
else // p a r t i c l e will either change cluster or hop within
cluster
{
tempRan = ranInt (2) ; // if current site is 0 or 1 , p a r t i c l e
either changes cluster or hops within cluster , with
equal chance
}
if ( tempRan ==0) // then p a r t i c l e hops to site from 0 to B -1
within cluster
{
i = siteHop (i , B ) ;
}
if ( tempRan ==1) // then p a r t i c l e hops between c l u s t e r s
{
if ( i ==0)
{
c = lHop (c , A ) ;
}
if ( i ==1)
{
c = rHop (c , A ) ;
}
}
}
tot += j -1; // j is the number of hops in total , i n c l u d i n g the
hop after the last site was visited
}
printf ( " % f \ n " , tot / M ) ;
}
// F U N C T I O N D E F I N I T I O N S
int ranInt ( int max ) // returns a random integer between 1 and
max
{
return random () % max ;
}
int rHop ( int curCluster , int numClusters ) // i n c r e m e n t s current
cluster with p e r i o di c b o u n d a r i e s
{
if ( curCluster == numClusters -1)
{
curCluster =0;
}
else
{
curCluster ++;
}
return curCluster ;
}
Figure 3: a heat map showing length of path taken for different environments
3.2
Figure 3 shows how both the number of clusters, A, and the number of sites per
cluster, B, determine the path length
Both quantities demonstrate the exponential pattern already observed, although examining the corners of the heat map shows that B contributes more
than A to the length of path taken
However, surprisingly, the graph is almost symmetric about the diagonal,
meaning that A and B contribute a very similar amount
B contributes more than A because, as B grows larger, it becomes less likely
at any moment for the particle to be able to change cluster, and hence inevitably
the full environment
On the other hand, A acts like L in question one, only there is a random
amount of time between hops
10