随时间进行预测
在本练习中,您将使用 NumPy 数组来探索一项投资在 20 年内如何增长。预测使用以下公式计算:
\( balance = deposit (1 + rate)^{years} \)
您需要计算并绘制 20 年内每个月的投资百分比增幅。时间以 NumPy 数组 years 表示,已与 deposit 和 annual_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.____()