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.Wed, 01 Dec 2010 10:41:31 +0100Differential system of equations in Sagehttps://ask.sagemath.org/question/7782/differential-system-of-equations-in-sage/Greetings all!
This is a "how do I do it in Sage" question. Let's say I have the following system of differential equations:
(a, b, p, r, and h are ALL functions of t)
eqn1 = diff(a,t) == 3*p
eqn2 = diff(b,t) == 4 + r
eqn3 = h == a^2 + b^2
NOW, I want to differentiate h from equation three (using implicit differentiation).
The "answer" would be: diff(h,t) = 2 * a * diff(a,t) + 2 * b * diff(b,t)
(note that we DO know what diff(a,t) and diff(b,t) are from eqn1 and eqn2)
--> How can I get sage to differentiate eqn3, AND plug in the equations from eqn1 and eqn2 for the time derivatives of a and b?
Wed, 01 Dec 2010 09:32:13 +0100https://ask.sagemath.org/question/7782/differential-system-of-equations-in-sage/Answer by kcrisman for <p>Greetings all!</p>
<p>This is a "how do I do it in Sage" question. Let's say I have the following system of differential equations:</p>
<p>(a, b, p, r, and h are ALL functions of t)</p>
<p>eqn1 = diff(a,t) == 3*p</p>
<p>eqn2 = diff(b,t) == 4 + r</p>
<p>eqn3 = h == a^2 + b^2</p>
<p>NOW, I want to differentiate h from equation three (using implicit differentiation).</p>
<p>The "answer" would be: diff(h,t) = 2 * a * diff(a,t) + 2 * b * diff(b,t)
(note that we DO know what diff(a,t) and diff(b,t) are from eqn1 and eqn2) </p>
<p>--> How can I get sage to differentiate eqn3, AND plug in the equations from eqn1 and eqn2 for the time derivatives of a and b?</p>
https://ask.sagemath.org/question/7782/differential-system-of-equations-in-sage/?answer=11806#post-id-11806This is a partial answer, but maybe it will help people do the second thing you want. I don't know if the obvious tuple thing will let you make all of these functions of t at once.
sage: var('t')
t
sage: h=function('h',t)
sage: a=function('a',t)
sage: b=function('b',t)
sage: p=function('p',t)
sage: r=function('r',t)
sage: diff(a^2+b^2,t)
2*a(t)*D[0](a)(t) + 2*b(t)*D[0](b)(t)
sage: z = diff(a^2+b^2,t)
sage: z.operands()[0].operands()[1]
D[0](a)(t)
sage: z.operands()[0].operands()[1].operator()
D[0](a)
sage: y = z.operands()[0].operands()[1]
sage: y
D[0](a)(t)
sage: z.substitute_expression({y:3*p})
6*a(t)*p(t) + 2*b(t)*D[0](b)(t)
but I can't think of a way to substitute with 3*p for y directly:
sage: z.substitute_expression({D[0](a)(t):3*p})
---------------------------------------------------------------------------
NameError: name 'D' is not defined
Maybe this should be implemented somehow? I hate these sorts of pattern-matching things.Wed, 01 Dec 2010 10:41:31 +0100https://ask.sagemath.org/question/7782/differential-system-of-equations-in-sage/?answer=11806#post-id-11806