MATLab: Not enough inputs for nlinfit

In summary, when using nlinfit, the test function can only take TWO inputs: a vector containing the coefficients, and the independent variable. If you want to use the test function to generate an equation for a response variable, you need to pass in the response variable as one of the inputs.
  • #1
TaylorLL
8
0
Hello! I'm trying to plot a best fit for some generated EQ data and I'm having issues with nlinfit. My code is as follows and when I try to run it, I get an error referring to the anonymous function ("test"), nlinfit (from Matlab), and not having enough input arguments.

Code:
%Test function
    t = t';
    y = y';
    test = @(t0,k,phi) (t-t0).*sin(k.*(t-t0)+phi).*(t>=t0)...
                      +  (t0).*sin(k.*t0    +phi).*(t< t0);
%Need to guess t0 - arrival time, k - wavenumber, phi - offset
    guess = [6 3 0];
%nlinfit
    coef_inv = nlinfit(t,y,test,guess);

Both t and y are 121x1 column vectors.
 
Last edited:
Physics news on Phys.org
  • #3
jedishrfu said:
Where is nlinfit defined? You mentioned it was anonymous.
nlinfit is a function from the statistics toolbox offered by Matlab. The anonymous function is the "test" function.

EDIT: I realize my phrasing was confusing, but it was a list of three separate things.
 
  • #5
jedishrfu said:
And what is the error? Can you post what Matlab said?

Code:
Error using nlinfit (line 205)
Error evaluating model function
'@(t0,k,phi)(t-t0).*sin(k.*(t-t0)+phi).*(t>=t0)+(t0).*sin(k.*t0+phi).*(t<t0)'.

Error in HW6_Part2_Lonner (line 28)
    coef_inv = nlinfit(t,y,test_fcn,guess);

Caused by:
    Not enough input arguments.
 
  • #7
jedishrfu said:
Here’s the Matlab doc on nlinfit

https://www.mathworks.com/help/stats/nlinfit.html?s_tid=gn_loc_drop

Try some of the examples they show and then try to see how your code is different. Software is very pedantic in this regard.

I've already looked at the documentation and I've done examples in class. I cannot see where my error is coming from.

EDIT: Sorry for being snippy, thank you!
 
  • #8
Okay! I figured it out (thanks to looking over the documentation again haha). When using nlinfit, the test function can only take TWO inputs: a vector containing the coefficients, and the independent variable. Here's my working code:

Code:
%Test function
    t = t';
    y = y';
    test = @(coef,t) (t-coef(1)).*sin(coef(2).*(t-coef(1))+coef(3)).*(t>=coef(1))...
       
%Need to guess t0 - arrival time, k - wavenumber, phi - offset
    guess = [6 3 0];
%nlinfit
    coef_inv = nlinfit(t,y,test,guess);
 
  • #9
I don't see it offhand either. This is where programmers do little hacks to find cause of the error or errors.

Try commenting out the nlinfit line. If the error goes away then you know it’s that line.

Next try subbing in known arts or even a simple nlinfit call to see that nlinfit does work and that Matlab is referencing the right function.

As a programmer you can’t assume anything until you test and eliminate it or recognize your error right away.
 
  • #10
That’s great, you’ve just been inducted into the ranks of programmer-dom.
 
  • Like
Likes TaylorLL

1. Why am I getting the error "Not enough inputs for nlinfit" in MATLAB?

This error occurs when the nlinfit function in MATLAB is not given enough input arguments. This function requires at least three input arguments: the model function, initial parameter guesses, and the data points. If any of these inputs are missing, this error will be displayed.

2. How can I fix the "Not enough inputs for nlinfit" error in MATLAB?

To fix this error, make sure you have provided all necessary input arguments for the nlinfit function. Check that you have supplied the correct model function, initial parameter guesses, and data points. If you are still getting the error, try redefining your inputs to make sure they are in the correct format.

3. Can I use fewer input arguments for nlinfit in MATLAB?

No, the nlinfit function in MATLAB requires at least three input arguments. This is because it is a nonlinear least squares fitting function, which requires a model function, initial parameter guesses, and data points to perform the fitting process.

4. What is the purpose of nlinfit in MATLAB?

The nlinfit function in MATLAB is used for nonlinear least squares fitting. It takes a model function, initial parameter guesses, and data points as inputs and returns the best-fit parameters for the model function. This is useful for finding the optimal parameters for a nonlinear model that best fits a set of data points.

5. Are there any alternatives to using nlinfit in MATLAB?

Yes, there are alternative functions in MATLAB for nonlinear fitting, such as lsqcurvefit and lsqnonlin. These functions have different algorithms and may be better suited for certain types of data or models. It is recommended to compare the results from different fitting functions to ensure the best fit for your data.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
941
  • Introductory Physics Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
943
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
829
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
Back
Top