Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python中显示图形上函数的转折点?_Python_Derivative - Fatal编程技术网

如何在Python中显示图形上函数的转折点?

如何在Python中显示图形上函数的转折点?,python,derivative,Python,Derivative,要求定义此函数f(x)=x^3-15x^2-18x+1,然后以绘图区域显示所有转折点的方式进行绘图。 我认为我下面的计算是错误的,因为如果你看图表,至少有两个转折点。 我的意思是,函数先增大,然后减小,但在我的计算中,我找到了x的单一解,其中x=0.5 我怎样才能解决这个问题 %matplotlib notebook import matplotlib.pyplot as plt plt.rcParams.update({'font.size': 14}) #1. define the fu

要求定义此函数
f(x)=x^3-15x^2-18x+1
,然后以绘图区域显示所有转折点的方式进行绘图。

我认为我下面的计算是错误的,因为如果你看图表,至少有两个转折点。
我的意思是,函数先增大,然后减小,但在我的计算中,我找到了
x
的单一解,其中
x=0.5

我怎样才能解决这个问题

%matplotlib notebook
import matplotlib.pyplot as plt

plt.rcParams.update({'font.size': 14})

#1. define the function
def f(x): 
    return x**3 - 15*x**2 - 18*x + 1


#find the turning points of a polynomial of 3rd degree

#2. find the derivative of  x**3 - 15*x**2 - 18*x + 1 
# the derivative is -15(2x + 1)

#3. by the null factor law, get the value of x: 
# 2x + 1 = 0
# 2x = 1
# x = 0.5

#4. check what is f(x) for x = 0.5
0.5**3 - 15*0.5**2 - 18*0.5 + 1 = -39/4 = -9.75

#5. Therefore, we can conclude that there is a single turning point at A(0.5, -9.75)

x = np.linspace(-5,5,100)
plt.plot(x, f(x))
plt.plot(0.5,f(0.5),'.r', ms=20,label='x_1') #plot A(0.5, -9.75)
plt.xlabel('x')
plt.ylabel('f(x)')
plt.grid()
plt.show()

编辑_1

根据皮卡洛斯的建议,我重新计算了导数,但我的方程的解在图表上看起来并不正确

def f(x): 
    return x**3 - 15*x**2 - 18*x + 1


#find the turning points of a polynomial of 3rd degree

#2. find the derivative of  x**3 - 15*x**2 - 18*x + 1 
# Workout the derivative
# (x**3)' = 3*x**2
# (-15*x**2)' = -30*x
# (-18x)' = -18
# (1)' = 0 
# Therefore our derviative equation is 3*x**2 -30*x  -18
#Bring it to a more concise form
# 3(x**2 - 10*x -6) = 0

#Find the x solution for x**2 - 10*x -6 
#Delta = b**2 - 4*a*c
#x1 = (10 + sqr(76))/2  = 9.358
#x2 = (10 - sqr(76))/2  = -3.84


x1 = (10 + np.sqrt(76))/2
x2 = (10 - np.sqrt(76))/2
print(x1)
print(x2)

#4. check what is f(x) for:  x1 = 9.358, x2 = 0.641

x = np.linspace(-5,5,100)
plt.plot(x, f(x))
plt.plot(x1,f(x1),'.r', ms=20,label='x_1') 
plt.plot(x2,f(x2),'.b', ms=20,label='x_2') 
plt.xlabel('x')
plt.ylabel('f(x)')
plt.grid()
plt.show()

三次多项式的导数是二次多项式。特别是你的导数是3*x**2-30*x-18,所以我们可以说导数的解是
3(x**2-10x-6)
。如果我不知道如何计算
3(x**2-10x-6)
,我如何计算这个二次型,以便得到
x的值。我可以试着找到x1,2的增量和解<代码>D=b**2-4*a*c
。因此
D=100-24=76
。和
x1,x2=(-b+-sqr(D))/2*a
。现在,
x1=(10+sqr(76))/2
x2=(10-sqr(76))/2
。这是正确的吗?@Picarus我已经编辑了我的问题,并根据你的答案进行了实现。你能给个建议吗?数学似乎正确,但你的情节是错的。X2未在正确的位置绘制。同时增加你的邻域间隔,这样你可以看到整个相关的区域,比如-10到20。蓝点和红点必须位于绘图的顶部或底部,它们是局部最大值或最小值。我是从我的平板电脑上回答的,所以不能说得更具体。我必须解决这个问题,而不要使用内置的方法,如
np.gradient
。它更侧重于手工解决数学问题,并在python的帮助下表示它。我已经运行了您的代码,它在图形上绘制了2个以上的点
import numpy as np
from matplotlib import pyplot as plt

# generate some toy data
n = 600
t = np.linspace(0, 600, n)
y = (300 * np.exp(-0.1 * t) + 1) + 20 * (np.random.random(n))

# get the gradient
dy = np.gradient(y)

# search gradient for first occurrence of thresh value:
thresh = 0.01
idx_thresh = np.argmax(dy > thresh)
# y[idx_thresh] would be the "turning point"

# visualization
plt.plot(t, y, 'b', label='y')
plt.plot(t, dy, 'g', label='dy')
plt.plot(t[idx_thresh:], y[idx_thresh:], 'r', label=f'y[dy > {thresh}]')
plt.legend()