Search code examples
pythonderivative

Higher Order Derivatives


I am trying to write a python program in which the user inputs the polynomial and it calculates the derivative. My current code is:

print ("Derviatives: ")
k5 = raw.input("Enter 5th degree + coefficent:     ")
k4 = raw.input("Enter 4th degree + coefficent:     ")
k3 = raw.input("Enter 3rd degree + coefficent:     ")
k2 = raw.input("Enter 2nd degree + coefficent:     ")
k1 = raw.input("Enter 1st degree + coefficent:     ")
k0 = raw.input("Enter constant:    ")
int(k5)
int(k4)
int(k3)
int(k2)
int(k1)
int(k0)
print (k5, "  ", k4, "  ", k3, "  ", k2, "   ", k1, "  ", k0)
1in =  raw.input("Correct Y/N?")
if (1in != Y)
    k5 = raw.input("Enter 5th degree + coefficent:     ")
    k4 = raw.input("Enter 4th degree + coefficent:     ")
    k3 = raw.input("Enter 3rd degree + coefficent:     ")
    k2 = raw.input("Enter 2nd degree + coefficent:     ")
    k1 = raw.input("Enter 1st degree + coefficent:     ")
    k0 = raw.input("Enter constant:    ")   

else
    """CODE GOES HERE"""

I am just a beginning python programmer so I am still a little bit fuzzy on some basic syntax issues. Are there any libraries that I should be importing?


Solution

  • Use Sympy:

    Put these two lines at the top:

    from sympy import *
    import numpy as np
    

    And these at the part """CODE GOES HERE"""

    x = Symbol('x')
    y = k5*x**5  + k4*x**4 + k3*x**3  + k2*x**2  + k1*x  +constant
    yprime = y.diff(x)
    yprime