Iteration limit exceeded - nlinfit for non-linear regression
3 views (last 30 days)
Show older comments
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);
0 Comments
Answers (1)
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
[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')
.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!