Um, don't use fminsearch? In fact, I'm not sure why you would want to do it that way.
d = 17;
n = 10000;
Now, estimate d. For this simple problem, diff and std are about all you really need.
dhat = std(diff(kk))/sqrt(2)