Issue
I am asked to find the maxima and minima of few functions. One of the functions is y = (9x^3) - (7x^2) + (3x) + 10 The code I could write so far is:
from sympy import*
import matplotlib.pyplot as plt
x = symbols ('x')
f = (9*x**3) - (7*x**2) + (3*x) + 10
intervals = np.arange(-5, 7)
df = diff(f, x)
df2 = diff(f, x, 2)
f = lambdify (x, f)
y = f(intervals)
print (intervals)
print (y)
I am new to using these 3 libraries so I dont know how to find the answer using these 3 libraries
Solution
SymPy can tell you the derivative and f
and can tell you when a function is zero. Since max/min occur when the derivative is zero you can find the zeros and then determine if those values make the 2nd derivative positive or negative, e.g.
>>> from sympy import real_roots
>>> from sympy.abc import x
>>> f = -x**2 + 1
>>> d1 = f.diff(x)
>>> d2 = d1.diff(x) # = f.diff(x,2)
>>> extrema = real_roots(d1)
>>> for i in extrema:
... if d2.subs(x, i).is_positive:
... print('minimum',i)
... else:
... print('maxima',i)
See also here. (If there are no real roots then there are no extrema for real values of x
.)
Answered By - smichr
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.