在Python中的一行中使用Forward Euler方法

时间:2018-11-05 10:49:41

标签: python

对于我正在遵循的课程中使用python进行的一项实验,我需要使用正向Euler方法绘制一条线。我得到的一段代码缺少一行,即第20行。我似乎找不到一条行可以做到这一点,因为我在该站点上发现的所有解决方案以及其他解决方案都涉及多行代码。如果有人可以给我一些指示,那将不胜感激!

import matplotlib.pyplot as plt
import numpy as np

def main():
   # Define parameters
   growth_rate = 3

   # Define step size, initial condition and create time vector
   dt = 0.001
   time = np.arange(0, 1, dt)
   init = 1.0

   # Start with the initial condition and loop through time
   current_pop = init
   population_euler = []

   for time_step in time:
          population_euler.append(current_pop)
          # This is the line i need to write


   # analytical solution
   population_analitical = init * np.exp(growth_rate * time)

   #plot the results
   fig = plt.figure()
   ax = fig.gca()
   ax.plot(time, population_analitical, label="Analytical solution")
   ax.plot(time, population_euler, label="Euler integration")
   ax.legend()
   ax.grid(True)
   plt.show()


if __name__ == "__main__":
   main()

0 个答案:

没有答案