clc; close all; clear all; %where you put the lnnormlike.m file addpath('U:\Public\html\files\matlab\functions'); m = 500; n = 1; mu = 2; sigma2 = 2; %initialize our matrices (fill them with 999 so we can easily see if the %values are replaced). xbarvec = 999*ones(100,1); s2vec = 999*ones(100,1); thetamat = 999*ones(100,2); for i = 1:1:100 %don't set a seed. doing so will give the same answer in each loop. x = normrnd(mu,sqrt(sigma2),m,n); xbarvec(i,1) = mean(x,1); s2vec(i,1) = sum((x-mean(x,1)).^2)/m; x0 = [1.95, 2.05]; %initial guess for the minsearch function %returns the MLE estimates of mu and sigma^2 in a 2x1 matrix theta5g = fminsearch(@(theta) lnnormlike(theta,x), x0); %save the estimates of each loop in a matrix thetamat(i,1:2) = theta5g; end; %average our numerical estimates meanxbarvec = mean(xbarvec); means2vec = mean(s2vec); meanthetamat = mean(thetamat); %calculate the variance of our numerical estimates varxbarvec = var(xbarvec); vars2vec = var(s2vec); varthetamat = var(thetamat);