Search code examples
pythonsympydsolve

How write the symbolic variable in the sympy EDO dsolve


Good afternoon,

I'm coming here as I noticed something unusual in the results of dsolve() in sympy.

from sympy import *
from sympy.abc import x,y
import sympy as s
import numpy as np

n = symbols('n', complex=True)

s.init_printing()
f=Function('x')

eq=Derivative(f(x),x,x)+n**2*f(x)
a=dsolve(eq, f(x))

eq2=Derivative(f(x),x,x)+2**2*f(x)
a2=dsolve(eq2, f(x))

display(a.subs(n,2)==a2)

The generated result is False.

Looking only at the result of 'a' it is already possible to see that there are differences in the results using the symbolic variable 'n'.

Could anyone guide if I'm doing it the right way?


Solution

  • The solution sets are equivalent:

    In [2]: a
    Out[2]: 
               -ⅈ⋅n⋅x       ⅈ⋅n⋅x
    x(x) = C₁⋅ℯ       + C₂⋅ℯ     
    
    In [3]: a2
    Out[3]: x(x) = C₁⋅sin(2⋅x) + C₂⋅cos(2⋅x)
    

    These are just different ways of writing the general solution. If you had declared n to be real then the sin/cos form would be used.

    The two forms are related by Euler's formula: https://en.wikipedia.org/wiki/Linear_differential_equation#Second-order_case