MATLAB MATLab: Not enough inputs for nlinfit

AI Thread Summary
The discussion revolves around issues encountered while using the nlinfit function in MATLAB to fit a model to generated EQ data. The user initially faces an error related to insufficient input arguments for the anonymous function defined as "test." The error message indicates that the function is not receiving the expected number of inputs. It is clarified that nlinfit requires the test function to accept only two inputs: a vector of coefficients and the independent variable. After reviewing the documentation, the user successfully modifies the test function to conform to this requirement, allowing nlinfit to execute without errors. The conversation highlights the importance of understanding function input requirements and the need for careful debugging in programming.
TaylorLL
Messages
8
Reaction score
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
Where is nlinfit defined? You mentioned it was anonymous.
 
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.
 
And what is the error? Can you post what Matlab said?
 
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.
 
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!
 
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);
 
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

Similar threads

Replies
4
Views
1K
Replies
5
Views
2K
Replies
1
Views
2K
Replies
1
Views
1K
Replies
4
Views
2K
Replies
8
Views
3K
Replies
2
Views
15K
Back
Top