nlinfit: problem with global and local variables and input data matrix

8 views (last 30 days)
I am trying to use nlinfit to fit 14 sets of data to obtain a common exchange rate while a second scaling variable is also allowed to vary during the fit and should be different for each data set. I load a data file (f1) where the first row contains a 0 followed by my independent variables (24 of them) and each subsequent row has in column 1 a variable corresponding to the data in that row (ligand concentration) followed by the dependant variable (intensity) for the corresponding independent variables in row 1. I am getting the following error:
Error using nlinfit (line 122) Requires a vector second input argument.
Error in fit_lineshape_exchange (line 42) [beta,R]=nlinfit(x,y,@lineshape_exchange, beta0);
my function is below:
function f = lineshape_exchange(a)
%a(1) is k (the rate), a(2) is c (the weighting function allowing each
%spectrum to vary in intensity)
global K L v1 v2 Tp1 Tp2 w n
K=0.0000033;
Tp1=4.96;
Tp2=7.25;
v1=12.212;
v2=13.114;
f=0; for j=1:n
t=1/(a(1*L*(1+K)));
p1=K*a(1)*L*t;
p2=1-p1;
W=v1-v2;
P=t*((1/Tp1*Tp2)-4*pi^2*w^2+pi^2*W^2)+(p1/Tp1)+(p2/Tp2);
Q=t*(2*pi*w-pi*W*(p1-p2));
R=2*pi*w*(1+t*((1/Tp1)+(1/Tp2)))+pi*W*t*((1/Tp1)-(1/Tp2)+pi*W*(p1-p2));
f=a(2)*(P*(1+t*(p2/Tp1 + p1/Tp2))+Q*R)/(P^2+R^2);
end

Answers (1)

Star Strider
Star Strider on 14 May 2014
First, from the documentation, modelfun must accept two input arguments, a coefficient vector and an array X—in that order—and return a vector of fitted response values. ’ So your function statement should be:
function f = lineshape_exchange(a,x)
Second, what sizes are your x and y arrays? I get the impression they’re matrices and not vectors. The nlinfit function can take matrix x values, but must return a vector of estimated function values, and y must be a vector as well. (The lsqcurvefit function in the Optimization Toolbox can take matrix independent variables and fit them to matrix dependent variables.)
  16 Comments
Star Strider
Star Strider on 28 May 2014
I found a different paper by one of the same authors in PNAS that explained the equations more clearly. I’m having problems understanding the NMR equations. That technology is definitely not my area of expertise, and I need to understand at least some of it in order to understand what you’re doing. It will be easier with a bit more information that I’m still not clear on:
  • What parameters do you have and what are they?
  • Do any of them change in the various data you posted, or are they constant across all?
  • If any of them change, which ones? What values do they take for each run?
  • What parameters do you want to estimate?
When I get the opportunity to read a bit more about NMR, I’ll probably go ahead and code the function myself. If it works, I’ll post it here.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!