MATLAB: How to fit a curve and get multiple coefficients

curve fittingMATLAB

Hello All!
I have an equation of the form
y= C1+C2* exp(-C3/x)
But here, C1 is K1*K2/K1+K2
C2= K1*K1/ K1+K2
and C3= (K1+K2)K3
I tried to use nlinfit but I kept getting an error.
My question:
If I have a set of data [y] and [x], is it possible to get the values of k1 and k2 and c3 ?
My Data Sets
x=[185.5 196.2 227.8 246.4 263.5 282.6 299.7 339.1 381.9 401.1 412.7 448.6 463.2 480.3];
y=[78.7 79.1 76.7 87.3 85.9 86.7 88.7 90.9 93.8 90.5 90.1 95.9 92.6 96.3];
equation
(((k1*k2)/(k1+k2))+(k1*k1/k1+k2)*(exp(-(c3/x))))

Best Answer

Huh?
K1*K2/K1+K2
I'm pretty sure that is just K2 + K2 = 2*K2, since the last time I checked, K1*K2/K1 was just K2.
So you probably don't know that parens are important, as they tell the computer as well as other people what you mean, instead of what you write.
Not only that, but MATLAB does not just know that when you put two things next to each other, that you want multiplication to be assumed. You need the * operator there.
So, is it just possible that you want to write things like
C1 = K1*K2/(K1+K2)
C2 = K1*K1/(K1+K2)
C3 = (K1 + K2)*K3
A little algebra should convince you that If you knew C1,C2,C3, then you can recover K1,K2,K3, as...
K1 = C1 + C2
K2 = (C1*(C1 + C2))/C2
K3 = (C2*C3)/(C1^2 + 2*C1*C2 + C2^2)
Or, you can be lazy and throw it at the symbolic toolbox todo the work for you. Either way, just fit that model in the form of
y= C1+C2*exp(-C3/x)
How do you do that? Simple. I'll assume the curve fitting toolbox.
ft = fittype('C1+C2*exp(-C3./x)');
You will need to choose an intelligent set of parameters to start the fit. Exponential models are nasty that way. Crappy starting values almost always seem to result in crap for a fit. Most important is getting the rate constant (C3) at least close.
C0 = [100,10,100];
mdl = fit(x',y',ft,'startpoint',C0)
mdl =
General model:
mdl(x) = C1+C2* exp(-C3./x)
Coefficients (with 95% confidence bounds):
C1 = 17.64 (-504.7, 539.9)
C2 = 90.64 (-410.2, 591.5)
C3 = 76.76 (-506.3, 659.8)
Pretty wide limits on those parameters. But then your data is pretty much complete crap too, with pretty large noise. Such is life.
plot(mdl)
hold on
plot(x,y,'o')
grid on
But the fit looks reasonable. It goes roughly through the middle of the data, and it looks like a negative exponential model.
Now just recover K1,K2,K3.
C1 = mdl.C1;
C2 = mdl.C2;
C3 = mdl.C3;
K1 = C1 + C2
K2 = (C1*(C1 + C2))/C2
K3 = (C2*C3)/(C1^2 + 2*C1*C2 + C2^2)
K1 =
108.28
K2 =
21.073
K3 =
0.59342
Could you have fit the model directly to K1,K2,K3? Well, yes. But then you would have needed to correctly type in that model. Again, that would require understanding when to use parens, etc. But way more importantly, it would have required you know intelligent starting guesses for K1,K2,K3. And while I could come up with them for the original model in terms of the C1,C2,C3 variables, doing so by merely looking at the curve would not have been possible in terms of the K1,K2,K3 unknowns.