Practice
Check whether the MATLAB rand function produces a uniform distributed random number in the range 0-1. (This can be done by writing a script or by executing the appropriate commands at the MATLAB command line ... about 4 lines required.)
The way I usually do this check is to create a histogram of the results of the random trials. For this case, for example, you might consider a histogram with 10 bins of width 0.1 spanning the range from 0 to 1. That is, the first bin collects all trial samples with values between 0 and 0.1, the second covers 0.1 to 0.2 and so on. We'd expect, for N trials, to see N/10 events in each bin if the random numbers are uniformly distributed.
Hint:The hist function can be used to create a histogram in MATLAB. According to MATLAB help the usage is: H = HIST(Y,X) returns a vector H with the distribution of input vector Y among bins with centers specified by another vector X. (Note: the first bin includes data between -inf and the first center and the last bin includes data between the last bin and inf.)
Hint 2:You can make a nice bar graph of the histogram using the bar( function in MATLAB. The hist function itself returns a bar graph if the result vector is not assigned.)
Assignment
Write a script to perform a numerical integration using the Monte Carlo method. The specific integral to be evaluated is:
It is useful and instructive to have the user enter the number of
trial points to be used as an input to the program.
Since this integral has a known value, your program should output both
the Monte Carlo solution and the error as results.
Extra Credit
The accuracy of the Monte Carlo Integration depends on the number
of trial points that are used. Make a quantitative comparison of the
error in the Monte Carlo approximation to the integral and the
number of trial points used. You should be able to demonstrate
that the error decreases as (1/N)1/2, where N is the
number of trial points. Prepare a graph of the error in the integral
versus number of trial points to demonstrate this and submit the graph
for extra credit consideration.
Copy your completed program into the directory
~schloerb/ph281/username,
where username is your user name.
See this link for examples of how to
copy a file to this directory.
If you do the extra credit calculation, please submit the plot
requested.
Please write your name in a comment field
near the beginning of the script.