【发布时间】:2019-04-16 19:03:34
【问题描述】:
我需要计算和绘制一个函数,它是前两个导数。然后,我需要在图形上绘制原始函数的最小和最大点。我已经计算了这些,但不知道如何绘制数据。
最小/最大点的 x 值为
criticalPoints[]
y 值为
criticalPointsY[]
这是出现错误的代码段。
equation=CreateFunction();
firstDeriv=equation.diff(x);
secondDeriv=firstDeriv.diff(x);
print(equation);
criticalPoints=solveset(firstDeriv,x);
criticalPointsY=[];
for a in criticalPoints:
criticalPointsY.append(equation.subs(x,a));
p=plot(equation,firstDeriv,secondDeriv,(x,-10,10));
# Need to add the critical points to the graph. We have them, and the
# y values, but need to put them on the graphs.
print(criticalPoints)
print(criticalPointsY);
for a in range(0, len(criticalPoints)):
xval=criticalPoints[a];
yval=criticalPointsY[a];
plt.plot(xval, yval, 'ro')
p.show();
plt.show();
当我运行程序时,我得到了这个错误。 `
Traceback (most recent call last):
File "--------", line 58, in <module>
xval=criticalPoints[a];
TypeError: 'FiniteSet' object does not support indexing
我尝试在 p 上绘制点并得到不同的错误
p.plot(criticalPoints,criticalPointsY);
AttributeError: 'Plot' object has no attribute 'plot'
有没有办法在这个图表上绘制点? (p)
【问题讨论】:
-
“'FiniteSet' 对象不支持索引”与 plottin 无关。 set 没有顺序,因此询问它的第一个或第二个元素是没有意义的。使用
criticalPoints=list(solveset(firstDeriv,x))。另外,equation=CreateFunction();对那些不知道CreateFunction();是什么的人没有用处。 -
我已经针对列表进行了调整,但是这些点绘制在不同的图表上,因为我将它们绘制在“plt”上。有没有办法在情节“p”上绘制它们?我尝试时收到第二个错误。 Create function 从用户已经收集的输入中返回一个函数 y,以 x 的形式。我没有把它包括在房间里。
标签: python matplotlib sympy graphing