[Python] How do I adjust my code so that the sympy command 'subs' substitutes the correct...

Discussão em 'Python' iniciado por Stack, Fevereiro 9, 2025.

  1. Stack

    Stack Membro Participativo

    I'm having trouble using the subs command in Python using the Sympy module. It's like the command doesn't exist. The question in the code is about the chain rule when x and y depend on a common variable.

    Here is my code:

    import sympy as sp
    from sympy import E
    import math

    x1, y1, t1 = sp.symbols('x y t')

    # Question 2

    # Method 1

    pi = math.pi

    f2 = E**y1 * sp.cos(x1)

    dfdx = sp.diff(f2, x1)

    dfdy = sp.diff(f2, y1)

    xt = (2*pi / 3)*t1 + pi/4
    dxdt = sp.diff(xt, t1)

    yt = -t1
    dydt = sp.diff(yt, t1)

    dfdt = dfdx * dxdt + dfdy * dydt
    print(dfdt.subs({x1 : xt}, {y1 : yt}))


    My output is just the original dfdt in terms of x and y and not in terms of t:

    PS C:\Users\maxko\OneDrive\Pulpit\Python> & C:/Users/maxko/AppData/Local/Programs/Python/Python311/python.exe "c:/Users/maxko/OneDrive/Pulpit/Python/Lab 2.py"
    -2.0943951023932*exp(y)*sin(x) - exp(y)*cos(x)


    Follow up question, is there any way to prettify that long integer? Maybe keep it in terms of pi or just 3 sig figs?

    I was expecting for the subs command to substitute the 'x' and 'y' in the 'dfdt' for the 'xt' and 'yt' that were defined above.

    Continue reading...

Compartilhe esta Página