Why does the Curve Fitting Toolbox use random start points for custom nonlinear equations, and the fit fail to converge to the expected curve?
I’m working with some data that I want to fit to a custom non-linear expression of the form `a * g(x)` (e.g., g(x) = x^(3/2)), but I’m having a lot of trouble getting the Curve Fitting Toolbox to produce a good fit. When I use the Curve Fitting Tool, the fitted curve is often way off on the first try. If I keep re-selecting one of my variables, the constant (a) changes each time, and after doing this a few times, the curve sometimes gets closer to my data. I don’t understand why the tool doesn’t fit the best curve right from the start.
When I use curve fitting functions like `lsqcurvefit` or `fit`, the resulting curve usually just reflects the initial guess or starting value for the coefficient - even if that doesn’t fit my data well.
Expected Behavior:
I expect the Curve Fitting Tool or the fitting functions to produce a curve with a high R-square value (close to 1) on the first try, and not to return a curve that simply echoes the initial guess for the coefficient.
Reproduction Steps and What I’ve Tried:
- Loaded my data into the Curve Fitting Tool after cropping it in a MATLAB script
- Selected a custom equation: `a * g(x)`
- Used default fit options (also tried increasing `MaxFunEvals` and `MaxIter` to 2000)
For script-based fitting, I tried:
>> model = @(a, x) a .* g(x); % e.g., g(x) = x.^(3/2);
>> a0 = 0.3486; % initial guess
>> a_fit = lsqcurvefit(model, a0, ind_indReg, f_indReg);
>> y = model(a_fit, ind_indReg);
Or using the fit function:
>> ft = fittype('a*g(x)', 'independent', 'x', 'dependent', 'y');
>> opts = fitoptions('Method', 'NonlinearLeastSquares');
>> opts.Display = 'Off';
>> opts.MaxFunEvals = 2000;
>> opts.MaxIter = 2000;
>> opts.StartPoint = 2; % also tried other values
>> [fitresult, gof] = fit(xData, yData, ft, opts);
No matter what I try, the fit is either poor or just reflects the initial guess for 'a', and I’m not sure why the fitting process doesn’t work as expected.
Accepted Answer
More Answers (0)
Categories
Find more on Get Started with Curve Fitting Toolbox 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!