ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 23 Jul 2011 03:29:42 +0200Solving a System of Differential Equationshttps://ask.sagemath.org/question/8239/solving-a-system-of-differential-equations/I am attempting to solve and graph the solution to an initial value problem containing a system of differential equations. If I am remembering calculus correctly, its properties (nonlinear, ordinary, no explicit appearance of the independent variable time) classify it as a 'time-invariant autonomous system'.<br>
I want to model a situation in which a microorganism in a bioreactor at some concentration 'X' is growing exponentially 'dX/dt = u*X' by consuming a substrate at some concentration 'S'.<br>
Substrate consumption '-dS/dt' and microorganism growth rate 'dX/dt' are related by a constant representing the yield of biomass on substrate, Y. If I stop here and leave the specific growth rate 'u' a constant, I can evaluate this just fine with **desolve_system()**:<br>
![image description](https://lh6.googleusercontent.com/-YrUX99Qnkkg/TidnGlC0d3I/AAAAAAAAABE/rME_HWK4sfM/s912/Screenshot-3.png)
The problems start when I make 'u' (specific growth rate) a function of 'S' (substrate concentration):<br> u = u_max*S/(K_m+S).<br>
I make the assumption that this must be evaluated numerically, and switch to **desolve_system_rk4()**. I get an "Error executing code in Maxima", which also states "in definition f_rk_4, found bad argument 'X(t)"
![image description](https://lh6.googleusercontent.com/-bGVea6dD6tc/Tig4WzV6qvI/AAAAAAAAABc/Bg-0WHwFTWc/Screenshot-4.png)
Where am I going wrong? Can I use desolve_system_rk4() to evaluate this system, and I'm just making a syntax error?
Thank you for your patience - I just started using SAGE yesterday.
- BrandonWed, 20 Jul 2011 20:44:36 +0200https://ask.sagemath.org/question/8239/solving-a-system-of-differential-equations/Answer by benjaminfjones for <p>I am attempting to solve and graph the solution to an initial value problem containing a system of differential equations. If I am remembering calculus correctly, its properties (nonlinear, ordinary, no explicit appearance of the independent variable time) classify it as a 'time-invariant autonomous system'.<br/></p>
<p>I want to model a situation in which a microorganism in a bioreactor at some concentration 'X' is growing exponentially 'dX/dt = u*X' by consuming a substrate at some concentration 'S'.<br/></p>
<p>Substrate consumption '-dS/dt' and microorganism growth rate 'dX/dt' are related by a constant representing the yield of biomass on substrate, Y. If I stop here and leave the specific growth rate 'u' a constant, I can evaluate this just fine with <strong>desolve_system()</strong>:<br/>
<img alt="image description" src="https://lh6.googleusercontent.com/-YrUX99Qnkkg/TidnGlC0d3I/AAAAAAAAABE/rME_HWK4sfM/s912/Screenshot-3.png"/></p>
<p>The problems start when I make 'u' (specific growth rate) a function of 'S' (substrate concentration):<br/> u = u_max*S/(K_m+S).<br/></p>
<p>I make the assumption that this must be evaluated numerically, and switch to <strong>desolve_system_rk4()</strong>. I get an "Error executing code in Maxima", which also states "in definition f_rk_4, found bad argument 'X(t)"</p>
<p><img alt="image description" src="https://lh6.googleusercontent.com/-bGVea6dD6tc/Tig4WzV6qvI/AAAAAAAAABc/Bg-0WHwFTWc/Screenshot-4.png"/></p>
<p>Where am I going wrong? Can I use desolve_system_rk4() to evaluate this system, and I'm just making a syntax error?</p>
<p>Thank you for your patience - I just started using SAGE yesterday.</p>
<ul>
<li>Brandon</li>
</ul>
https://ask.sagemath.org/question/8239/solving-a-system-of-differential-equations/?answer=12527#post-id-12527That function `desolve_system_rk4` is very badly documented. I would suggest filing a bug report on the documentation.
That said, I think the answer to your question is that you have entered the DE in the wrong way. The list of expressions you pass to the solver need to be the right hand sides of equations you get after solving for $X'(t)$ and $S'(t)$ respectively. So instead of $\frac{-X'(t)}{Y}$ for the second expression, you want $$\frac{-0.01*X*S}{2+S}$$ . Then you don't need to define $X(t)$ and $S(t)$ as symbolic functions (which is I think where the error lies in your screenshot), they can remain just "variables". Here's an example:
sage: (X,S,t) = var('X,S,t')
sage: des = [0.01*X*S/(2+S), -0.01*X*S/(2+S)]
sage: desolve_system_rk4(des, [X,S], ics=[0,0.1,10], ivar=t, end_points=20)
[[0, 0.100000000000000, 10], [0.1, 0.100083368007, 9.99991663199],
[0.2, 0.100166805401, 9.9998331946], [0.3, 0.100250312238, 9.99974968776],
[0.4, 0.100333888577, 9.99966611142], [0.5, 0.100417534474, 9.99958246553],
...Sat, 23 Jul 2011 03:26:50 +0200https://ask.sagemath.org/question/8239/solving-a-system-of-differential-equations/?answer=12527#post-id-12527Comment by benjaminfjones for <p>That function <code>desolve_system_rk4</code> is very badly documented. I would suggest filing a bug report on the documentation. </p>
<p>That said, I think the answer to your question is that you have entered the DE in the wrong way. The list of expressions you pass to the solver need to be the right hand sides of equations you get after solving for $X'(t)$ and $S'(t)$ respectively. So instead of $\frac{-X'(t)}{Y}$ for the second expression, you want $$\frac{-0.01<em>X</em>S}{2+S}$$ . Then you don't need to define $X(t)$ and $S(t)$ as symbolic functions (which is I think where the error lies in your screenshot), they can remain just "variables". Here's an example:</p>
<pre><code>sage: (X,S,t) = var('X,S,t')
sage: des = [0.01*X*S/(2+S), -0.01*X*S/(2+S)]
sage: desolve_system_rk4(des, [X,S], ics=[0,0.1,10], ivar=t, end_points=20)
[[0, 0.100000000000000, 10], [0.1, 0.100083368007, 9.99991663199],
[0.2, 0.100166805401, 9.9998331946], [0.3, 0.100250312238, 9.99974968776],
[0.4, 0.100333888577, 9.99966611142], [0.5, 0.100417534474, 9.99958246553],
...
</code></pre>
https://ask.sagemath.org/question/8239/solving-a-system-of-differential-equations/?comment=21450#post-id-21450I don't know why the LaTeX expressions are giving me trouble in the answer... Maybe someone can point out the typesetting problem.Sat, 23 Jul 2011 03:29:42 +0200https://ask.sagemath.org/question/8239/solving-a-system-of-differential-equations/?comment=21450#post-id-21450Answer by niles for <p>I am attempting to solve and graph the solution to an initial value problem containing a system of differential equations. If I am remembering calculus correctly, its properties (nonlinear, ordinary, no explicit appearance of the independent variable time) classify it as a 'time-invariant autonomous system'.<br/></p>
<p>I want to model a situation in which a microorganism in a bioreactor at some concentration 'X' is growing exponentially 'dX/dt = u*X' by consuming a substrate at some concentration 'S'.<br/></p>
<p>Substrate consumption '-dS/dt' and microorganism growth rate 'dX/dt' are related by a constant representing the yield of biomass on substrate, Y. If I stop here and leave the specific growth rate 'u' a constant, I can evaluate this just fine with <strong>desolve_system()</strong>:<br/>
<img alt="image description" src="https://lh6.googleusercontent.com/-YrUX99Qnkkg/TidnGlC0d3I/AAAAAAAAABE/rME_HWK4sfM/s912/Screenshot-3.png"/></p>
<p>The problems start when I make 'u' (specific growth rate) a function of 'S' (substrate concentration):<br/> u = u_max*S/(K_m+S).<br/></p>
<p>I make the assumption that this must be evaluated numerically, and switch to <strong>desolve_system_rk4()</strong>. I get an "Error executing code in Maxima", which also states "in definition f_rk_4, found bad argument 'X(t)"</p>
<p><img alt="image description" src="https://lh6.googleusercontent.com/-bGVea6dD6tc/Tig4WzV6qvI/AAAAAAAAABc/Bg-0WHwFTWc/Screenshot-4.png"/></p>
<p>Where am I going wrong? Can I use desolve_system_rk4() to evaluate this system, and I'm just making a syntax error?</p>
<p>Thank you for your patience - I just started using SAGE yesterday.</p>
<ul>
<li>Brandon</li>
</ul>
https://ask.sagemath.org/question/8239/solving-a-system-of-differential-equations/?answer=12533#post-id-12533I don't know how to do this. Could you try re-defining `f` in the first place, without using `mu` as a variable but instead using the expression you've given?
Also, your question might be easier for someone else to answer if you copy/paste the function definitions (using the code format button to format it correctly here). Then people who want to help can just copy/paste to get their own versions to test with.Fri, 22 Jul 2011 10:11:41 +0200https://ask.sagemath.org/question/8239/solving-a-system-of-differential-equations/?answer=12533#post-id-12533