Troubleshooting nlinfit: Using Sinc^2 Function in MATLAB

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 15K views
Mindscrape
Messages
1,854
Reaction score
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
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.
 
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: