我目前正在使用以下 python 腳本在指定域上繪制給定的分段函式,np.linspace(1.0, 3.0)并遇到了一些繪圖錯誤。當我運行腳本時,我收到以下錯誤:ValueError: x and y must have same first dimension, but have shapes (50,) and (42,). 我曾嘗試調整 的長度domain以匹配codomainby的長度,domain = np.linspace(1.0, 3.0, 42)但沒有運氣。這是我在下面給出的代碼:
import numpy as np
from matplotlib import pyplot as plt
def f(x):
image = []
for p in x:
if p in np.linspace(1.0, 1.5):
y = 1.000004 * (p - 1.0) 0.486068 * (p - 1.0) ** 2 - 0.106566 * (p - 1.0) ** 3
image.append(y)
elif p in np.linspace(1.5, 2.0):
y = 0.608198 1.406152 * (p - 1.5) 0.326219 * (p - 1.5) ** 2 - 0.052277 * (p - 1.5) ** 3
image.append(y)
elif p in np.linspace(2.0, 2.5):
y = 1.386294 1.693164 * (p - 2.0) 0.247803 * (p - 2.0) ** 2 - 0.032798 * (p - 2.0) ** 3
image.append(y)
elif p in np.linspace(2.5, 3.0):
y = 2.290727 1.916372 * (p - 2.5) 0.198606 * (p - 2.5) ** 2 - 0.021819 * (p - 2.5) ** 3
image.append(y)
return image
domain = np.linspace(1.0, 3.0)
codomain = f(domain)
plt.plot(domain, codomain)
plt.show()
免責宣告:這是我在這個網站上的第一篇文章,所以如果有什么我需要調整以獲得更好的反饋,請告訴我。
uj5u.com熱心網友回復:
你似乎誤解了np.linspace正在做的事情。具體來說,np.linspace(1,1.5)不保證 in 的值在np.linspace(1,3)
我建議閱讀
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/442807.html
標籤:Python python-3.x 功能 数学
