nlinfit: problem with global and local variables and input data matrix
Afficher commentaires plus anciens
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
Réponses (1)
Star Strider
le 14 Mai 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 commentaires
Julie
le 14 Mai 2014
Star Strider
le 14 Mai 2014
Modifié(e) : Star Strider
le 14 Mai 2014
I just now saw your previous post. I thought ‘exchange rate’ had to do with economics, and since that is far from my areas of expertise, didn’t look. Your actual problem turns out to be very much within them.
There, I got the impression you want to fit a vector of x values to a matrix of y values, something nlinfit doesn’t permit.
If you are fitting your x vector to each row of your y vector independently, nlinfit will comply.
You have to do the y-row selection in a separate calling program, pass your x vector and each row of your y matrix to nlinfit individually from within a for-loop, and then collect the necessary outputs from nlinfit. (I suggest you get all of them, since you will likely need them for nlparci and nlpredci.)
Do not pass the indicator variable ( 0, 9, 18, 36, etc. ) to it, because its presence in the vector will result in unequal x and y lengths, and throw an error. Save your indicator variables in a separate vector that corresponds to the subscripts in the beta and other results arrays you save.
So your code will go something like this:
for k1 = 1:size(y,1)
yr = y(k1,:);
[beta(:,k1),R(:,k1)]=nlinfit(x,yr,@lineshape_exchange, beta0);
end
This assumes your x and y values are row vectors, and beta and R are column vectors. If the beta and R vectors are row vectors, the subscripts become (k1,:) instead.
Julie
le 15 Mai 2014
Star Strider
le 15 Mai 2014
I honestly cannot follow your ‘lineshape_exchange’ function beyond figuring out that you’re only estimating the rate constant as a(1). I have no idea what ‘lineshape_exchange’ does.
It should use your ‘a’ and ‘x’ vectors something like this:
f = a(1).*exp(a(2).*x) + a(3);
This is only intended as an illustrative example, but you don’t need to refer to each element of x as you apparently do with your reference to x(j).
Don’t pass your weighting variable as a parameter to be estimated (an element of your ‘a’ parameter vector). It isn’t one. It is a parameter you need to pass to the function outside separately, and that is apparently used by the function somewhere.
You can do weighted nonlinear regression with nlinfit, and you can pass your ‘lineshape_exchange’ extra parameters (like your indicator variable) easily, but I get the impression it is simply a parameter and not a true weight vector.
Maybe I can help you sort this. What are your original equations for ‘lineshape_exchange’? If you have a PDF of the paper, attach it to your original post. (Use the ‘Edit’ option on your original post, then use the ‘paperclip’ icon to attach and upload the paper.)
Julie
le 15 Mai 2014
Star Strider
le 15 Mai 2014
I’ll wait for the paper. Give me some time to read it and figure out what it is you’re doing.
Julie
le 15 Mai 2014
Star Strider
le 15 Mai 2014
Modifié(e) : Star Strider
le 15 Mai 2014
I don’t see it yet. I’ll check back later, since sometimes it takes a while to appear.
You might want to check back from time to time as well to see when it appears. If it hasn’t in a half hour, upload it again.
TWO HOURS LATER (19:38Z — No upload yet.
Julie
le 15 Mai 2014
Star Strider
le 15 Mai 2014
I finally found it — uploaded to your other question.
It’s going to take me a bit to get through it, since I have to figure out what they’re doing and then if I can do it in MATLAB.
To make it easier, what variable do you want to fit to your data? What parameter(s) do you want to estimate?
Star Strider
le 16 Mai 2014
I’ll put the previous paper aside then, and give this one priority. Should I refer to the previous paper at all, or just this one? How do they interact? How do they apply to what you’re doing?
I have to come up to speed on this relatively quickly, so I need to know what particular ideas and equations to attend to.
- What equations are you fitting? I assume one or more of Eqns 3-6, but it would help for me to know sooner rather than later.
- What parameters are you estimating (in terms of the equations in the paper)?
- What are your independent and dependent variables (in terms of the equations in the paper) that you’re fitting?
- I assume you’re referring to Figure 6 (d)-(e), correct?
- How do the changes you’ve obviously made in your code (but haven’t posted) bear on what you have posted? (I’m going to code it up myself anyway, but I need to know if your approach has changed.)
- After we clarify the questions I’ve raised here, this is going to take me at least a few hours to read the paper (this one or both), understand how they relate to your data, code the function to give to nlinfit, and test the code to be sure it works before I post it here.
I’m somewhat familiar with the biochemistry (although H. sapiens, not E. coli) and the essence of NMR and spectral analysis, but I’ve not done anything similar to what you’re doing.
Julie
le 16 Mai 2014
Star Strider
le 16 Mai 2014
Thanks for the clarification. It’s still going to take me some time to go through the first paper to understand their derivations, but your annotated code should help.
Julie
le 28 Mai 2014
Star Strider
le 28 Mai 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.
Catégories
En savoir plus sur Chemistry dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!