lsqcurvefit error when fitting polynomial but okay for exponential

7 views (last 30 days)
RuiQi on 7 Feb 2017
Edited: RuiQi on 7 Feb 2017
I am trying to fit an exponential and a polynomial function using lsqcurvefit (must be lsqcurvefit). The code below works.
%F = @(p,x) p(1)*exp(p(2)*(x+p(3)));
%x0 = [0 0 0];
%[p,resnorm] = lsqcurvefit(F, x0, x, y);
%estimate_x = 1:0.1:100;
%estimate_y = p(1)*exp(p(2)*(estimate_x + p(3)));
%plot(estimate_x, estimate_y,'-','LineWidth',2);
But this does not work
F = @(p,x) (p(1) + p(2)*(x*x));
x0 = [-1 1];
[p,resnorm] = lsqcurvefit(F, x0, x, y);
estimate_x = 1:0.1:100;
estimate_y = (p(1) + p(2)*(estimate_x*estimate_x));
plot(estimate_x, estimate_y,'-','LineWidth',2);
I am getting the error below.
Error using *
Inner matrix dimensions must agree.
Error in @(p,x)(p(1)+p(2)*(x*x))
Error in lsqcurvefit (line 199)
initVals.F = feval(funfcn_x_xdata{3},xCurrent,XDATA,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. LSQCURVEFIT cannot continue.

John D'Errico on 7 Feb 2017
Edited: John D'Errico on 7 Feb 2017
It helps if you read your own code. :) Yes, I know things like this are sometimes not obvious.
Here is the block that matters.
F1 = @(p1,x) (p1(1) + p1(2)*(x*x));
x0 = [0 0];
[p1,resnorm] = lsqcurvefit(F, x0, x, y);
Note that you define a function F1, but then call lsqcurvefit with F.
What are the odds that MATLAB will know that you meant differently? Yes, computers are smart, but they do tend to do what you tell them.
Ok, having said that, you should consider that your exponential model does not have a unique solution.
F = @(p,x) p(1)*exp(p(2)*(x+p(3)));
See that if you have ANY solution for [p(1),p(2),p(3)], then there are infinitely many alternative solutions that are equally good. They are all equivalent.
Your three parameter exponential model is actually over-parameterized. Effectively, this means that you will find a solution, but that solution will be completely dependent on your starting values.
So if [p(1),p(2),p(3)] is one solution, then consider that
[p(1)*exp(p(2)*p(3) , p(2) , 0]
is another solution. There are infinitely many other sets of equally good parameters you can find. We can always convert any x-translation for an exponential into a scale factor on the outside of the exponential due to this basic identity for exponentials:
exp(a+B) = exp(A)*exp(B)
So really, you can do no better than to write the model as a TWO parameter model.
F = @(p,x) p(1)*exp(p(2)*x));