Why does the Curve Fitting Toolbox use random start points for custom nonlinear equations, and the fit fail to converge to the expected curve?

23 views (last 30 days)

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

MathWorks Support Team
MathWorks Support Team on 30 Jul 2025
1. Start Points for Custom Equations: 
By default, when using custom equations, the Curve Fitting Toolbox assigns random start points between 0 and 1 for all coefficients (this is indicated as "Random" in this table). Each time a new 'fittype' is selected in the app, a new random value is chosen unless a specific start point is set using `fitoptions` at the command line.
If a start point is not provided, you will see a warning message: 
"Start point not provided, choosing random start point."
Since users can enter any custom equation with any data, it is not possible for the software to predict suitable start values. Therefore, we recommend that users - who are most familiar with their data and the context of their model, provide reasonable initial guesses for start points whenever possible. 
2. Solution Convergence and Coefficient Behavior
The issue that the final solution does not change much from the original starting point - this is not uncommon for nonlinear custom equations. There are several reasons why this can occur, including the nature of the data, the model, and the solver's stopping criteria. In some cases, setting lower and upper bounds for the coefficients (for example, specifying that a must fall between 1 and 2) can help guide the solver toward a better solution.
3. Expectations for Curve Fit Quality
While the app or command line often produces a reasonable curve fit on the first try, this is not guaranteed. The fit quality depends on the relationship between your data and the chosen equation. From the data and equation, you provided, the optimal value for a (for the example with g(x) = x^(3/2)) appears to be around 0.5175, but the fit may not closely match your data, even if the R² value is relatively high (e.g., around 0.95). Ultimately, it is up to you to determine if the fit meets your needs.
4. Data Scaling and Normalization
It appears that the scale of your data may be influencing the fitting process. Typically, it is advisable to center and scale the data by enabling the "Normalize" fit option. However, for custom equations in the app, this option is not available, and enabling normalization at the command line for your equation will result in an error. This is because normalization would map your x data onto [-1, 1], which can result in complex numbers when raised to the 3/2 power (in this example) or based on the non-linear equation you have.
Regarding the residuals, from the output struct (3rd output argument of fit), the "firstorderopt" may give some information about that (link to equivalent Optimization Toolbox page).
5. Investigating the Iterative Process
If you are interested in the iterative fitting process, you can set the "Display" fit option to "iter" (command line only), which will print diagnostic information in the Command Window. Increasing `MaxFunEvals` and `MaxIter` only helps if the solver stops due to reaching these limits. In your case, only two function evaluations occurred, so increasing these limits does not affect the result.
Possible Workarounds:
1. Use a Linear Custom Equation: 
Although your equation is nonlinear in x, it is linear in the coefficient a. Custom linear equations in the Curve Fitting Toolbox do not require start points and generally have a unique solution. You can fit your data at the command line as follows:
>> fit(ind_Lim, f_Lim, {'x^(3/2)'}) %replace x^(3/2) with your custom equation ans = Linear model: ans(x) = a*x^(3/2) Coefficients (with 95% confidence bounds): a = 0.5175 (0.4869, 0.5481)
2. If You Prefer a Nonlinear Custom Equation:  
You may try manually scaling your data or adjusting your equation so that the coefficients are more compatible with the fitting process. For example, artificially scaling both *x* and *y* can sometimes yield consistent coefficient results, even without specifying start points:
>> fit(1e7*ind_Lim, 1e9*f_Lim, 'a*x^(3/2)') %replace x^(3/2) with your custom equation Warning: Start point not provided, choosing random start point. > In curvefit.attention/Warning/throw (line 30) In fit>iFit (line 355) In fit (line 117) ans = General model: ans(x) = a*x^(3/2) Coefficients (with 95% confidence bounds): a = 0.01636 (0.0154, 0.01733)
Lastly, as mentioned before, specifying lower and upper bounds in the fit options can help the solver converge to an appropriate solution.
  1 Comment
Alex Sha
Alex Sha on 28 Sep 2025
@MathWorks Support Team Does MathWorks have a plan to develop or introduce optimization algorithms similar to 1stOpt? The algorithm in 1stOpt basically eliminates the need for users to blindly guess initial start-values while still being able to obtain the optimal solution.

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!