Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
Drexel University or The Math Forum.


Torsten
Posts:
1,626
Registered:
11/8/10


Re: Trying to get linear fit using fminsearch. Problem: function goes to Inf and bad parameter results
Posted:
Oct 14, 2013 6:21 AM


"Alex" <storm_ryder16@yahoo.com> wrote in message <l3g5ai$ptq$1@newscl01ah.mathworks.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))));
Best wishes Torsten.



