-1

I have a function which creates a string of a polynomial which has the coefficients in a list lets say lst1. I have to create a function which can create the derivative form of the polynomial. For the integral I have to have be able to evaluate the integral of the polynomial according to the upper and lower bound asked. I am struggling to form functions for this, can anyone help me out with this and guide me?

gboffi
  • 22,939
  • 8
  • 54
  • 85
Jon
  • 27
  • 4
  • Is this a general task or a class-related one? ie. can you just use a 3rd-party solver like [sympy](https://docs.sympy.org/latest/tutorial/calculus.html) or are you attempting to derive the values yourself? What have you tried so far? Please provide example inputs and outputs – ti7 Mar 01 '21 at 21:30
  • @Jon--1) For the derivative are you asking for the coefficients of the derivative? 2) Looks like for the integral you are expecting the area under the curve--is that correct? – DarrylG Mar 01 '21 at 21:39
  • 1
    @ti7 This is a class related task and I won't be able to use sympy – Jon Mar 01 '21 at 22:06
  • @DarrylG Yes for the integral I am expecting the area under the curve, and for the derivative I would just require coefficients since I have a function that will return a polynomial once fed a list – Jon Mar 01 '21 at 22:07
  • Anecdotally, you may be interested in this [YouTube video](https://www.youtube.com/watch?v=PNKj529yY5c) and question of mine on the subject of the first paper to do this symbolically: https://stackoverflow.com/questions/65650999/how-can-i-convince-sympy-to-come-to-the-same-simplification-saint-does-for-the-1 – ti7 Mar 01 '21 at 22:22

1 Answers1

2

Consider writing a given polynom as follows: 3 + 7x^2 - 4x^5 = [3, 0, 7, 0, 0, -4] .

For the derivate:

def derivateit(lst1):
    deriv_poly = [lst1[i]*i for i in range(1, len(lst1))]
    return deriv_poly

For the integral part:

def integrateit(lst1, X_0, X_1):
    integral = 0
    for i in range(len(lst1)):
        integral += (lst1[i]*(X_1)**(i+1))/(i+1) - (lst1[i]*(X_0)**(i+1))/(i+1)
    return integral

Where lst1 is your polynom, x_0 and x_1 your upper and lower limits.

  • `integrateit([0,1], 0, 1)` should be 0.5, but this reports 1. That is, the integral of x between 0 and 1 is 0.5. – DarrylG Mar 01 '21 at 22:22
  • Thank you for pointing that out! Code is working now. – Wilhelmroentgen Mar 02 '21 at 00:30
  • @Wilhelmroentgen my list has coefficients in reversed order, like 5x^2+2x+7 will be in list [5,2,7]. How does this change your code? – Jon Mar 02 '21 at 08:18
  • 1
    Use the `lst1.reverse()` method to reverse the order of your list. Here the documentation: https://www.python.org/dev/peps/pep-0322/ – Wilhelmroentgen Mar 02 '21 at 13:18