# fminsearch for fitting a function to experimental data

22 views (last 30 days)
ME_Raneem on 22 Jan 2020
Answered: Alex Sha on 28 Jan 2020
I have a response exponential function that I want to fit to experimental data represting the concentration of a drug in plasma at different times. I have 3 unknown parameters: c2,k12,k2e and i want to use fmnisearch (nelder-meade).
This is the experimental data:
t= [0 5 15 30 90 180 360 720 1440 2160 2880 3600 4320];
y= [0 3.13 8.40 17.15 43.19 66.53 91.38 103.82 111 108.18 98.66 106.40 100.67];
This is the function:
120*c2*k12((exp(-k12*t)/(k2e-k12))+ (exp(-k2e*t)/(k12-k2e)))
I added an obj function and set the initial values for the parameters but got multiple error. Can anyone direct me on what to do? thank you

John D'Errico on 22 Jan 2020
Even after you fix the problems in multiplication, let me look at your model and your data.
syms c2 k12 k2e t
fun = 120*c2*k12*((exp(-k12*t)/(k2e-k12))+ (exp(-k2e*t)/(k12-k2e)));
pretty(fun)
/ exp(-k12 t) exp(-k2e t) \
-c2 k12 | ----------- - ----------- | 120
\ k12 - k2e k12 - k2e /
So a difference between a pair of negative exponentials, but not a model that allows much flexibility. This is always a very difficult problem to solve, one that requires good data. Is your data at all good?
t = [0 5 15 30 90 180 360 720 1440 2160 2880 3600 4320];
y = [0 3.13 8.40 17.15 43.19 66.53 91.38 103.82 111 108.18 98.66 106.40 100.67];
plot(t,y,'-o') UGH. Not terribly great data. That serious noise on the top end means your parameters will be not well estimated.
However, let me see if I can make it work. Some moderately intelligent guesses for starting values will be helpful. I'll use the curve fitting toolbox, since it returns nice confidence limits on the parameters,
ft = fittype('120*c2*k12*((exp(-k12*t)/(k2e-k12))+ (exp(-k2e*t)/(k12-k2e)))','indep','t','coef',{'c2','k12','k2e'});
mdl = fit(t',y',ft,'start',[100,.001 .0001])
mdl =
General model:
mdl(t) = 120*c2*k12*((exp(-k12*t)/(k2e-k12))+ (exp(-k2e*t)/(k12-k2e)))
Coefficients (with 95% confidence bounds):
c2 = 259.1 (-42.19, 560.5)
k12 = 1.853e-05 (-2.137e-06, 3.921e-05)
k2e = 0.005252 (0.0045, 0.006004)
plot(mdl)
hold on
plot(t,y,'o') Honestly, I was surprised it came out fitting the data as well as it did.
However, you want to recognize the HUGE limits on the parameters, especially c2 and k12. That suggests the model can easily tradeoff variation in one of the parameters for a corresponding increase or decrease in the other.
This is a virtue of using a tool like the curve fitting toolbox, instead of fminsearch. Besides being easy to use, it also yields some information about the result that would not have been obvious if you just threw it at a tool like fminsearch.

Walter Roberson on 22 Jan 2020
lambdaone(exp(-lambdaone*t)
MATLAB does not have implied multiplication so that code is trying to index lambdaone at a non-integer value.
##### 2 CommentsShowHide 1 older comment
Walter Roberson on 22 Jan 2020
In MATLAB expressions of the form A(B) are either indicating that array A is to be indexed at location B, or else that A is a function or function handle that is to be invoked passing in B.
MATLAB has absolutely no implied multiplication, including the symbolic toolbox. A(B) never means that A is to be multiplied by B in MATLAB.

Star Strider on 22 Jan 2020
There are several errors in the posted code, as Walter noted, mostly involving implicit multiplication, that MATLAB does not recognise. It also needs to be vectorised for best results.
f1 = @(c2,k12,k2e,t) 120.*c2.*k12.*((exp(-k12.*t)./(k2e-k12))+ (exp(-k2e.*t)./(k12-k2e)));
[B, fval] = fminsearch(@(b) norm(f1(b(1),b(2),b(3),t) - y), rand(3,1))

Alex Sha on 28 Jan 2020
There seem to be two sets of solutions:
1:
Root of Mean Square Error (RMSE): 2.45406561689894
Sum of Squared Residual: 78.2916946765927
Correlation Coef. (R): 0.998412539305854
R-Square: 0.996827598643164
Determination Coef. (DC): 0.996779811828132
Chi-Square: 0.45194762989722
F-Statistic: 1565.7471589716
Parameter Best Estimate
---------- -------------
c2 0.914786467280745
k12 0.00524586453053884
k2e 1.86308569115126E-5
2:
Root of Mean Square Error (RMSE): 2.45406561689894
Sum of Squared Residual: 78.2916946765926
Correlation Coef. (R): 0.998412539304863
R-Square: 0.996827598641184
Determination Coef. (DC): 0.996779811828132
Chi-Square: 0.451947628306123
F-Statistic: 1565.74715859552
Parameter Best Estimate
---------- -------------
c2 257.575157220257
k12 1.86308568627457E-5
k2e 0.00524586453574418