lsqcurvefit error when fitting polynomial but okay for exponential
Show older comments
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.
Accepted Answer
More Answers (0)
Categories
Find more on Nonlinear Least Squares (Curve Fitting) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!