% The percent symbol is the comment
symbol in MATLAB.
NUM_SAMPLES = 1000; % The number of observations that we'll take
from a probability function.
NUM_TRIALS = 100; % We'll take this number of observations
a bunch of times to see how our estimates change.
TRUE_MEAN = -50; % The true value of the mean.
TRUE_STD = 5; % The true value of the standard deviation.
mhist=zeros(1,NUM_TRIALS); % Create a vector (initialized to
zeros) where we can store estimates of the mean.
shist=zeros(1,NUM_TRIALS) ; % Create a vector (initialized to
zeros) where we can store estimates of the std.
% Type help zeros to learn about this
MATLAB function.
for (i=1:NUM_TRIALS) % In MATLAB, we don't have to declare a
variable before using it.
% We also needn't specify our increment size ---
it's always 1.
% Rather than using {} a la C++,
we use for () ... end;
x=TRUE_STD * randn(1,NUM_SAMPLES)+TRUE_MEAN; %
Take a sample from our trud distribution.
m = mean(x); % Use MATLAB's "mean" function to
estimate the true mean using the data.
s = std(x); % Use MATLAB's "std"
function to estimate the true standard deviation using the data.
% Print out a progress bar and learn how to
use print statements in MATLAB
fprintf(1,'Iteration=%d\n',i);
fprintf(1,'\tTrue mean = %d, estimated mean =
%.2f\n',TRUE_MEAN,m); % Notice the C++-like syntax.
fprintf(1,'\tTrue std = %d, estimated std =
%.2f\n\n',TRUE_STD,s); % Notice the C++-like syntax.
% Put the estimates from this trial into our vector.
mhist(1,i)=m; % The estimate from this, the
ith trial, goes in the ith location of our history vector.
shist(1,i)=s;
end; % End the for loop.
subplot(2,1,1); % Allows us to put two plots into the same figure
window. Type "help subplot."
hist(mhist);
subplot(2,1,2); % Put the history of the standard deviation into the
other plot in the figure.
hist(shist);