我试图绘制一个递归函数,该函数可测量随时间的增长。这是函数:
def pop(start_pop, years, percentage_change, max_pop):
if years == 0:
return start_pop
else:
changes = pop(start_pop,years-1,percentage_change,max_pop)
return changes + (1-changes/max_pop)*percentage_change*changes
print(pop(600,85,0.1,20000))
哪个给我输出:
19879.4425
如何绘制图形的此函数,其中“年”在x轴上,而“ max_pop”在y轴上?
感谢您的帮助!
注意:如果有帮助,我希望/期望一旦绘制该曲线将看起来类似于学习曲线。
答案 0 :(得分:1)
您可以在顶部添加一个列表:
import matplotlib as mpl
import matplotlib.pyplot as plt
changes_plot=[]
def pop(start_pop, years, percentage_change, max_pop):
if years == 0:
return start_pop
else:
changes = pop(start_pop,years-1,percentage_change,max_pop)
changes_plot.append(changes)
return changes + (1-changes/max_pop)*percentage_change*changes
pop(600,85,0.1,20000)
plt.plot(changes_plot)
plt.show()