# Symbolic ODE solutions for mkin¶

Johannes Ranke, 11 May 2020

This jupyter notebook was created using Maxima-Jupyter which makes it comfortable to write and execute code for the Maxima Computer Algebra System (CAS). It was prepared to support the implementation of symbolic solutions for some of the systems of linear autonomous differential equations used in the mkin R package. In particular, parent compounds that degrade with Simple First-Order (SFO) or Dual First-Order in Parallel (DFOP) kinetics, coupled to one SFO metabolite are now implemented in mkin. More solutions would be possible, expecially for systems that can be represented by coefficient matrices like the Single First-Order Reversible Binding (SFORB) equations and their combinations with SFO equations. In order to explore this, Maxima code is shown here that solves the corresponding initial value problem for the SFORB-SFO system as an example. However, this solution is not implemented in mkin at current.

The solutions that are implemented (SFO-SFO and DFOP-SFO) were obtained using the desolve function as shown below.

## Preliminaries¶

The following two functions for setting up matrices used to solve the initial value problem for autonomous systems (without time on the right hand side) are from a first wonderful blog entry by Eric Barth.

In :
eigU(v):=transpose(apply('matrix,makelist(part(v,2,i,1),i,1,length(part(v,2)),1)))$eigdiag(v):=apply('diag_matrix,part(v,1,1))$


The following function for solving systems of linear equations in matrix form is from another wonderful blog entry.

In :
matsolve(A,b):=block(
[AugU],
backsolve(AugU)
)$backsolve(augU):=block( [i,j,m,n,b,x,klist,k,np,nosoln:false], [m,n]:matsize(augU), b:col(augU,n), klist:makelist(concat('%k,i),i,1,n-1), k:0, x:transpose(matrix(klist)), for i:m thru 1 step -1 do ( np:pivot(row(augU,i)), if is(equal(np,n)) then (nosoln:true,return()) else if not(is(equal(np,0))) then (x[np]:b[i], for j:np+1 thru n-1 do x[np]:x[np]-augU[i,j]*x[j]) ), if nosoln then return([]) else return(expand(x)) )$

matsize(A):=[length(A),length(transpose(A))]$pivot(rr):=block([i,rlen], p:0, rlen:length(transpose(rr)), for i:1 thru rlen do( if is(equal(part(rr,1,i),1)) then (p:i,return())), return(p) )$


## SFO-SFO with complete formation¶

In :
diff_1_sfo: 'diff(n1(t), t) = - k1  * n1(t);
diff_2_sfo_sfo: 'diff(n2(t), t) = k1 * n1(t) - k2 * n2(t);

Out:
$\tag{{\it \%o}_{7}}\frac{d}{d\,t}\,n_{1}\left(t\right)=-k_{1}\,n_{1}\left(t\right)$
Out:
$\tag{{\it \%o}_{8}}\frac{d}{d\,t}\,n_{2}\left(t\right)=k_{1}\,n_{1}\left(t\right)-k_{2}\,n_{2}\left(t\right)$
In :
atvalue(n1(t), t = 0, n10)$atvalue(n2(t), t = 0, n20)$
sol_sfo_sfo: desolve([diff_1_sfo, diff_2_sfo_sfo], [n1(t), n2(t)])$sol_sfo_sfo; sol_sfo_sfo;  Out: $\tag{{\it \%o}_{12}}n_{1}\left(t\right)=n_{10}\,e^ {- k_{1}\,t }$ Out: $\tag{{\it \%o}_{13}}n_{2}\left(t\right)=\frac{\left(\left(k_{2}-k_{1}\right)\,n_{20}-k_{1}\,n_{10}\right)\,e^ {- k_{2}\,t }}{k_{2}-k_{1}}+\frac{k_{1}\,n_{10}\,e^ {- k_{1}\,t }}{k_{2}-k_{1}}$ ## SFO-SFO with formation fraction¶ In : diff_2_sfo_f12_sfo: 'diff(n2(t), t) = f12 * k1 * n1(t) - k2 * n2(t);  Out: $\tag{{\it \%o}_{14}}\frac{d}{d\,t}\,n_{2}\left(t\right)=f_{12}\,k_{1}\,n_{1}\left(t\right)-k_{2}\,n_{2}\left(t\right)$ In : atvalue(n1(t), t = 0, n10)$
atvalue(n2(t), t = 0, n20)$sol_sfo_f12_sfo: desolve([diff_1_sfo, diff_2_sfo_f12_sfo], [n1(t), n2(t)])$
sol_sfo_f12_sfo;
sol_sfo_f12_sfo;

Out:
$\tag{{\it \%o}_{18}}n_{1}\left(t\right)=n_{10}\,e^ {- k_{1}\,t }$
Out:
$\tag{{\it \%o}_{19}}n_{2}\left(t\right)=\frac{\left(\left(k_{2}-k_{1}\right)\,n_{20}-f_{12}\,k_{1}\,n_{10}\right)\,e^ {- k_{2}\,t }}{k_{2}-k_{1}}+\frac{f_{12}\,k_{1}\,n_{10}\,e^ {- k_{1}\,t }}{k_{2}-k_{1}}$

## SFO-SFO without formation fraction¶

In :
diff_1_sfo_k120_sfo: 'diff(n1(t), t) = - (k10 + k12)  * n1(t);
diff_2_sfo_k120_sfo: 'diff(n2(t), t) = k12 * n1(t) - k2 * n2(t);

Out:
$\tag{{\it \%o}_{20}}\frac{d}{d\,t}\,n_{1}\left(t\right)=\left(-k_{12}-k_{10}\right)\,n_{1}\left(t\right)$
Out:
$\tag{{\it \%o}_{21}}\frac{d}{d\,t}\,n_{2}\left(t\right)=k_{12}\,n_{1}\left(t\right)-k_{2}\,n_{2}\left(t\right)$
In :
atvalue(n1(t), t = 0, n10)$atvalue(n2(t), t = 0, n20)$
sol_sfo_k120_sfo: desolve([diff_1_sfo_k120_sfo, diff_2_sfo_k120_sfo], [n1(t), n2(t)])$sol_sfo_k120_sfo; sol_sfo_k120_sfo;  Out: $\tag{{\it \%o}_{25}}n_{1}\left(t\right)=n_{10}\,e^ {- \left(k_{12}+k_{10}\right)\,t }$ Out: $\tag{{\it \%o}_{26}}n_{2}\left(t\right)=\frac{\left(\left(k_{2}-k_{12}-k_{10}\right)\,n_{20}-k_{12}\,n_{10}\right)\,e^ {- k_{2}\,t }}{k_{2}-k_{12}-k_{10}}+\frac{k_{12}\,n_{10}\,e^ {- \left(k_{12}+k_{10}\right)\,t }}{k_{2}-k_{12}-k_{10}}$ ## DFOP-SFO with complete formation¶ In : diff_11_dfop: 'diff(m1(t), t) = - l1 * m1(t); diff_12_dfop: 'diff(m2(t), t) = - l2 * m2(t); diff_2_dfop_sfo: 'diff(n2(t), t) = l1 * m1(t) + l2 * m2(t) - k2 * n2(t);  Out: $\tag{{\it \%o}_{27}}\frac{d}{d\,t}\,m_{1}\left(t\right)=-l_{1}\,m_{1}\left(t\right)$ Out: $\tag{{\it \%o}_{28}}\frac{d}{d\,t}\,m_{2}\left(t\right)=-l_{2}\,m_{2}\left(t\right)$ Out: $\tag{{\it \%o}_{29}}\frac{d}{d\,t}\,n_{2}\left(t\right)=-k_{2}\,n_{2}\left(t\right)+l_{2}\,m_{2}\left(t\right)+l_{1}\,m_{1}\left(t\right)$ In : atvalue(m1(t), t = 0, g * n10)$
atvalue(m2(t), t = 0, (1 - g) * n10)$atvalue(n2(t), t = 0, n20)$
sol_dfop_sfo: desolve([diff_11_dfop, diff_12_dfop, diff_2_dfop_sfo], [m1(t), m2(t), n2(t)])$sol_dfop_sfo; sol_dfop_sfo; mapped_sol_dfop_sfo: [n1(t)= rhs(sol_dfop_sfo) + rhs(sol_dfop_sfo), sol_dfop_sfo]$
mapped_sol_dfop_sfo;
mapped_sol_dfop_sfo;

Out:
$\tag{{\it \%o}_{34}}m_{1}\left(t\right)=g\,n_{10}\,e^ {- l_{1}\,t }$
Out:
$\tag{{\it \%o}_{35}}m_{2}\left(t\right)=\left(1-g\right)\,n_{10}\,e^ {- l_{2}\,t }$
Out:
$\tag{{\it \%o}_{37}}n_{1}\left(t\right)=\left(1-g\right)\,n_{10}\,e^ {- l_{2}\,t }+g\,n_{10}\,e^ {- l_{1}\,t }$
Out:
$\tag{{\it \%o}_{38}}n_{2}\left(t\right)=\frac{\left(g-1\right)\,l_{2}\,n_{10}\,e^ {- l_{2}\,t }}{l_{2}-k_{2}}-\frac{g\,l_{1}\,n_{10}\,e^ {- l_{1}\,t }}{l_{1}-k_{2}}+\frac{\left(\left(\left(l_{1}-k_{2}\right)\,l_{2}-k_{2}\,l_{1}+k_{2}^2\right)\,n_{20}+\left(\left(l_{1}+\left(g-1\right)\,k_{2}\right)\,l_{2}-g\,k_{2}\,l_{1}\right)\,n_{10}\right)\,e^ {- k_{2}\,t }}{\left(l_{1}-k_{2}\right)\,l_{2}-k_{2}\,l_{1}+k_{2}^2}$

## DFOP-SFO with formation fraction¶

In :
diff_11_dfop: 'diff(m1(t), t) = - l1 * m1(t);
diff_12_dfop: 'diff(m2(t), t) = - l2 * m2(t);
diff_2_dfop_f12_sfo: 'diff(n2(t), t) = f12 * l1 * m1(t) + f12 * l2 * m2(t) - k2 * n2(t);

Out:
$\tag{{\it \%o}_{39}}\frac{d}{d\,t}\,m_{1}\left(t\right)=-l_{1}\,m_{1}\left(t\right)$
Out:
$\tag{{\it \%o}_{40}}\frac{d}{d\,t}\,m_{2}\left(t\right)=-l_{2}\,m_{2}\left(t\right)$
Out:
$\tag{{\it \%o}_{41}}\frac{d}{d\,t}\,n_{2}\left(t\right)=-k_{2}\,n_{2}\left(t\right)+f_{12}\,l_{2}\,m_{2}\left(t\right)+f_{12}\,l_{1}\,m_{1}\left(t\right)$
In :
atvalue(m1(t), t = 0, g * n10)$atvalue(m2(t), t = 0, (1 - g) * n10)$
atvalue(n2(t), t = 0, n20)$sol_dfop_f12_sfo: desolve([diff_11_dfop, diff_12_dfop, diff_2_dfop_f12_sfo], [m1(t), m2(t), n2(t)])$
sol_dfop_f12_sfo;
sol_dfop_f12_sfo;
mapped_sol_dfop_f12_sfo: [n1(t)= rhs(sol_dfop_sfo) + rhs(sol_dfop_sfo), sol_dfop_f12_sfo]$mapped_sol_dfop_f12_sfo; mapped_sol_dfop_f12_sfo;  Out: $\tag{{\it \%o}_{46}}m_{1}\left(t\right)=g\,n_{10}\,e^ {- l_{1}\,t }$ Out: $\tag{{\it \%o}_{47}}m_{2}\left(t\right)=\left(1-g\right)\,n_{10}\,e^ {- l_{2}\,t }$ Out: $\tag{{\it \%o}_{49}}n_{1}\left(t\right)=\left(1-g\right)\,n_{10}\,e^ {- l_{2}\,t }+g\,n_{10}\,e^ {- l_{1}\,t }$ Out: $\tag{{\it \%o}_{50}}n_{2}\left(t\right)=\frac{\left(f_{12}\,g-f_{12}\right)\,l_{2}\,n_{10}\,e^ {- l_{2}\,t }}{l_{2}-k_{2}}-\frac{f_{12}\,g\,l_{1}\,n_{10}\,e^ {- l_{1}\,t }}{l_{1}-k_{2}}+\frac{\left(\left(\left(l_{1}-k_{2}\right)\,l_{2}-k_{2}\,l_{1}+k_{2}^2\right)\,n_{20}+\left(\left(f_{12}\,l_{1}+\left(f_{12}\,g-f_{12}\right)\,k_{2}\right)\,l_{2}-f_{12}\,g\,k_{2}\,l_{1}\right)\,n_{10}\right)\,e^ {- k_{2}\,t }}{\left(l_{1}-k_{2}\right)\,l_{2}-k_{2}\,l_{1}+k_{2}^2}$ ## SFORB-SFO¶ In : m1_sforb: 'diff(m1(t), t) = - (l12 + k1) * m1(t) + l21 * m2(t); m2_sforb: 'diff(m2(t), t) = + l12 * m1(t) - l21 * m2(t); n2_sforb: 'diff(n2(t), t) = + k1 * m1(t) - k2 * m2(t);  Out: $\tag{{\it \%o}_{51}}\frac{d}{d\,t}\,m_{1}\left(t\right)=l_{21}\,m_{2}\left(t\right)+\left(-l_{12}-k_{1}\right)\,m_{1}\left(t\right)$ Out: $\tag{{\it \%o}_{52}}\frac{d}{d\,t}\,m_{2}\left(t\right)=l_{12}\,m_{1}\left(t\right)-l_{21}\,m_{2}\left(t\right)$ Out: $\tag{{\it \%o}_{53}}\frac{d}{d\,t}\,n_{2}\left(t\right)=k_{1}\,m_{1}\left(t\right)-k_{2}\,m_{2}\left(t\right)$ In : m1_sforb: 'diff(m1(t), t) = - l12 * m1(t) - k1 * m1(t) + l21 * m2(t); m2_sforb: 'diff(m2(t), t) = + l12 * m1(t) - l21 * m2(t);  Out: $\tag{{\it \%o}_{54}}\frac{d}{d\,t}\,m_{1}\left(t\right)=l_{21}\,m_{2}\left(t\right)-l_{12}\,m_{1}\left(t\right)-k_{1}\,m_{1}\left(t\right)$ Out: $\tag{{\it \%o}_{55}}\frac{d}{d\,t}\,m_{2}\left(t\right)=l_{12}\,m_{1}\left(t\right)-l_{21}\,m_{2}\left(t\right)$ These equations could not be handeled by desolve - it asks if an expression containing l12, l21 and k1 is positive, negative or zero and returns very complicated answers for positive or negative. In : /* atvalue(m1(t), t = 0, n10)$
atvalue(m2(t), t = 0, 0)$atvalue(n2(t), t = 0, n20)$
sol: desolve([m1_sforb, m2_sforb], [m1(t), m2(t)])$sol; sol; */  Therefore, a coefficient was set up, the Eigenvalue problem was solved, and the constants relevant for the desired initial value problem were obtained as shown below. In : K_sforb_sfo: matrix( [-(l12 + k1), l21, 0], [l12, -l21, 0], [k1, 0, -k2]);  Out: $\tag{{\it \%o}_{56}}\begin{pmatrix}-l_{12}-k_{1} & l_{21} & 0 \\ l_{12} & -l_{21} & 0 \\ k_{1} & 0 & -k_{2} \\ \end{pmatrix}$ In : E_sforb_sfo: eigenvectors(K_sforb_sfo)$
[[lambda, mult], vecs]: E_sforb_sfo$lambda; lambda; lambda;  Out: $\tag{{\it \%o}_{59}}-\frac{\sqrt{l_{21}^2+\left(2\,l_{12}-2\,k_{1}\right)\,l_{21}+l_{12}^2+2\,k_{1}\,l_{12}+k_{1}^2}+l_{21}+l_{12}+k_{1}}{2}$ Out: $\tag{{\it \%o}_{60}}\frac{\sqrt{l_{21}^2+\left(2\,l_{12}-2\,k_{1}\right)\,l_{21}+l_{12}^2+2\,k_{1}\,l_{12}+k_{1}^2}-l_{21}-l_{12}-k_{1}}{2}$ Out: $\tag{{\it \%o}_{61}}-k_{2}$ In : mult;  Out: $\tag{{\it \%o}_{62}}\left[ 1 , 1 , 1 \right]$ In : U: eigU(E_sforb_sfo);  Out: $\tag{{\it \%o}_{63}}\begin{pmatrix}1 & 1 & 0 \\ -\frac{\sqrt{l_{21}^2+\left(2\,l_{12}-2\,k_{1}\right)\,l_{21}+l_{12}^2+2\,k_{1}\,l_{12}+k_{1}^2}+l_{21}-l_{12}-k_{1}}{2\,l_{21}} & \frac{\sqrt{l_{21}^2+\left(2\,l_{12}-2\,k_{1}\right)\,l_{21}+l_{12}^2+2\,k_{1}\,l_{12}+k_{1}^2}-l_{21}+l_{12}+k_{1}}{2\,l_{21}} & 0 \\ -\frac{k_{1}\,\sqrt{l_{21}^2+\left(2\,l_{12}-2\,k_{1}\right)\,l_{21}+l_{12}^2+2\,k_{1}\,l_{12}+k_{1}^2}-k_{1}\,l_{21}-k_{1}\,l_{12}+2\,k_{1}\,k_{2}-k_{1}^2}{\left(2\,k_{2}-2\,k_{1}\right)\,l_{21}+2\,k_{2}\,l_{12}-2\,k_{2}^2+2\,k_{1}\,k_{2}} & \frac{k_{1}\,\sqrt{l_{21}^2+\left(2\,l_{12}-2\,k_{1}\right)\,l_{21}+l_{12}^2+2\,k_{1}\,l_{12}+k_{1}^2}+k_{1}\,l_{21}+k_{1}\,l_{12}-2\,k_{1}\,k_{2}+k_{1}^2}{\left(2\,k_{2}-2\,k_{1}\right)\,l_{21}+2\,k_{2}\,l_{12}-2\,k_{2}^2+2\,k_{1}\,k_{2}} & 1 \\ \end{pmatrix}$ In : c: matsolve(U, [n10, 0, 0])$
c;
c;
c;

Out:
$\tag{{\it \%o}_{65}}\left[ -\frac{l_{21}\,\sqrt{l_{21}^2+2\,l_{12}\,l_{21}-2\,k_{1}\,l_{21}+l_{12}^2+2\,k_{1}\,l_{12}+k_{1}^2}\,n_{10}}{2\,l_{21}^2+4\,l_{12}\,l_{21}-4\,k_{1}\,l_{21}+2\,l_{12}^2+4\,k_{1}\,l_{12}+2\,k_{1}^2}+\frac{l_{12}\,\sqrt{l_{21}^2+2\,l_{12}\,l_{21}-2\,k_{1}\,l_{21}+l_{12}^2+2\,k_{1}\,l_{12}+k_{1}^2}\,n_{10}}{2\,l_{21}^2+4\,l_{12}\,l_{21}-4\,k_{1}\,l_{21}+2\,l_{12}^2+4\,k_{1}\,l_{12}+2\,k_{1}^2}+\frac{k_{1}\,\sqrt{l_{21}^2+2\,l_{12}\,l_{21}-2\,k_{1}\,l_{21}+l_{12}^2+2\,k_{1}\,l_{12}+k_{1}^2}\,n_{10}}{2\,l_{21}^2+4\,l_{12}\,l_{21}-4\,k_{1}\,l_{21}+2\,l_{12}^2+4\,k_{1}\,l_{12}+2\,k_{1}^2}-\frac{l_{21}^2\,n_{10}}{2\,l_{21}^2+4\,l_{12}\,l_{21}-4\,k_{1}\,l_{21}+2\,l_{12}^2+4\,k_{1}\,l_{12}+2\,k_{1}^2}-\frac{2\,l_{12}\,l_{21}\,n_{10}}{2\,l_{21}^2+4\,l_{12}\,l_{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12}^2+4\,k_{1}\,l_{12}+2\,k_{1}^2}+\frac{l_{21}^2\,n_{10}}{2\,l_{21}^2+4\,l_{12}\,l_{21}-4\,k_{1}\,l_{21}+2\,l_{12}^2+4\,k_{1}\,l_{12}+2\,k_{1}^2}+\frac{2\,l_{12}\,l_{21}\,n_{10}}{2\,l_{21}^2+4\,l_{12}\,l_{21}-4\,k_{1}\,l_{21}+2\,l_{12}^2+4\,k_{1}\,l_{12}+2\,k_{1}^2}-\frac{2\,k_{1}\,l_{21}\,n_{10}}{2\,l_{21}^2+4\,l_{12}\,l_{21}-4\,k_{1}\,l_{21}+2\,l_{12}^2+4\,k_{1}\,l_{12}+2\,k_{1}^2}+\frac{l_{12}^2\,n_{10}}{2\,l_{21}^2+4\,l_{12}\,l_{21}-4\,k_{1}\,l_{21}+2\,l_{12}^2+4\,k_{1}\,l_{12}+2\,k_{1}^2}+\frac{2\,k_{1}\,l_{12}\,n_{10}}{2\,l_{21}^2+4\,l_{12}\,l_{21}-4\,k_{1}\,l_{21}+2\,l_{12}^2+4\,k_{1}\,l_{12}+2\,k_{1}^2}+\frac{k_{1}^2\,n_{10}}{2\,l_{21}^2+4\,l_{12}\,l_{21}-4\,k_{1}\,l_{21}+2\,l_{12}^2+4\,k_{1}\,l_{12}+2\,k_{1}^2} \right]$
Out:
$\tag{{\it \%o}_{67}}\left[ \frac{k_{1}\,k_{2}\,n_{10}}{k_{2}\,l_{21}-k_{1}\,l_{21}+k_{2}\,l_{12}-k_{2}^2+k_{1}\,k_{2}}-\frac{k_{1}\,l_{21}\,n_{10}}{k_{2}\,l_{21}-k_{1}\,l_{21}+k_{2}\,l_{12}-k_{2}^2+k_{1}\,k_{2}} \right]$