%% Physics 233, MATLAB Exercises for Hughes and Hase: Chapter 1
% MATLAB activities to complement _Measurements and their Uncertainties_,
% Chapter 1, "Errors in the physical sciences."
%
% Author: J. S. Dodge, 2016

%% MATLAB basics
% Review the following documentation pages to familiarize yourself with the
% basic elements of the MATLAB language and integrated development
% environment (IDE).
% 
% * <http://www.mathworks.com/help/matlab/matlab_env/enter-statements-in-command-window.html Enter Statements in Command Window>
% * <http://www.mathworks.com/help/matlab/matlab_env/command-history.html Command History>
% * <http://www.mathworks.com/help/matlab/examples/basic-matrix-operations.html Basic Matrix Operations> 
% * <http://www.mathworks.com/help/matlab/examples/matrix-manipulation.html Matrix Manipulation>
% * <http://www.mathworks.com/help/matlab/examples/creating-2-d-plots.html Creating 2-D Plots>
% * <http://www.mathworks.com/help/matlab/creating_plots/plotting-tools--interactive-plotting.html Customize Graph Using Plot Tools>
% 

%% Random number generation
% We'll use MATLAB's random number generation capabilities to simulate
% experimental errors. The MATLAB function
% <http://www.mathworks.com/help/matlab/ref/rand.html |rand|> returns
% random numbers that are uniformly distributed over (0,1), and the
% function <http://www.mathworks.com/help/matlab/ref/randn.html |randn|>
% returns random numbers that are normally distributed with zero mean and
% unit variance. Both functions will return a *square* |N x N| array if
% given a single input |N|.
rand(3)
randn(3)

%%
% Frequently you will want a one-dimensional *vector* of random numbers,
% and to do this you just provide the size of the array in | row, col |
% order.
rand(3,1)
rand(1,3)

%%
% Now use the output from the two different functions to make histograms
% using the MATLAB <http://www.mathworks.com/help/matlab/ref/histogram.html
% |histogram|> function, and examine the distributions.
close all       % Close any existing plots
hist(rand(1000,1),20)
figure
hist(randn(1000,1),20)

%%
% RNGs are wonderful tools for learning about statistics, simulating
% experiments, and even doing certain types of theoretical calculations.
% You might wonder how a computer, a completely deterministic machine, can
% generate random numbers. The short answer is that it does not; the
% functions use algorithms that are cleverly designed to produce numbers
% that satisfy most statistical requirements associated with randomness,
% but actually follow a well-defined sequence. If this sounds strange, you
% are not alone: the great 20th century mathematician John von Neumann
% wrote, "Anyone who considers arithmetical methods of producing random
% digits is, of course, in a state of sin."
% <https://en.wikipedia.org/wiki/Pseudorandom_number_generator#cite_note-3
% [J. von Neumann, NBS Applied Mathematics Series, (1951).]> Technically,
% the numbers produced by |rand| and |randn| are called *pseudorandom*
% numbers to distinguish them from their purer counterparts. A brief review
% of the algorithm used for |rand| is available in the short article
% <http://www.mathworks.com/company/newsletters/news_notes/pdf/Cleve.pdf
% "Random Thoughts"> by Cleve Moler; a more comprehensive and mathematical
% review of the subject is available in Don Knuth's
% <http://troy.lib.sfu.ca/record=b6414758~S1a *The Art of Computer
% Programming, volume 2: Seminumerical Algorithms*> , among others.

%%
% Note that every call to |rand| generates different numbers. If you
% restart MATLAB and enter the same commands, however, the sequence of 24
% numbers generated by these commands will be the identical to those that
% you just obtained. Write a few of them down, and try it.

%%
% The pattern repeats after a restart because MATLAB begins each new
% session with its RNGs in the same state. Every call to the RNG after that
% changes the state in a repeatable way, but the sequence after that
% appears to be random. The initial state of both |rand| and |randn| is
% determined by a number that is commonly called the "seed" of the RNG,
% which can be controlled using the
% <http://www.mathworks.com/help/matlab/ref/rng.html |rng|> function. If
% you do simulations that employ random numbers, it is useful to initialize
% the seed directly so that you can reproduce your simulations.
rand('seed',0)
% rng('default')  % Restore RNG to default
% rng(0)          % Set the seed to zero and show random numbers
rand(1,3)
rand(1,3)

%%
rand('seed',0)
%rng(0)          % Reset the seed and do it again
rand(1,3)
rand(1,3)

%%
rand('seed',1)
%rng(1)          % A different seed leads to different numbers
rand(1,3)
rand(1,3)

%%
% $Id: P233HH1.m 150 2016-02-02 09:07:14Z physcmpt $