Iteration limit exceeded - nlinfit for non-linear regression

3 views (last 30 days)
Hi, I'm performing a nonlinear regression using nlinfit, and I keep getting the warning "iteration limit exceeded". I need to find 3 unknowns which I get but the same error is displayed each time. The function is y = C*(L)^m*(G)^n. I've also attached the code below for reference.
clearvars; close all; clc;
z = [87.1446607 94.47343938 99.51904391 108.3315307 134.1848125]'; %Ky_a
x = [2.008969 2.012897 1.999 2.008 2.0092]'; %L
y = [1.883907496 2.04141846 2.187417647 2.461654149 2.617206617]'; %G
XY = [x y];
modelfun = @(b,XY)(b(1).*((XY(:,1).^b(2)).*(XY(:,2).^b(3)))); % C*((L^m)*(G^n))
% C, m and n here are b(1), b(2), and b(3)
beta0 = [0.5;0.5;0.5]; % initial guesses for b(1), b(2) and b(3)
%function below finds the value of the unknowns and finds
%residuals, MSE, jacobian matrix and covariance matrix
[beta,R,J,CovB,MSE,ErrorModelInfo] = nlinfit(XY,z,modelfun,beta0);

Answers (1)

Star Strider
Star Strider on 10 Mar 2022
I used fitnlm here instead, since it outputs all the statistics as well as the parameter estimated. (It calls nlinfit so is essentially the same.)
z = [87.1446607 94.47343938 99.51904391 108.3315307 134.1848125]'; %Ky_a
x = [2.008969 2.012897 1.999 2.008 2.0092]'; %L
y = [1.883907496 2.04141846 2.187417647 2.461654149 2.617206617]'; %G
XY = [x y];
modelfun = @(b,XY)(b(1).*((XY(:,1).^b(2)).*(XY(:,2).^b(3)))); % C*((L^m)*(G^n))
% C, m and n here are b(1), b(2), and b(3)
beta0 = [0.5;0.5;0.5]; % initial guesses for b(1), b(2) and b(3)
opts = statset('MaxIter',5000); % Tell It To Keep Iterating Until It Converges
mdl = fitnlm(XY,z,modelfun,beta0,'Options',opts) % Fit Function, Dispaly Tesults
mdl =
Nonlinear regression model: y ~ (b1*((x1^b2)*(x2^b3))) Estimated Coefficients: Estimate SE tStat pValue ________ _______ ________ ________ b1 0.25048 2.8795 0.086987 0.93861 b2 7.2317 16.492 0.43849 0.70385 b3 1.233 0.29945 4.1174 0.054233 Number of observations: 5, Error degrees of freedom: 2 Root Mean Squared Error: 8.34 R-Squared: 0.895, Adjusted R-Squared 0.79 F-statistic vs. zero model: 269, p-value = 0.00371
[ze,zeci] = predict(mdl, XY);
figure
stem3(x, y, z, 'filled')
hold on
plot3(x, y, ze)
plot3(x, y, zeci, '--k')
hold off
xlabel('x')
ylabel('y')
zlabel('z')
legend('Data','Function Fit','Confidence Intervals', 'Location','best')
.

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!