syms x1(t) x2(t) k12 k21 k20 k01
conds = [x1(0) == k01; x2(0) == 0];
eqn1 = diff(x1) == -k12*x1 + k21*x2;
eqn2 = diff(x2) == k12*x1 - (k21 + k20)*x2;
[x1sol, x2sol] = dsolve(eqn1, eqn2, conds);
time = [0.03,0.07,0.10,0.13,0.17,0.23,0.30,0.37,0.50,0.75,1.00,2.00,3.00,4.00,6.00];
conc = [21.0,22.0,18.0,15.0,15.5,14.0,11.3,13.5,12.5,9.1,6.7,3.6,2.6,2.2,1.2];
fitfun = @(k,ti) double(subs(subs(x1sol, [k01,k12,k21,k20],k),t,ti));
k_init = [50,20,0.0001,15];
kfit = lsqcurvefit(fitfun, k_init, time, conc)
Local minimum possible.
lsqcurvefit stopped because the final change in the sum of squares relative to
its initial value is less than the value of the function tolerance.
kfit =
24.7277 4.0073 4.2529 1.4075
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
x1fit = fitfun(kfit, time);
plot(time, conc, 'bo-', time, x1fit,'r*--')
legend("x1 (data)", "x1 (lsqcurvefit)")
title("Plasma Compartment (Model Fitting)")