# Solve a system of equations for functions

If I run the following code:

var('r')
f = function('f')(r)
g = function('g')(r)

eq1 = diff(f,r) + diff(g,r) == 6
eq2 = diff(f,r) - diff(g,r) == 4

solve([eq1,eq2],diff(f,r),diff(g,r))


I get:

TypeError: diff(f(r), r) is not a valid variable.

However, solving eq1 or eq2 independently does provide a solution:

sage: solve(eq1, diff(f,r))
[diff(f(r), r) == -diff(g(r), r) + 6]

sage: solve(eq2, diff(g,r))
[diff(g(r), r) == diff(f(r), r) - 4]


What could be wrong here? Could it be that in a system of equations 'Solve' only provides a solution if the unknowns are variables and not functions (or derivative of functions)?

edit retag close merge delete

Sort by » oldest newest most voted The documentation of the solve function states:

Algebraically solve an equation or system of equations (over the complex numbers) for given variables. Inequalities and systems of inequalities are also supported.

So it should not be expected to work in other cases.

The function desolve_system can be used for systems of first-order ordinary differential equations:

sage: desolve_system([eq1,eq2],[f,g])
[f(r) == 5*r + f(0), g(r) == r + g(0)]

more

1

desolve_system doesn't give the original rburings solution (edited later ?) :

sage: t = SR.var("t")
sage: f, g = function("f, g")
sage: e1 = diff(f(t), t) + diff(g(t), t) == 6
sage: e2 = diff(f(t), t) - diff(g(t), t) == 4
sage: Sol = desolve_system([e1, e2], [f(t), g(t)], ivar=t) ; Sol
[f(t) == 5*t + f(0), g(t) == t + g(0)]


BTW :

sage: from sympy import dsolve, sympify
sage: SSol = [u._sage_() for u in dsolve(*map(sympify, ([e1, e2], [f(t), g(t)])))] ; SSol
[f(t) == C1 + 5*t, g(t) == C2 + t]
sage: mathematica("DSolve[{D[f[t],t] + D[g[t],t] == 6, D[f[t],t] - D[g[t],t] == 4}, {f[t], g[t]}, t]")
{{f[t] -> 5*t + C, g[t] -> t + C}}


Cut n' paste error ?

Using the original notations :

sage: %cpaste
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:var('r')
:f = function('f')(r)
:g = function('g')(r)
:
:eq1 = diff(f,r) + diff(g,r) == 6
:eq2 = diff(f,r) - diff(g,r) == 4
:--
r
sage: desolve_system([eq1,eq2],[f,g])
[f(r) == 5*r + f(0), g(r) == r + g(0)]

1

One notes that the functions appear only differentiated. They can be substituted by temporary variables, and the resulting system solved for them, then integrated. But you don't even need that here ; using my notations :

sage: (e1+e2)/2
diff(f(t), t) == 5
sage: ((e1+e2)/2).integrate(t)
f(t) == c1 + 5*t
sage: ((e1-e2)/2).integrate(t)
g(t) == c2 + t