# 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?

edit retag close merge delete

Sort by » oldest newest most voted

This 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(a)(t) + 2*b(t)*D(b)(t)
sage: z = diff(a^2+b^2,t)
sage: z.operands().operands()
D(a)(t)
sage: z.operands().operands().operator()
D(a)
sage: y = z.operands().operands()
sage: y
D(a)(t)
sage: z.substitute_expression({y:3*p})
6*a(t)*p(t) + 2*b(t)*D(b)(t)


but I can't think of a way to substitute with 3*p for y directly:

sage: z.substitute_expression({D(a)(t):3*p})
---------------------------------------------------------------------------
NameError: name 'D' is not defined


Maybe this should be implemented somehow? I hate these sorts of pattern-matching things.

more