I have a RKF45 numerical integrator that simulates polymerization of proteins using CUDA. It does so by tracking the populations of discrete length polymers, e.g. monomers, dimers, trimers, etc. all the way up to 8192-mers using the equations of the type that follow.
$$\frac{dc_r}{dt}= k_nc_r^{n_c}+2k_a(c_{r-1}-c_r) + k_m(2\sum^{max}_{s=r+2}c_s-(r-1)c_r) $$
where r represents the length of the polymer and c its concentration at that given time. The k's are the parameters that I'd like to minimize an objective function with respect to. These equations are integrated over about 20,000-30,000 timesteps.
My goal is to start fitting the model to experimental data. The first type of fit I'm trying is fitting a value derived from these polymer populations at arbitrary timesteps. For instance, I want to calculate the average length of polymers at five different times in the simulation/integration and compare them to externally supplied data. The average length is derived for each timestep from the concentrations like this :
$$L(t) = \frac{M(t)}{N(t)}$$ $$M(t) = \sum^{max}_{s=n_c}s*c_s(t)$$ $$N(t) = \sum^{max}_{s=n_c}c_s(t)$$
The objective function I'm trying to minimize thusly looks like this :
$$y(t)=\sqrt{\sum_{fit data}(L_{calculated}-L_{experimental})^2}$$
I've already implemented a solution to do this and run it through the following norm and minimize said norm/objective function using Scipy's Nelder-Mead optimize method. It seems to work, but if it's possible to make it perform better, I'd like to make it happen. I want to try and utilize more robust and swifter converging methods like L-BFGS-B, especially because they also allow me to impose bounds on the parameters, that need to remain positive.
Now, the Runge-Kutta algorithm, for the unfamiliar, is here : http://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods
Due to the recursive nature of the Runge Kutta calculations such as $$k_1=f(y_n)$$ $$k_2=f(y_n+a_{21}k_1)$$ $$k_3=f(y_n+a_{31}k_1+a_{32}k_2)$$
I'm having issues figuring out how to calculate the Jacobian, which I believe would be this.
$$J(t) = [\frac{dy}{dk_a} \frac{dy}{dk_m} \frac{dy}{dk_n}]$$
So, if I framed this question properly, is it possible at some point during the RKF calculation to construct a Jacobian to pass out to Scipy along with the objective function evaluation? I'd like to get an optimized optimization script up and running ASAP.