開始使用免費開始

隨時間的預測

在這個練習中,你會使用 NumPy 陣列來探索一筆投資在 20 年內如何成長。預測依照下列公式計算:

\( balance = deposit (1 + rate)^{years} \)

你需要計算並繪製 20 年期間每個月份的投資報酬百分比。這些時間點以 NumPy 陣列 years 表示,已經和 depositannual_rate(年報酬率)一起載入到你的工作環境。報酬百分比的計算如下:

\( gain = \dfrac{(balance-deposit)}{deposit}\times100\)

本練習屬於課程

給 MATLAB 使用者的 Python

檢視課程

練習說明

  • 針對變數 years 中的每個時間點,預測帳戶餘額。
  • 計算帳戶的報酬百分比。
  • 以年為自變數,繪製報酬百分比的折線圖。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Calculate the balance in the account for each time in the variable years
balance =

# Calculate the percent gain in the account
percent_gain =

# Plot the percent gain as a function of years
plt.plot(____,____)
plt.____('Years')
plt.____('Percent Gain')
plt.____()
編輯並執行程式碼