MATLAB: Error in nonlinear regression

nonlinear regressionregresssion

I am trying to make a nonlinear regression model of the attached csv file. I used the following code:
if true
data=readtable('ban1.csv')
sea=data(:,1)
sst=data(:,2)
at=data(:,3)
X = [sst,at];
y = sea;
modelfun = @(b,x)b(1) + b(2)*x(:,1).^b(3) + ...
b(4)*x(:,2).^b(5)
beta0 = [100 100 100 100 100];
mdl = fitnlm(X,y,modelfun,beta0)
% code
end
I am getting the following errors:
Error using internal.stats.parseArgs (line 42) Wrong number of arguments.
Error in NonLinearModel.fit (line 1385) internal.stats.parseArgs(paramNames, paramDflts, otherArgs{:});
Error in fitnlm (line 99) model = NonLinearModel.fit(X,varargin{:});
Would be grateful if someone can help me, if possible with code. I would also like to know where I am going wrong.
Thank you

Best Answer

You are not extracting your table variables correctly.
This works (extracting the data from your table):
X = [data.sst, data.at];
y = data.sea;
modelfun = @(b,x)b(1) + b(2)*x(:,1).^b(3) + ...
b(4)*x(:,2).^b(5)
beta0 = [100 100 100 100 100];
mdl = fitnlm(X,y,modelfun,beta0)
and so does this (using your table, and simply rearranging its columns to be compatible with what fitnlm expects):
new_table = data(:, [2 3 1]);
modelfun = @(b,x)b(1) + b(2).*x(:,1).^b(3) + ...
b(4).*x(:,2).^b(5)
beta0 = [100 100 100 100 100];
mdl = fitnlm(new_table,modelfun,beta0)
The ‘carbig’ model is likely not appropriate for your sea level and temperature data, so I am not surprised that the model fails, even though the code now runs without error.