[关闭]
@zy-0815 2016-10-09T13:49:42.000000Z 字数 1967 阅读 956

计算物理课后作业Problem1.5


摘要

经过一段时间的学习,我们已经掌握了一定的语法和语言,而此次作业就是用Euler法解决常微分方程。

背景

学习计算物理的原因就是学习用计算机解决物理实际问题,而这次作业便是让我们开始解决实际问题,加以应用。

正文

由题可得,方程组为:

      

可解得:
      

由第一章PPT可知,Euler method为:

于是上两式即为:
    

因此学习第一章PPT便可写出代码为:

  1. import pylab as pl
  2. class uranium_decay:
  3. def __init__(self, number_of_nuclei_A = 100, number_of_nuclei_B = 0, time_constant = 1, time_of_duration = 5, time_step = 0.05):
  4. # unit of time is second
  5. self.n_uranium_A = [number_of_nuclei_A]
  6. self.n_uranium_B = [number_of_nuclei_B]
  7. self.t = [0]
  8. self.tau = time_constant
  9. self.dt = time_step
  10. self.time = time_of_duration
  11. self.nsteps = int(time_of_duration // time_step + 1)
  12. print("Initial number of nuclei A ->", number_of_nuclei_A)
  13. print("Initial number of nuclei B ->", number_of_nuclei_B)
  14. print("Time constant ->", time_constant)
  15. print("time step -> ", time_step)
  16. print("total time -> ", time_of_duration)
  17. def calculate(self):
  18. for i in range(self.nsteps):
  19. tmp1 = self.n_uranium_A[i] + (self.n_uranium_B[i] - self.n_uranium_A[i]) / self.tau * self.dt
  20. tmp2 = self.n_uranium_B[i] + (self.n_uranium_A[i] - self.n_uranium_B[i]) / self.tau * self.dt
  21. self.n_uranium_A.append(tmp1)
  22. self.n_uranium_B.append(tmp2)
  23. self.t.append(self.t[i] + self.dt)
  24. def show_results(self):
  25. plot1, = pl.plot(self.t, self.n_uranium_A,'b')
  26. plot2, = pl.plot(self.t, self.n_uranium_B,'g')
  27. pl.title('Nuclei Decay')
  28. pl.xlabel('time ($s$)')
  29. pl.ylabel('Number of Nuclei')
  30. first_legend=pl.legend([plot1, plot2], ['Particle A', 'Particle B'], loc="best")
  31. pl.show()
  32. a = uranium_decay()
  33. a.calculate()
  34. a.show_results()

结论

(1)运行上面程序可以得出如下结果:
image_1aukqdfkeftsdn113fg1etc1135m.png-27.9kB
(2)虽然写出了程序,也得出了结果,但是不知道为什么PPT中有些语法的含义,删去之后便可以运行,如a.store_results()。感觉还要自己多查多练。

致谢

感谢宗玥同学提供的作业部落VIP账号,同时告诉我一些LaTeX公式编辑的语法。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注