这段代码有什么问题?(python Cubicfunction)

2024-04-27 15:21:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我在画一张三次函数的图。代码如下所示:

import matplotlib.pyplot as plt
from sympy import *
x=Symbol('x')
a,b,c,d=(5,6,7,8)
f = a*x**3 - b*x**2 + c*x - d
xpt=[i for i in (-100,100)]
ypt=[l for l in solve(f)]
plt.axis([-100, 100, -100, 100]) 
plt.plot(xpt,ypt) #畫線
plt.xlabel("x", fontsize=16) 
plt.ylabel("y", fontsize=16) 


plt.grid() #顯示格線
plt.show() #顯示繪製的圖形

得到

ValueError: x and y must have same first dimension, but have shapes (2,) and (3,)

怎么了?怎么修复?
对不起,英语不好


Tags: and函数代码infromimportformatplotlib
2条回答

您可以决定根本不使用symphy并放置f = lambda x: a*x**3 ...或放置ys = [f.subs(x, xval) for xval in xs]

我认为您根本不需要使用solve()函数。只要xs = range(-100, 100)然后ys = [f(x) for x in xs]

相关问题 更多 >