IDL HW 2

 

 

1)      Write a function called my_fillarr.pro that generates a sequence of numbers starting at Nmin and not exceeding Nmax, with a spacing bin. For example:

 

IDL> nmin = 0.3

IDL> nmax = 1.4

IDL> bin = 0.25

IDL> n = my_fillarr(bin, nmin, nmax)

IDL> print,n                  

             0.300000  0.550000  0.800000  1.05000  1.30000

 

HINT: This should be done without loops! Instead, make judicious use of finden().

 

2)      Write a function called gaus.pro that accepts an array of x-values along with a three-element vector containing the amplitude, centroid and width, and returns a Gaussian evaluated at each value of x. For example, to create a Gaussian with an amplitude of 2, centered at x=0 and a width of 1.5, you would type:

 

IDL> x = my_fillarr(0.25, -5, 5)

IDL> c = [2, 0, 1.5]

IDL> g = gaus(x, c)

IDL> plot, x, g

 

3)      Read this webpage about using random numbers to estimate the value of pi:

http://polymer.bu.edu/java/java/montepi/MontePi.html

 

This is an example of a Monte Carlo simulation. For this problem, you are going to write an IDL function called mc_pi.pro that returns a value of pi estimated using N random numbers. Here’s an example of how the function should be called:

 

IDL> N = 1000

IDL> pi = mc_pi(N)

IDL> print, pi

      3.17324

 

You will write this program using two methods:

 

a)       Write this function using a FOR loop that generates the random values one at a time and keeps a tally of the number that fall inside the circle in order to estimate pi.

 

b)      Next, modify your code so that the user can set the keyword no_loop, which generates all of the random values at the same time as vectors. randomu() can generate an array of random values, check the help page. Then use a WHERE statement to determine the number of these points that lie within the circle and estimate pi.

 

IDL> pi = mc_pi(N, /no_loop)

 

/no_loop is equivalent to no_loop=1

 

HINT: You’ll need to generate a random array of x-values and a separate random array of y-values.

                   

c)       Download Tim Robishaw’s benchmark.pro (right click this link and save it to your ~/idl/ directory) and use it to determine which version is faster, the one with loops or the one done vector-wise. Use N=1000 in your call to mc_pi, and set the keywords nreps=25, navgs=50 in your call to benchmark, e.g:

 

IDL> benchmark, nreps=25, navgs=50

 

To view the documentation for benchmark.pro, type

 

IDL> doc_library, ‘benchmark’

 

After your test, copy-paste the resulting output from benchmark into a file called benchmark_output.txt. Also, add this line to your .idlenv file:

 

                     setenv IDL_PATH $IDL_PATH{:}+/home/johnjohn/idl/statistics/