Troubleshooting nlinfit: Using Sinc^2 Function in MATLAB

In summary: The exact error message when I run the first file is:Error using ==> beta at 21Not enough input arguments
  • #1
Mindscrape
1,861
1
I'm trying to use the nlinfit that MATLAB has to fit a sinc^2 function, but it keeps telling me that my coefficients have the wrong inputs. Here's basically what I am doing.

%driver.m
y = [intensity points]; %I actually have a bunch of intensity points, but I don't want to list them
x = [data points]; %data points relating to intensity
plot(x,y,'o')
hold on
[beta,r,J,sigma] = nlinfit(x,y,@sincsq, beta);
beta
r
J
sigma
plot(x,sincsq(beta,x),'-k')

and I have the function sincsq defined as
%sinc^2 model for passing to the nlinear fit

function yhat = sincsq(beta,x)

yhat = beta(1).*sinc(beta(2).*x(1,:)).^2;

The exact error message when I run the first file is:
Error using ==> beta at 21
Not enough input arguments
 
Physics news on Phys.org
  • #2
Your problem is that 'beta' is a MATLAB function. It's mad that you are trying to use it as a variable. I used to have this problem a lot so now I always use 'bayta' for variables I want to name beta.
 
  • #3
Okay, so I fixed that problem, but now I have another. The nlinfit is being really weird though, and changing the starting coefficients, which should be pretty close to ones it determines in the end, to stuff that isn't anywhere close.

Here is my exact code:
clear all

%input data
y=[.002 .007 .014 .019 .018 .011 .005 .002 .007 .016 .022 .021 .015 .006...
.002 .008 .016 .025 .025 .019 .009 .003 .006 .016 .028 .032 .024 .012...
.004 .006 .019 .034 .040 .034 .019 .006 .006 .020 .041 .054 .050 .050...
.032 .012 .005 .020 .048 .075 .076 .055 .024 .006 .017 .058 .096 .114...
.112 .096 .051 .013 .015 .064 .133 .179 .171 .129 .037 .017 .085 .204 ...
.340 .378 .294 .147 .038 .121 .446 .757 1.200 1.189 .788 .311 .220 1.187...
3.924 7.56 2.24*4 2.884*4 3.654*4 3.943*4 3.841*4 3.601*4 2.515*4 ...
2.515*4 2.236*4 7.892 3.430 1.685 .454 .344 .635 1.176 1.303 1.202 ...
.760 .469 .113 .038 .155 .284 .492 .471 .236 .092 .015 .048 .168 ...
.242 .238 .173 .064 .015 .013 .061 .098 .121 .112 .096 .062 .021 ...
.021 .006 .027 .049 .078 .076 .048 .023 .005 0.0120 0.0320...
0.0500 0.0500 0.0540 0.0410 0.0200 0.0060 0.0060 0.0190 0.0340 0.0400...
0.0340 0.0190 0.0060 0.0040 0.0120 0.0240 0.0320 0.0280 0.0160...
0.0060 0.0030 0.0090 0.0190 0.0250 0.0250 0.0160 0.0080 0.0020...
0.0060 0.0150 0.0210 0.0220 0.0160 0.0070 0.0020 0.0050 0.0110...
0.0180 0.0190 0.0140 0.0070 0.0020];
n = length(y);
x = 0:.5:(n-1)*.5;
plot(x,y,'o', 'MarkerSize',2.5)
hold on

%initialize coefficients
A=3.943*4;
x0=pi*.25*10^-3/(.633*10^-6);
shift = x(1,ceil(length(x)/2));
v=[A,x0,shift];

%plot start guess
vStart = v;
yStart=sincsq(vStart,x);
plot(x,yStart,'r-');
hold on

%plot sinc^2
sinc2 = A.*sinc(x0.*(x-shift)).^2;
plot(x,sinc2,'y-')
hold on

%use nlinfit
[vEnd,r,J,cov,sigma] = nlinfit(x,y,@sincsq, vStart);
sigma
yEnd=sincsq(vEnd,x);
plot(x,yEnd,'k-')

Here is the sincsq function:
%sinc^2 model for passing to the nlinear fit

function [yhat] = sincsq(v,x)

A=v(1);
x0=v(2);
shift = v(3);

yhat = A.*sinc(x0.*(x-shift)).^2;
 
Last edited:

1. What is the purpose of using the Sinc^2 function in nlinfit?

The Sinc^2 function is commonly used in nlinfit to model signals with a symmetric peak shape, such as spectral lines. It allows for a more accurate and precise fit to the data, as compared to other common functions like Gaussian or Lorentzian.

2. How do I troubleshoot errors when using the Sinc^2 function in nlinfit?

If you encounter errors while using the Sinc^2 function in nlinfit, the first step is to check that your input data is properly formatted and that you have specified the correct number of parameters. You may also need to adjust the initial parameter values to improve the fit. Additionally, checking for any potential outliers in your data and removing them can also help resolve errors.

3. Can I use the Sinc^2 function in nlinfit for non-symmetric data?

While the Sinc^2 function is primarily used for symmetric data, it can also be used for non-symmetric data. However, the fit may not be as accurate and it is recommended to use other functions that better suit the shape of the data.

4. Are there any specific considerations when using the Sinc^2 function in nlinfit?

One important consideration when using the Sinc^2 function in nlinfit is the choice of initial parameter values. These values can greatly affect the quality of the fit, so it is recommended to use values that are close to the actual parameters. It is also important to ensure that the function is properly scaled to the data, as this can also impact the fit.

5. Are there any alternatives to using the Sinc^2 function in nlinfit?

Yes, there are several alternatives to using the Sinc^2 function in nlinfit, such as Gaussian, Lorentzian, and Voigt functions. The choice of function will depend on the shape of the data and the specific application. It is recommended to try different functions and compare the fit results to determine the best fit for your data.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
987
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
806
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Back
Top