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); %don't set a seed. doing so will give the same answer in each loop. x = normrnd(mu,sqrt(sigma2),m,n); xbarvec(1,1) = mean(x,1); s2vec(1,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(1,1:2) = theta5g; theta_test = [1.07, 3.01]; lnnormlike(theta_test,x)