MATLab: Not enough inputs for nlinfit

  • Context: MATLAB 
  • Thread starter Thread starter TaylorLL
  • Start date Start date
  • Tags Tags
    Matlab Matlab code
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting an issue with the MATLAB function nlinfit, specifically regarding the input arguments for an anonymous function used in fitting a model to data. Participants explore the nature of the error encountered and share insights on the correct usage of nlinfit.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes an error encountered when using nlinfit, indicating that the anonymous function does not have enough input arguments.
  • Another participant asks for clarification on the definition of nlinfit and the anonymous function, suggesting that nlinfit is part of MATLAB's statistics toolbox.
  • Participants inquire about the specific error message from MATLAB, seeking to understand the nature of the problem.
  • A participant shares a link to the MATLAB documentation for nlinfit, suggesting that reviewing examples may help identify differences in the code.
  • One participant reports resolving the issue by modifying the test function to accept only two inputs: a vector of coefficients and the independent variable.
  • Another participant suggests debugging strategies, such as commenting out lines of code to isolate the source of the error.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the initial cause of the error, but there is agreement on the importance of understanding the input requirements for nlinfit. The discussion includes both troubleshooting strategies and a successful resolution by one participant.

Contextual Notes

The discussion highlights limitations in understanding the function's requirements and the potential for confusion when using anonymous functions in MATLAB. Specific assumptions about input formats and function definitions are critical to resolving the issue.

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   Reactions: TaylorLL

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 11 ·
Replies
11
Views
5K
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
15K