First time here? Check out the FAQ!

Ask Your Question
1

Solve a system of equations for functions

asked 3 years ago

keko gravatar image

updated 3 years ago

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

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
0

answered 3 years ago

rburing gravatar image

updated 3 years ago

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)]
Preview: (hide)
link

Comments

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[1], g[t] -> t + C[2]}}

Cut n' paste error ?

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 3 years ago )

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)]
Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 3 years ago )
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
Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 3 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 3 years ago

Seen: 386 times

Last updated: Jan 17 '22