简体   繁体   English

如何在matplotlib.pyplot中绘制递归函数?

[英]How to plot recursive function in matplotlib.pyplot?

I'm trying to plot a recursive function I had made that measures growth over time. 我正在尝试绘制我用来测量随时间增长的递归函数。 Here is the function: 这是函数:

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))

Which gives me the output of: 这给了我输出:

19879.4425

How can I plot this function of the graph, where "years" is on the x-axis and "max_pop" is on the y-axis? 我如何绘制图形的此函数,其中“年”在x轴上,而“ max_pop”在y轴上?

Thanks for your help! 谢谢你的帮助!

Note: If it helps, I'm wanting/ expecting once plotted that the curve will look something similar to a learning curve. 注意:如果有帮助,我希望/期望一旦绘制,曲线将看起来类似于学习曲线。

You could just add a list at the top: 您可以在顶部添加一个列表:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM