亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 numpy 以不同的 theta 值返回 r

使用 numpy 以不同的 theta 值返回 r

皈依舞 2022-01-18 13:49:08
我需要根據 r 在不同的 theta 值處等于什么來生成一個表。我可以很容易地用 matplotlib 繪制和顯示方程,并希望有一種簡單的方法:給 numpy theta 變量、我的曲線方程和 viola,返回 r 值我試圖查看 numpy 的文檔,但很難找到我需要的東西。import matplotlib.pyplot as pltimport matplotlib as mplimport numpy as npmpl.style.use('default')# Number of Points Ploted# Change this numer to affect accuracy of the graphn = 3000theta = np.linspace(0, 2.0*np.pi, n)def show_grid():  plt.grid(True)  plt.legend()  plt.show()# Setting the range of theta to [0, 2π] for this specific equationtheta4 = np.linspace(0, 2*np.pi, n)# Writing the equationcurve4 = 5*np.cos(64*theta)ax1 = plt.subplot(111, polar=True)ax1.plot(theta4, curve4, color='xkcd:cyan', label='CURVE 4: r = 5cos(64θ), [0, 2π)')ax1.set_ylim(0,5)ax1.set_yticks(np.linspace(0,5,6))show_grid()上面的代碼很好地生成了一個圖表,但是:我可以使用相同的變量在 theta 處返回 r 嗎?
查看完整描述

1 回答

?
HUWWW

TA貢獻1874條經驗 獲得超12個贊

通常不能保證 theta 值數組實際上包含您要查詢的值。作為一個例子考慮


theta = np.array([1,2,3,4])

r = np.array([8,7,6,5])

現在您想知道 r at 的值theta0 = 2.5,但由于該值不是它的一部分,theta因此在 中沒有對應的值r。


因此,您可能決定在 之后的 theta 處找到 r 的值theta0,在這種情況下,3 是 2.5 之后 theta 中的下一個值,因此您可能正在尋找 r == 6,


theta0 = 2.5

print(r[np.searchsorted(theta, theta0)])   # prints 6

或者您可能想要在 theta 上插入 r 值,在這種情況下,2.5 介于 2 和 3 之間,因此您正在尋找 6.5,它介于 7 和 6 之間,


theta0 = 2.5

print(np.interp(theta0, theta, r))    # prints 6.5

或者更一般地說,你有一個實際的函數,它定義了r(theta). 這里,


theta = np.array([1,2,3,4])

rf = lambda x: -x + 9


r = rf(theta)

print(r)                              # prints [8,7,6,5]


print(rf(theta0))                     # prints 6.5

您的示例的最后一個案例看起來像


theta = np.linspace(0, 2*np.pi, 3001)


# Writing the equation

r = lambda theta: 5*np.cos(64*theta)


ax1 = plt.subplot(111, polar=True)

ax1.plot(theta, r(theta), label='CURVE 4: r = 5cos(64θ), [0, 2π)')


print(r(np.pi/2))  # prints 5


plt.show()


查看完整回答
反對 回復 2022-01-18
  • 1 回答
  • 0 關注
  • 192 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號