英文:
Graph parabola in matplotlib with vertex and 2 'end' points known
问题
尝试创建一个抛物线矢量,其中顶点和抛物线上的另外两个点已知。
示例...
- 范围从0到10
- 顶点 = [5,2]
- 坐标1 = [1,1]
- 坐标2 = [10,1]
任何帮助/建议都将不胜感激。
谢谢
英文:
Trying to create a parabolic vector of values where the Vertex and two other points along the parabola are known.
Example...
- Range from 0 to 10
- Vertex = [5,2]
- Coordinate 1 = [1,1]
- Coordinate 2= [10,1]
Any help/advice is greatly appreciated.
Thanks
答案1
得分: 1
import matplotlib.pyplot as plt
import numpy as np
# 点
x = [1, 5, 10]
y = [1, 2, 1]
# 使用 polyfit 来拟合通过这些点的二次多项式
poly_coeffs = np.polyfit(x, y, 2)
# 评估多项式找到的值
# 评估点的向量 xx
xmin = 0
xmax = 10
xx = np.linspace(xmin, xmax, 100)
yy = np.polyval(poly_coeffs, xx) # y 坐标
# 画图
plt.figure()
plt.plot(x, y, 'or')
plt.plot(xx, yy)
plt.grid()
plt.ylim([-3, 4])
plt.xlim([-0.5, 12])
这会绘制出下面的图像:
英文:
I would use numpy to adjust a parabola passing by the points with polyfit
, and then polyval
to evaluate the polynomial found:
import matplotlib.pyplot as plt
import numpy as np
#points
x = [1, 5, 10]
y = [1, 2, 1]
poly_coeffs = np.polyfit(x,y,2) #fit a polynomial with degree=2
#evaluation points vector xx
xmin = 0
xmax = 10
xx = np.linspace(xmin, xmax, 100)
yy = np.polyval(poly_coeffs, xx) #y coords
#ploting
plt.figure()
plt.plot(x,y,'or')
plt.plot(xx,yy)
plt.grid()
plt.ylim([-3,4])
plt.xlim([-0.5,12])
This would draw the next image:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论