# How to optimize for decay constant in exponential-like function?

I've got a data set of points $M_O .. M_N$ for time points $t_0 .. t_N$, where $N$ is approximately 10-20, and the spacing of time is not uniform (i.e., $t_{i+1}-t_i$ is not constant for all i). It is believed that this data should be able to be modeled by a differential equation of the form

$\frac{dM}{dt} = -k (M - M_{e})$

My goal is to solve for the constant $k$. If $M_e$ were a constant, then the solution of the function $M$ would of course be something like

$M = M_{e} + Ce^{-kt}$

and I could then fit this function to the data and determine $k$ with any regular minimization routine (say, using the RMSE).

However, $M_{e}$ is not a constant, but instead varies with $t$. I already have the values of $M_e$ for all $N$ points in my data set.

My question is, what is the best way to numerically optimize for the $k$ which fits the model best? Naively, I could approximate $\frac{dM}{dt}\approx \frac{\Delta M}{\Delta t}$, compute the differences for each segment, and choose something like the mean value of $k$ from each $\Delta t$. I'm curious as to whether this is the best way to do this, or if there's another more appropriate method. One possible weakness of what I'm describing is that averaging $k$ for all segments ignores the fact that some $\Delta t$ intervals are larger than others, and perhaps smaller $\Delta t$ should be systematically favored over larger time intervals. Finally, is there any good method to get a somewhat closed form solution for $M(t)$, or does this hinge on whether there's a simple closed-form solution to $M_e(t)$ (which is doubtful in my case because of its specific physical origin).

• If you're willing to stick with the optimisation route, you could note that $\frac{d}{dt}(M e^{kt}) = kM_e$, giving the integral equation $M(t) = M_0 +e^{-kt}\int_{t_0}^t ke^{kt'}M_e(t') dt'$. This leaves you either to find weights for the numerical quadrature, or to fit a curve through your $M_e$ data. – origimbo Sep 26 '17 at 23:22
• Thanks for the comment @origimbo. Maybe I'm missing something, but isn't $\frac{d}{dt}(Me^{kt}) = k M_e e^{kt}?$ Either way, could you point me further to how you turn that into an integral equation? And if $M_{e}$ isn't a smooth function of $t$, will this be problematic to do? And finally, am I able to perform these operations while not knowing $k$, which is at least half of what I'm interested in solving for? Thanks. – gammapoint Sep 27 '17 at 0:31
• Yes, sorry, I dropped a couple of $e^{-kt}$s there. Similarly the second line should be $M(t) = M_0e^{-kt+kt_0}+e^{-kt}\int_{t_0}^t k e^{kt'} M_e(t') dt'$, which follows by integrating the last relation. How difficult the integration gets depends on your knowledge of $M_e$. if it's not integrable, this won't work (but I suspect neither will anything else) but it wouldn't need to be smooth or everywhere continuous (you'd work piecewise). – origimbo Sep 27 '17 at 0:44
• Try to substract your estimate for $M_e$ from your data. Call this result $m$. Then use Levenberg-Marquardt to find $C$ and $k$ such that $(m-Ce^{-kt})^2=0$. – knl Sep 27 '17 at 12:44
• If $M_e$ is known at every data point, why not just minimize RMSE between the data and the functional form in your second equation? The residuals can already be computed, so you can find $k$ without needing any continuous-time $M_e(t)$. – Dave Kielpinski Sep 27 '17 at 18:08