[Python] Plotting the components of a general solution returned by sympy

Discussão em 'Python' iniciado por Stack, Junho 12, 2025.

  1. Stack

    Stack Membro Participativo

    I have written the following code that returns a solution.

    import sympy as sym
    import numpy as np

    z = sym.Symbol('z')
    e = sym.Symbol('e')
    f = sym.Function('f')

    edo = sym.diff(f(z), z , 2) + e * f(z)
    soln = sym.dsolve(edo, f(z))
    print(soln.rhs)


    the above code returns:

    C1*exp(-z*sqrt(-e)) + C2*exp(z*sqrt(-e))


    I want to be able to access the elements of this 'soln.rhs' directly and plot them. I could copy and paste the results, but I want to do something like:

    plot(x, soln.rhs[0])


    which would plot exp(-z*sqrt(-e))

    The reason for this is that I am analysing many different types of ODE, some of which return solutions which are combinations of Airy functions like

    C1*airyai(-e + z) + C2*airybi(-e + z)


    Does anyone know how to access the elements of the general solution? I have looked through the documentation and nothing really pops out.

    Continue reading...

Compartilhe esta Página