[关闭]
@rfhongyi 2016-11-09T03:37:20.000000Z 字数 5474 阅读 601

Exercise_04 :

Consider a decay problem with two types of nuclei

冉峰 弘毅班 2014301020064


Abstract


Background


The Main Body

Code 1

  1. import pylab as pl
  2. class uranium_decay:
  3. def __init__(self, number_of_nuclei_A = 100,\
  4. number_of_nuclei_B = 0,time_constant_1 = 1,\
  5. time_constant_2 = 1, time_of_duration = 8,\
  6. time_step = 0.05):
  7. # unit of time is second
  8. self.n1_uranium = [number_of_nuclei_A ]
  9. self.n2_uranium = [number_of_nuclei_B ]
  10. self.t = [0]
  11. self.tau1 = time_constant_1
  12. self.tau2 = time_constant_2
  13. self.dt = time_step
  14. self.time = time_of_duration
  15. self.nsteps = int(time_of_duration // time_step + 1)
  16. print("Initial number of nuclei A ->", number_of_nuclei_A)
  17. print("Initial number of nuclei B ->", number_of_nuclei_B)
  18. print("Time constant A ->", time_constant_1)
  19. print("Time constant B ->", time_constant_2)
  20. print("time step -> ", time_step)
  21. print("total time -> ", time_of_duration)
  22. def calculate(self):
  23. for i in range(self.nsteps):
  24. tmp1 = self.n1_uranium[i] - self.n1_uranium[i] /self.\
  25. tau1 * self.dt+self.n2_uranium[i] / self.tau2 * self.dt
  26. tmp2 = self.n2_uranium[i] - self.n2_uranium[i] /self.\
  27. tau2 *self.dt+self.n1_uranium[i] / self.tau1 * self.dt
  28. self.n1_uranium.append(tmp1)
  29. self.n2_uranium.append(tmp2)
  30. self.t.append(self.t[i] + self.dt)
  31. def show_results(self):
  32. pl.plot(self.t, self.n1_uranium)
  33. pl.plot(self.t, self.n2_uranium)
  34. pl.xlabel('time ($s$)')
  35. pl.ylabel('Number of Nuclei ')
  36. pl.legend(['$N_A$','$N_B$'])
  37. pl.show()
  38. def store_results(self):
  39. myfile = open('nuclei_decay_data.txt', 'w')
  40. for i in range(len(self.t)):
  41. print(self.t[i], self.n1_uranium[i], file = myfile)
  42. print(self.t[i], self.n2_uranium[i], file = myfile)
  43. myfile.close()
  44. a = uranium_decay()
  45. a.calculate()
  46. a.show_results()
  47. a.store_results()

Result

figure4_2.png

figure4_3.png

figure4_4.png

figure4_5.png
figure4_6.png

Code 2

We can see the results in one figure.

  1. import pylab as pl
  2. class uranium_decay:
  3. def __init__(self, number_of_nuclei_A = 100,\
  4. number_of_nuclei_B = 0,time_constant_1 = 2,\
  5. time_constant_2 = 1, time_of_duration = 8,\
  6. time_step = 0.05,time_step1 = 0.10,number_of_nuclei_A1 = 100,number_of_nuclei_B1 = 0):
  7. # unit of time is second
  8. self.n1_uranium = [number_of_nuclei_A ]
  9. self.n2_uranium = [number_of_nuclei_B ]
  10. self.n11_uranium = [number_of_nuclei_A1 ]
  11. self.n21_uranium = [number_of_nuclei_B1 ]
  12. self.t = [0]
  13. self.tau1 = time_constant_1
  14. self.tau2 = time_constant_2
  15. self.dt = time_step
  16. self.dt1 = time_step1
  17. self.time = time_of_duration
  18. self.nsteps = int(time_of_duration // time_step + 1)
  19. self.nsteps1 = int(time_of_duration // time_step1 + 1)
  20. print("Initial number of nuclei A ->", number_of_nuclei_A)
  21. print("Initial number of nuclei B ->", number_of_nuclei_B)
  22. print("Initial number of nuclei A ->", number_of_nuclei_A1)
  23. print("Initial number of nuclei B ->", number_of_nuclei_B1)
  24. print("Time constant A ->", time_constant_1)
  25. print("Time constant B ->", time_constant_2)
  26. print("time step -> ", time_step)
  27. print("time step -> ", time_step1)
  28. print("total time -> ", time_of_duration)
  29. def calculate(self):
  30. for i in range(self.nsteps):
  31. tmp1 = self.n1_uranium[i] - self.n1_uranium[i] /self.\
  32. tau1 * self.dt+self.n2_uranium[i] / self.tau2 * self.dt
  33. tmp2 = self.n2_uranium[i] - self.n2_uranium[i] /self.\
  34. tau2 *self.dt+self.n1_uranium[i] / self.tau1 * self.dt
  35. tmp11 = self.n11_uranium[i] - self.n11_uranium[i] /\
  36. self.tau1 * self.dt1+self.n21_uranium[i] /\
  37. self.tau2 * self.dt1
  38. tmp21 = self.n21_uranium[i] - self.n21_uranium[i] /\
  39. self.tau2 *self.dt1+self.n11_uranium[i] /\
  40. self.tau1 * self.dt1
  41. self.n1_uranium.append(tmp1)
  42. self.n2_uranium.append(tmp2)
  43. self.n11_uranium.append(tmp11)
  44. self.n21_uranium.append(tmp21)
  45. self.t.append(self.t[i] + self.dt)
  46. def show_results(self):
  47. pl.plot(self.t, self.n1_uranium)
  48. pl.plot(self.t, self.n2_uranium)
  49. pl.plot(self.t, self.n11_uranium)
  50. pl.plot(self.t, self.n21_uranium)
  51. pl.xlabel('time ($s$)')
  52. pl.ylabel('Number of Nuclei ')
  53. pl.legend(['$N_A$','$N_B$'])
  54. pl.show()
  55. def store_results(self):
  56. myfile = open('nuclei_decay_data.txt', 'w')
  57. for i in range(len(self.t)):
  58. print(self.t[i], self.n1_uranium[i], file = myfile)
  59. print(self.t[i], self.n2_uranium[i], file = myfile)
  60. print(self.t[i], self.n11_uranium[i], file = myfile)
  61. print(self.t[i], self.n21_uranium[i], file = myfile)
  62. myfile.close()
  63. a = uranium_decay()
  64. a.calculate()
  65. a.show_results()
  66. a.store_results()

Result

figure4_9.png
figure4_10.png

figure4_11.png


From the figure we can see that the system reaches a steady state in which and are constant.In such a steady state, the time derivatives and wanish.


Conclusion


Thanks For

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