我在画一张三次函数的图。代码如下所示:
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,)
怎么了?怎么修复?
对不起,英语不好
您可以决定根本不使用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]
相关问题 更多 >
编程相关推荐