MATLAB: Help to find the mistake in the code for nonlinear regression, or tell me what’s wrong

nonlinear regression

Hi, I have a matrix which includes 29 rows and 11 columns, the first 10 columns are the predictors and the 11th columns is the response to be modeled. I have a code for nonlinear regression with a cubic function as u can find in the following code. but the error is AMAZING. I have tested the function with feval and the result is a 29*1 matrix as it must be. Can anyone tell me what is wrong with my code? any help would be greatly appreciated
clear
cd('d:/')
A=xlsread('book1.xls');
X=A(:,1:10);
Y=A(:,11);
modelfun=@(x,b)b(1)*x(:,1).^2+b(2)*x(:,2).^2+b(3)*x(:,3).^2+(b(4)*x(:,4).^2+b(5)*x(:,5).^2+b(6)*x(:,6).^2+b(7)*x(:,7).^2+b(8)*x(:,8).^2+b(9)*x(:,9).^2+b(10)*x(:,10).^2+b(11));
beta0=[ -1 -1 +1 -1 -1 -1 -1 -1 -1 -1 -1]
mdl= fitnlm(X,Y,modelfun,beta0)
and the result is the following error:
Error using nlinfit (line 211)
MODELFUN must be a function that returns a vector of fitted values the same size as Y (29-by-1). The model
function you provided returned a result that was 1-by-1.
One common reason for a size mismatch is using matrix operators (*, /, ^) in your function instead of the
corresponding elementwise operators (.*, ./, .^).
Error in NonLinearModel/fitter (line 1122)
[model.Coefs,~,J_r,model.CoefficientCovariance,model.MSE,model.ErrorModelInfo,~] = ...
Error in classreg.regr.FitObject/doFit (line 220)
model = fitter(model);
Error in NonLinearModel.fit (line 1430)
model = doFit(model);
Error in fitnlm (line 94)
model = NonLinearModel.fit(X,varargin{:});
Error in sand_regression (line 11)
mdl= fitnlm(X,Y,modelfun,beta0)

Best Answer

This is not even a nonlinear regression anyway! It is linear in the parameters. So use any tool for linear regression, perhaps regress from the stats toolbox, or lsqr, lscov, or even just backslash. For example...
n = size(X,1);
beta = [X.^2,ones(n,1)]\Y;