"Alex" <firstname.lastname@example.org> wrote in message <email@example.com>... > Hi everyone. I apologize for being yet another sucker posting on this topic. It seems that there are a lot of these questions cluttering this forum already... But unfortunately I couldn't locate an answer in the posts/answers that I've looked through, and I need help to get this assignment completed. > > Good news is I have very little code to show. The background: there is some random x,y data that is generated which already looks like a linear relationship. The task is to make a linear fit to that data using the logarithm of a maximum likelihood function. The standard deviation (variable 'sigma') of the given data is assumed known. Here's my code: > > % initial parameters: > m = 1; > b = 0; > > X = data(:,1); > Y = data(:,2); > p = [m;b]; > f = @(p)normpdf(Y,p(1)*X+p(2),sigma); > f2 = @(p)sum(log(f(p))); > f3 = fminsearch(f2,p); > > (Assume that variable 'data' contains the data I'm trying to fit.) Here f is my likelihood function, calculating the discrepancy between each actual data point and the corresponding point on the linear fit, which is to be adjusted. f2 sums the logs, and then I want to minimize this sum to get the parameters for the linear fit, m and b. > > Running this I always get a notice: > "Exiting: Maximum number of function evaluations has been exceeded > - increase MaxFunEvals option. > Current function value: -Inf" > > And the parameters I get are not good at all. What is wrong here? Any help greatly appreciated!
fminsearch determines the minimum of a function while search for its maximum. Thus your objective function has to be f2 = @(p)(-sum(log(f(p))));