BLACK FRIDAY EXTRA SAVINGS EVENT - EXTENDED
START FREE

Solving derivatives with Sympy package

Hello guys,

resolving the mission: https://app.dataquest.io/m/159/finding-extreme-points/6/power-rule
I think it would be nice to perform the examples of the mission by using the previously managed package Sympy. Here it is an example that could be useful for new users.

Code:

import sympy
x, y = sympy.symbols('x y')
derivative_one = sympy.diff(x**5, x)
derivative_two = sympy.diff(x**9, x)
slope_one = int(derivative_one.subs(x, 2))
slope_two = int(derivative_one.subs(x, 0))

Notice that the int function is provided just for answer checking purposes.

I would like to complete the section with another possible resolution for the Finding Extreme Values task using Sympy. The following few lines could be useful to compute critical points and assing them as relative minimum or relative maximum, taking into account:

  • When the slope transitions from positive to negative at a point, it can be a maximum value.
  • When the slope transitions from negative to positive at a point, it can be a minimum value.
import sympy

x = sympy.symbols('x')
derivative = sympy.diff(x**3-x**2, x)
critical_points = sympy.solvers.solve(derivative, x)
h = 0.025
rel_min = list()
rel_max = list()

for point in critical_points:        
    before_value = derivative.subs(x, point-h)
    after_value = derivative.subs(x, point+h)
    if before_value < 0 and after_value > 0:
        rel_min.append(point)
    elif before_value > 0 and after_value < 0:
        rel_max.append(point)

Greetings and happy learning.

5 Likes

Sounds good @DavidCamino. Do you mind filling up a ticket here as part of your content suggestions? Thanks