0

How can I solve multiple ODEs? sympy.dsolve returns the same integrations constants, so I cannot solve it.

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
from sympy.interactive import printing
printing.init_printing(use_latex=True)
from sympy import *

x,l,F,k_0,q_0,n,a,C1,C2,C3,C4,Φ1,Φ2,D1,D2=symbols('x l F k_0 q_0 n a C1 C2 C3 C4 Φ_1 Φ_2 D1 D2')
n=5

q=0
k1=k_0
k2=n*k_0

eq1=Function('eq1')(x)
eq2=Function('eq2')(x)

w1=dsolve(k1.diff(x)*eq1.diff(x)+k1*eq1.diff(x,2)+q).rhs
w2=dsolve(k2.diff(x)*eq2.diff(x)+k2*eq2.diff(x,2)+q).rhs

display(w1)
display(w2)

The code returns:

1+2*

1+2*

While I would want something like:

1+2*

3+4*

mehfluffy
  • 49
  • 1
  • 9
Mark
  • 25
  • 5
  • Why not solve it as a system? Note that `k1` is a constant, so `k1.diff(x)` will be zero. Also, sympy is not complete for all the standard cases of scalar ODE and rather limited for systems of ODE, esp. non-linear ones. So even in the rare case that there is a symbolic solution for a system, there is only a small chance that sympy will find it (correctly). – Lutz Lehmann Dec 26 '19 at 12:31

1 Answers1

0

I've found this as an answer, but maybe there is something simpler

char='D'
diff=k2.diff(x)*eq2.diff(x)+k2*eq2.diff(x,2)+q

def ODE(diff,char):
    func=dsolve(diff).rhs
    for i in range(1,len(func.free_symbols)):
        old=symbols('C{}'.format(i))
        new=symbols(char + '{}'.format(i))
        func=func.subs(old,new)
    return func

w2=ODE(diff,char)
display(w2)
Mark
  • 25
  • 5