Home > Back-end >  How can I use a while loop in Python using Sympy to differentiate an equation 5 times, each run diff
How can I use a while loop in Python using Sympy to differentiate an equation 5 times, each run diff

Time:10-29

I got stuck on a question where I needed to import Sympy and differentiate Y = x^8 3x^7 3x^5 15 five times using a while loop, each loop differentiating the equation once. I got stuck at:

import sympy as sym

y = ( x**8   3*x**7   3*x**5   15 )

while y:
    y = sym.diff ( x**8   3*x**7   3*x**5   15 )

Any takers? I'm a newbie and don't really know how to approach this. Thanks.

CodePudding user response:

y is not a True false statement you should use

import sympy as sym

y = ( x**8   3*x**7   3*x**5   15 )
i = 0

while i<5:
    y = sym.diff ( x**8   3*x**7   3*x**5   15 )
    i =1

ps.im not familiar with sympy so I cant correct your y

CodePudding user response:

import sympy as sym

x = sym.symbols('x')
y_next =  x**8   3*x**7   3*x**5   15

i=0
while i<5:
    y_next = sym.diff ( y_next )
    i = i   1
    print(y_next)

Result:

8*x**7   21*x**6   15*x**4
56*x**6   126*x**5   60*x**3
336*x**5   630*x**4   180*x**2
1680*x**4   2520*x**3   360*x
6720*x**3   7560*x**2   360
  • Related