MATLab: Not enough inputs for nlinfit

  • Context: MATLAB 
  • Thread starter Thread starter TaylorLL
  • Start date Start date
  • Tags Tags
    Matlab Matlab code
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
9 replies · 3K views
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
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.
 
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.