Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sympy dsolve answers depends on the order of the input?

Tags:

python

sympy

Why do the answers that sympy's dsolve function gives, depend on the order in which the equations are given?

This order (x, then y, then z):

    k2, k3, t = symbols('k2 k3 t')
    x,y,z = symbols('x y z', function=True)

    eq = (Eq(Derivative(x(t),t), k3*y(t)), Eq(Derivative(y(t),t), -(k3+k2)*y(t)), Eq(Derivative(z(t),t), k2*y(t)))
    dsolve(eq)

gives:

   [Eq(x(t), C1 + C2*t + C3*k3*exp(t*(-k2 - k3))/k2), Eq(y(t), -C3*(k2 + k3)*exp(t*(-k2 - k3))/k2), Eq(z(t), C2 + C3*exp(t*(-k2 - k3)))]

But when the order of eq is change (first z, then x, then y):

    k2, k3, t = symbols('k2 k3 t')
    x,y,z = symbols('x y z', function=True)

    eq = (Eq(Derivative(z(t),t), k2*y(t)), Eq(Derivative(x(t),t), k3*y(t)), Eq(Derivative(y(t),t), -(k3+k2)*y(t)))

    dsolve(eq, ics={z(0):0, x(0):0, y(0):1})

the answer is different.

[Eq(z(t), C1 + C2*t - C3*k2*exp(t*(-k2 - k3))/(k2 + k3)), Eq(x(t), C2 - C3*k3*exp(t*(-k2 - k3))/(k2 + k3)), Eq(y(t), C3*exp(t*(-k2 - k3)))]

Besides that, is the answer that dsolve gives not correct in both cases. Especially the constants C1, C2 and C3 are not correct. Giving the initial conditions as ics={x(0):0, y(0):1,...etc doesn't work or influence the answer.

like image 894
Viktor van der Valk Avatar asked Jan 25 '26 23:01

Viktor van der Valk


1 Answers

Apparently, the logic of the solver for this type of systems is incorrect; I raised an issue.

Aside: the constants in a solution of a system of differential equations can be presented in multiple ways, there is not a single canonical form of them. It can be expected that the order in which equations are presented will influence the order in which they are processed (like it happens with systems of linear equations), which will affect the form of solution.

But, as you point out, neither of two solutions is correct. This can be checked by plugging them in:

eq1 = (Eq(Derivative(x(t),t), k3*y(t)), Eq(Derivative(y(t),t), -(k3+k2)*y(t)), Eq(Derivative(z(t),t), k2*y(t)))
sol1 = dsolve(eq1)
print([eqn.subs({e.lhs: e.rhs for e in sol1}).doit().simplify() for eqn in eq1])
eq2 = (Eq(Derivative(z(t),t), k2*y(t)), Eq(Derivative(x(t),t), k3*y(t)), Eq(Derivative(y(t),t), -(k3+k2)*y(t)))
sol2 = dsolve(eq2)
print([eqn.subs({e.lhs: e.rhs for e in sol2}).doit().simplify() for eqn in eq2])

The first prints

[Eq(C3*k3*(k2 + k3)*exp(-t*(k2 + k3))/k2, -(C2*k2*exp(t*(k2 + k3)) - C3*k3*(k2 + k3))*exp(-t*(k2 + k3))/k2),
 True,
 True]

indicating that the 2nd and 3rd equations are satisfied, but the 1st is not.

The second prints

[Eq(C3*k2*exp(-t*(k2 + k3)), C2 + C3*k2*exp(-k2*t)*exp(-k3*t)), True, True]

So once again, the first equation fails to be solved correctly, even though it's a different one now.

Giving the initial conditions as ics={x(0):0, y(0):1,...etc doesn't work or influence the answer.

The support for initial conditions was added after SymPy 1.1.1 was released. They work in development version and will work in SymPy 1.2+.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!