Not enough input arguments error in nlinfit

10 views (last 30 days)
Hello all,
I'm quite new to matlab and I'm trying to fit a non linear model to my data (I have 3 independent variables, 4000+ data points)
I have written the following code after following few you tube videos and matlab documents. Result is a 4000*4 matrix that has my three independent variables and the dependent variable
x=Result.x;% independent variable I
y=Result.y;% independent variable II
z=Result.z;% independent variable III
p=Result.fx; % dependent variable
inputs=[x,y,z];
my_fun = @(beta,x,y,z) (x.^beta(1) * y.^beta(2) / z.^beta(3)); % assumed model
initials = [1,2,1];
new_coeff = nlinfit(inputs,p,my_fun,initials);
However, I get the following error when I run this code
Error using nlinfit (line 205)
Error evaluating model function '@(beta,x,y,z)(x.^beta(1)*y.^beta(2)/z.^beta(3))'.
Error in Untitled5 (line 11)
new_coeff = nlinfit(inputs,p,my_fun,initials);
Caused by:
Not enough input arguments.
Can someone help me? Why does it say not enough input parameters?

Accepted Answer

Star Strider
Star Strider on 31 May 2018
Try this:
inputs = [x(:),y(:),z(:)];
my_fun = @(beta,inputs) (inputs(:,1).^beta(1) .* inputs(:,2).^beta(2) ./ inputs(:,3).^beta(3)); % assumed model
That should work with this nlinfit call:
new_coeff = nlinfit(inputs,p,my_fun,initials
(I cannot test your code.)
  2 Comments
Awanthika Senarath
Awanthika Senarath on 31 May 2018
Edited: Awanthika Senarath on 31 May 2018
Thanks, this was the error. I had passed too many arguments. Matlab seriously needs to re-do their error messages!

Sign in to comment.

More Answers (2)

Darren Wethington
Darren Wethington on 31 May 2018
When passing arguments to nlinfit, you've (correctly) passed your inputs as one variable, "inputs". Now my_fun will take "initials" as the argument "beta", "inputs" as the argument "x", and... no input argument for "y" or "z".
Try changing my_fun to take two arguments, "beta" and "data" for instance. Then specify in my_fun that x=data(1,:), y=data(2,:), etc (or whatever makes sense for your data). Hopefully this helps.

Awanthika Senarath
Awanthika Senarath on 31 May 2018
I solved it, the model_fun only takes two parameters. I changed it as follows and now it works.
my_fun = @(beta,Results) (Result.x.^beta(1) .* Result.y.^beta(2) ./ Result.z.^beta(3));

Community Treasure Hunt

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

Start Hunting!