lsqcurvefit error when fitting polynomial but okay for exponential
5 次查看(过去 30 天)
显示 更早的评论
I am trying to fit an exponential and a polynomial function using lsqcurvefit (must be lsqcurvefit). The code below works.
%x = load('depth-0.txt');
%y = load('mean-0.txt');
%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
x = load('depth-0.txt');
y = load('mean-0.txt');
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.
0 个评论
采纳的回答
John D'Errico
2017-2-7
编辑:John D'Errico
2017-2-7
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));
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Web Services 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!