Determine recursive Python derivative calculus

Asked

Viewed 2,728 times

0

I need to create an algorithm so that it determines in N times the derivative of a X function.

However I still have some problems to implement recursive functions, I still can’t get a good idea of the logic.

To determine the derivative of the function I am using:

diff(função)

I was thinking about putting one

for(x = 0; x < VezesUsuario; x++){ derivar } 

But I’m not sure how I’d do to return the result of the previous calculation and calculate its derivative. I have the requirement to make it recursive, that’s my problem actually.

  • What you need to do is calculate the n-th derivative?

  • @Andersoncarloswoss this! So that is recursive, I had the idea to make an IF, but it did not meet the requirements.

1 answer

4


One of the ways you can do it is:

  • Receive in a function diff the function to be derived, f, and the derivation order, N;
  • Calculate the derivative g of order 1 of function f;
  • If derivation order N equals 1, return function g;
  • Otherwise, return the N-1 order derivative of the g function;

So the code would look like:

def diff(func, N=1):
    # Calcula a derivada de ordem 1
    # g = func'

    return g if N == 1 else diff(g, N-1)

So if you need the second derivative, it will be:

  • The order derivative 1 shall be calculated;
  • Since N is greater than 1, the value of the derivative of order 1 of the derivative will be returned;
  • When calculating the derivative of the derivative, N will be 1 and the derivative itself will be returned;
  • The final value will be the order 2 derivative of the input function;

I leave you the challenge of writing a Table test to calculate the derivative of order 5 or higher.


For example, considering a monomial composed of coefficient and exponent:

class Monomial:

    coefficient: float
    exponent: int

    def __init__(self, coefficient, exponent):
        self.coefficient = coefficient
        self.exponent = exponent

    def __str__(self):
        return f'{self.coefficient}x^{self.exponent}'

    def __diff__(self):
        coefficient = self.coefficient * self.exponent
        exponent = self.exponent - 1

        return Monomial(coefficient, exponent)

We can define the monomial 2x^5 making:

p = Monomial(2, 5)

print('Monômio:', p)  # Monômio: 2x^5

Define the function diff as:

def diff(func, N=1):
    g = func.__diff__()

    return g if N == 1 else diff(g, N-1)

Thus, to calculate the third derivative of p, just do q = diff(p, 3), getting 120x^2

p = Monomial(2, 5)

print('Polinômio:', p)  # Polinômio: 2x^5

print('Derivada de ordem 1:', diff(p, 1))  # Derivada de ordem 1: 10x^4
print('Derivada de ordem 2:', diff(p, 2))  # Derivada de ordem 2: 40x^3
print('Derivada de ordem 3:', diff(p, 3))  # Derivada de ordem 3: 120x^2
print('Derivada de ordem 4:', diff(p, 4))  # Derivada de ordem 4: 240x^1
print('Derivada de ordem 5:', diff(p, 5))  # Derivada de ordem 5: 240x^0
print('Derivada de ordem 6:', diff(p, 6))  # Derivada de ordem 6: 0x^-1

See working on Repl.it

  • Thank you very much Anderson, I managed to solve my exercises, your demonstration helped me a lot!

  • I saw this monomial rising from a polynomial...

  • @Jeffersonquesado Shhhh, nobody saw anything...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.