2 回答

TA貢獻1812條經驗 獲得超5個贊
由于這些y值取決于a您需要a為y查找指定的值??紤]以下:
def costfunction(b, a):
# b Value
x = np.linspace(b*(-b), b*(b), 100)
y = (x - a)**2
return x, y
a = 5
c = costfunction(20, a)
plt.plot(c[0], c[1], linestyle='-', linewidth=1)
plt.ylabel("Cost Value");
b = 100
yb = (b - a)**2 # Find the corresponding y-value
plt.plot(b, yb, marker='o', color='b')
plt.show()
這會給你
您可能還注意到我修改了costfunction
定義以返回x
值,否則 matplotlib 將只使用它喜歡的任何值。

TA貢獻1789條經驗 獲得超8個贊
def costfuntion(b, a):
# b Value
x = np.linspace(b*(-b), b*(b), 100)
y = (x - a)**2
return x, y
x, y = costfuntion(20,5)
plt.plot(x, y)
for i in range(0, len(x), 2):
plt.plot(x[i], y[i], marker='o', color='b')
更改成本函數以返回您正在繪制的函數的x和y,并使用此信息在函數上繪制點。
添加回答
舉報