Home:ALL Converter>Solving set of coupled differential equations with sympy

Solving set of coupled differential equations with sympy

Ask Time:2020-05-28T21:10:53         Author:pongo

Json Formatter

I have the following set of coupled differential equations. I want to get an analytical solution with sympy.

from sympy import *
import numpy as np

init_printing(use_unicode=True)
x, y, z, t, w,  V=symbols('x y z t omega V')
c1=Function('c1')
c2=Function('c2')
hq=symbols('hbar',positive=True)

g1=Eq(c2(t)*hq*V*exp(-I*w*t),I*hq*Derivative(c1(t),t))
g2=Eq(c1(t)*hq*V*exp(+I*w*t),I*hq*Derivative(c2(t),t))

eq=(g1,g2)

dsolve(eq,hint='all',ics={c1(0):1,c2(0):0})

When I try to solve the equation system, I get the error:

ValueError: The function cannot be automatically detected for nan.

Unfortunately I can not see my mistake.

Edit:

classify_ode(g1) returns the following hints:

('factorable', 'nth_algebraic', 'separable', '1st_exact', '1st_linear', 'Bernoulli', '1st_power_series', 'lie_group', 'nth_linear_constant_coeff_variation_of_parameters', 'nth_linear_euler_eq_nonhomogeneous_variation_of_parameters', 'nth_algebraic_Integral', 'separable_Integral', '1st_exact_Integral', '1st_linear_Integral', 'Bernoulli_Integral', 'nth_linear_constant_coeff_variation_of_parameters_Integral', 'nth_linear_euler_eq_nonhomogeneous_variation_of_parameters_Integral')

Every one of them produces the error mentioned above.

Manually, one can solve this system by applying the Laplace transform to c1 and c2. This turns the system of ODEs in a system of purely algebraic equations, which is solvable by rearranging and eliminating the coupling between equations. The result is then the transform of the solution, so one has to transform back using the inverse Laplace transform.

Edit 2:

classify_sysode(eq) returns the following:

{'no_of_equation': 2,
 'eq': [-c1(t)*exp(-I*omega*t) + I*Derivative(c2(t), t),
  -c2(t)*exp(I*omega*t) + I*Derivative(c1(t), t)],
 'func': [c2(t), c1(t)],
 'order': {c1(t): 1, c2(t): 1},
 'func_coeff': {(0, c2(t), 0): 0,
  (0, c2(t), 1): I,
  (0, c1(t), 0): -exp(-I*omega*t),
  (0, c1(t), 1): 0,
  (1, c2(t), 0): -exp(I*omega*t),
  (1, c2(t), 1): 0,
  (1, c1(t), 0): 0,
  (1, c1(t), 1): I},
 'is_linear': True,
 'type_of_equation': 'type6'}

This means that the solver that will be used is Linear, 2 equations, Order 1, Type 6, for a system of the form:

x' = f(t) x + g(t) y
y' = a [f(t) + a h(t)] x + a [g(t) - h(t)] y

But our system looks more like a Type 7, that is, of the form:

x' = f(t) x + g(t) y
y' = h(t) x + p(t) y

with f(t) and p(t) being zero. The suggested method of solution for a type 7 mentioned in the documentation also resembles what Lutz Lehmann mentions in his comment.

For completeness this is the error, which seems to originate from an exception raised by _preprocess:

Traceback (most recent call last):
  File "problem_ode.py", line 21, in <module>
    dsolve(eq,hint='all',ics={c1(0):1,c2(0):0})
  File "/home/quoniam/anaconda3/envs/data/lib/python3.8/site-packages/sympy/solvers/ode.py", line 634, in dsolve
    sols = solvefunc(match)
  File "/home/quoniam/anaconda3/envs/data/lib/python3.8/site-packages/sympy/solvers/ode.py", line 7405, in sysode_linear_2eq_order1
    sol = _linear_2eq_order1_type6(x, y, t, r, eq)
  File "/home/quoniam/anaconda3/envs/data/lib/python3.8/site-packages/sympy/solvers/ode.py", line 7731, in _linear_2eq_order1_type6
    hint1 = classify_ode(equ)[1]
  File "/home/quoniam/anaconda3/envs/data/lib/python3.8/site-packages/sympy/solvers/ode.py", line 976, in classify_ode
    eq, func_ = _preprocess(eq, func)
  File "/home/quoniam/anaconda3/envs/data/lib/python3.8/site-packages/sympy/solvers/deutils.py", line 84, in _preprocess
    raise ValueError('The function cannot be '
ValueError: The function cannot be automatically detected for nan.

Author:pongo,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/62065599/solving-set-of-coupled-differential-equations-with-sympy
yy