[关闭]
@Guozhongzhi 2016-10-09T11:57:21.000000Z 字数 2772 阅读 806

第四次作业

郭忠智 学号2014301020087

Problem1.5

一、摘要

本次作业应用Python编写程序,解决了一个微分方程组——两种原子核A和B吸收和释放核子的相互转变,计算出每个时刻两种原子核数量并将其结果通过画图展现出来,可以利用数值运算可以方便地解决微分方程。

二、背景

利用计算机解决物理问题,在今天的物理研究里至关重要,利用编程语言,可以将科研工作者集中精力于物理问题,避免花过多时间在解决数学问题上。第一章通过介绍了一种简单的解决微分方程的方法——级数展开及数值逼近法。这种方法简单易懂,但是需要注意精度问题,取不同的时间间隔,会产生不同大小的误差,在具体的实际问题中,需要细心考虑所取步长对结果的影响。

三、正文

原问题所给出的微分方程组为:



(1)用微积分解该方程组,得到:


代入、以及初值条件,解得:


所以


(2)利用数值逼近方法,写出的python程序代码如下:

  1. # -*- coding: utf-8 -*-
  2. """
  3. Spyder Editor
  4. This is a temporary script file.
  5. """
  6. # A decay problem with two types of nuclei A and B.
  7. # Simulation of the decay.
  8. # Program to problem_1.5 on the book Computational Physics.
  9. import pylab as pl
  10. class nuclei_decay:
  11. """
  12. A decay problem with two types of nuclei A and B.
  13. Simulation of the decay.
  14. Program to problem_1.5 on the book Computational Physics.
  15. """
  16. def __init__(self, number_of_A = 100, number_of_B = 0, time_constant = 1, time_of_duration = 5, time_step = 0.05):
  17. # unit of time is second
  18. self.N_A = [number_of_A]
  19. self.N_B = [number_of_B]
  20. self.t = [0]
  21. self.tau = time_constant
  22. self.dt = time_step
  23. self.time = time_of_duration
  24. self.nsteps = int(time_of_duration / time_step + 1)
  25. print("Initial number of nuclei A ->", number_of_A)
  26. print("Initial number of nuclei B ->", number_of_B)
  27. print("Time constant ->", time_constant)
  28. print("time step -> ", time_step)
  29. print("total time -> ", time_of_duration)
  30. def calculate(self):
  31. for i in range(self.nsteps):
  32. tmp_A = self.N_A[i] + (self.N_B[i] - self.N_A[i]) * self.dt / self.tau
  33. tmp_B = self.N_B[i] + (self.N_A[i] - self.N_B[i]) * self.dt / self.tau
  34. self.N_A.append(tmp_A)
  35. self.N_B.append(tmp_B)
  36. self.t.append(self.t[i] + self.dt)
  37. def show_results(self):
  38. plot1, = pl.plot(self.t, self.N_A, 'r')
  39. plot2, = pl.plot(self.t, self.N_B, 'g')
  40. pl.xlabel('time ($s$)')
  41. pl.ylabel('Number of Nuclei')
  42. pl.title("Number's change of nuclei A and nuclei B")
  43. pl.legend([plot1, plot2], ['nuclei A', 'nuclei B'], loc="best")
  44. pl.show()
  45. def store_results(self):
  46. myfile = open('nuclei_decay_data.txt', 'w')
  47. for i in range(len(self.t)):
  48. myfile.write(str(self.t[i]))
  49. myfile.write(str(self.N_A[i]))
  50. myfile.close()
  51. print myfile
  52. a = nuclei_decay()
  53. a.calculate()
  54. a.show_results()
  55. a.store_results()

(3)对比结果:改写show_results()函数,加入利用微积分所求得的结果的图形,改写的代码如下:

  1. def show_results(self):
  2. import math
  3. L = []
  4. for x in range(501):
  5. L.append(x * 0.01)
  6. x = L
  7. y = []
  8. z = []
  9. for i in range(501):
  10. a = 50 + 50 * math.e ** (-2 * x[i])
  11. b = 50 - 50 * math.e ** (-2 * x[i])
  12. y.append(a)
  13. z.append(b)
  14. plot_1, = pl.plot(x, y, 'r:')
  15. plot_2, = pl.plot(x, z, 'g:')
  16. plot1, = pl.plot(self.t, self.N_A, 'r')
  17. plot2, = pl.plot(self.t, self.N_B, 'g')
  18. pl.xlabel('time ($s$)')
  19. pl.ylabel('Number of Nuclei')
  20. pl.title("Number's change of nuclei A and nuclei B")
  21. pl.legend([plot1, plot2, plot_1, plot_2], ['nuclei A', 'nuclei B', 'nuclei A for calculus', 'nuclei B for calculus'], loc="best")
  22. pl.show()

四、结论

运行(2)中所写代码,并将(1)(2)中所得结果作图,得到如下结果:
1 5
1 5

可以看到,利用数值逼近的方法可以得到所求结果的较好近似

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