[关闭]
@computationalphysics-2014301020090 2016-10-08T13:31:05.000000Z 字数 6152 阅读 176

The 4th homework

Chapter 1 problem 1.5: The decay of two kinds of particles


Abstract

● The use of Python mapping function to study the equilibrium state of radioactive particles.
● Solve problem about two kinds of particles decay.
● The equilibrium state under different initial values is considered in the decay model.


background

To solve first-order differential problems,there is the Euler method which is based on Taylor expansion:


where is the value of our function at .If we take to be small,then it is usually a good approximation to simply ignore the terms that involve second and higher powers of ,leaving us with:

For problem 1.5,it dives us two relevant and which are characterised by two first-order differential equations.We can raplace with former equation,so,

It's the same with


Content

● Problem 1.5

Consider again a decay problem with two types of nuclei A and B, but now suppose that nuclei of type A decay into ones of type B, while nucleiof type B decay into ones of type A. Strictly speaking, this is not a "decay" process, since it is possible for the type B nuclei to turn back into type A nuclei. A better analogy would be a reasonance in which a system can tunnel or move back and forth between two states A and B which have equal energies. The corresponding rate equations are


where for simplicity we have assumed that the two types of decay are characterized by the same time constant, . Solve this system of equations for the numbers of nuclei,and,as functions of time .Consider different initial conditions, such as,etc., and take. Show that your numerical results are consistent with the idea that the system reaches a steady state whichare constant. In such a steady state, the time derivatives should vanish.

● solution

Taylor expansion for


Similarly,

● Program

take = 0.05 and the duration of time is 5s.
Codes are listed:

  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Fri Oct 07 2016
  4. @author: 胡胜勇
  5. """
  6. import pylab as pl
  7. class uranium_decay:
  8. def __init__(self,number_of_a = 100, number_of_b = 0,time_constant = 1, time_of_duration = 5, time_step = 0.05):
  9. # unit of time is second
  10. self.nuclei_a = [number_of_a]
  11. self.nuclei_b = [number_of_b]
  12. self.t = [0]
  13. self.tau = time_constant
  14. self.dt = time_step
  15. print("Initial number of nuclei ->", number_of_a)
  16. print("Time constant ->", time_constant)
  17. print("time step -> ", time_step)
  18. print("total time -> ", time_of_duration)
  19. def calculate(self):
  20. for i in range(100):
  21. tmpa = self.nuclei_a[i] + (self.nuclei_b[i] - self.nuclei_a[i]) / self.tau * self.dt
  22. self.nuclei_a.append(tmpa)
  23. tmpb = self.nuclei_b[i] + (self.nuclei_a[i] - self.nuclei_b[i]) / self.tau * self.dt
  24. self.nuclei_b.append(tmpb)
  25. self.t.append(self.t[i] + self.dt)
  26. def store(nuclei_a,nuclei_b,t):
  27. data = open('data.txt','w')
  28. for i in range(100):
  29. data.write(str(t[i]))
  30. data.write(' ')
  31. data.write(str(nuclei_a[i]))
  32. data.write(' ')
  33. data.write(str(nuclei_b[i]))
  34. data.write('\n')
  35. data.close
  36. def show_results(self):
  37. plot1, = pl.plot(self.t,self.nuclei_a,'g')
  38. plot2, = pl.plot(self.t,self.nuclei_b,'r')
  39. pl.xlabel('Time(s)')
  40. pl.ylabel('Number of Nuclei')
  41. pl.legend([plot1, plot2], ['NA', 'NB'], loc="best")
  42. pl.xlim(0, 5)
  43. pl.ylim(0, 100)
  44. pl.savefig('chapter1_1.5.png',dpi=144)
  45. pl.show()
  46. b = uranium_decay()
  47. b.calculate()
  48. b.show_results()

● result

figure: Problem 1.5.png
From the figure we can see finally and the system reaches a steady state which and are constant.

now we change the initial conditions:
NA=50, NB=0
NA=50, NB=20
NA=80, NB=40
It's obvious that no matter how many about the initial conditions, the more one tend to decrease and the less one tend to increase and they will both reach the equilium number after a long time.And the equilium number is the average value of the initial number of .What's more,the more steps we do, the more precise the results are. Because the high order terms we dropped are getting smaller as the time step go smaller.


● Conclusion

Euler mothed is powerful to solve simple first order ordinary differential equations.draw figures by Python is a little difficult as well as interesting.It is essential to master the basic Python syntax,for me,there is still a long way to go.


addition

In order to celebrate the national day, we use Python to draw a five star red flag.
Codes are as follows:

  1. #coding=utf-8
  2. '''
  3. Created on 2016-10-1
  4. @author: 胡胜勇
  5. '''
  6. import turtle
  7. import math
  8. def draw_polygon(aTurtle, size=50, n=3):
  9. ''' 绘制正多边形
  10. args:
  11. aTurtle: turtle对象实例
  12. size: int类型,正多边形的边长
  13. n: int类型,是几边形
  14. '''
  15. for i in xrange(n):
  16. aTurtle.forward(size)
  17. aTurtle.left(360.0/n)
  18. def draw_n_angle(aTurtle, size=50, num=5, color=None):
  19. ''' 绘制正n角形,默认为黄色
  20. args:
  21. aTurtle: turtle对象实例
  22. size: int类型,正多角形的边长
  23. n: int类型,是几角形
  24. color: str, 图形颜色,默认不填色
  25. '''
  26. if color:
  27. aTurtle.begin_fill()
  28. aTurtle.fillcolor(color)
  29. for i in xrange(num):
  30. aTurtle.forward(size)
  31. aTurtle.left(360.0/num)
  32. aTurtle.forward(size)
  33. aTurtle.right(2*360.0/num)
  34. if color:
  35. aTurtle.end_fill()
  36. def draw_5_angle(aTurtle=None, start_pos=(0,0), end_pos=(0,10), radius=100, color=None):
  37. ''' 根据起始位置、结束位置和外接圆半径画五角星
  38. args:
  39. aTurtle: turtle对象实例
  40. start_pos: int的二元tuple,要画的五角星的外接圆圆心
  41. end_pos: int的二元tuple,圆心指向的位置坐标点
  42. radius: 五角星外接圆半径
  43. color: str, 图形颜色,默认不填色
  44. '''
  45. aTurtle = aTurtle or turtle.Turtle()
  46. size = radius * math.sin(math.pi/5)/math.sin(math.pi*2/5)
  47. aTurtle.left(math.degrees(math.atan2(end_pos[1]-start_pos[1], end_pos[0]-start_pos[0])))
  48. aTurtle.penup()
  49. aTurtle.goto(start_pos)
  50. aTurtle.fd(radius)
  51. aTurtle.pendown()
  52. aTurtle.right(math.degrees(math.pi*9/10))
  53. draw_n_angle(aTurtle, size, 5, color)
  54. def draw_5_star_flag(times=20.0):
  55. ''' 绘制五星红旗
  56. args:
  57. times: 五星红旗的规格为30*20, times为倍数,默认大小为10倍, 即300*200
  58. '''
  59. width, height = 30*times, 20*times
  60. # 初始化屏幕和海龟
  61. window = turtle.Screen()
  62. aTurtle = turtle.Turtle()
  63. aTurtle.hideturtle()
  64. aTurtle.speed(10)
  65. # 画红旗
  66. aTurtle.penup()
  67. aTurtle.goto(-width/2, height/2)
  68. aTurtle.pendown()
  69. aTurtle.begin_fill()
  70. aTurtle.fillcolor('red')
  71. aTurtle.fd(width)
  72. aTurtle.right(90)
  73. aTurtle.fd(height)
  74. aTurtle.right(90)
  75. aTurtle.fd(width)
  76. aTurtle.right(90)
  77. aTurtle.fd(height)
  78. aTurtle.right(90)
  79. aTurtle.end_fill()
  80. # 画大星星
  81. draw_5_angle(aTurtle, start_pos=(-10*times, 5*times), end_pos=(-10*times, 8*times), radius=3*times, color='yellow')
  82. # 画四个小星星
  83. stars_start_pos = [(-5, 8), (-3, 6), (-3, 3), (-5, 1)]
  84. for pos in stars_start_pos:
  85. draw_5_angle(aTurtle, start_pos=(pos[0]*times, pos[1]*times), end_pos=(-10*times, 5*times), radius=1*times, color='yellow')
  86. # 点击关闭窗口
  87. window.exitonclick()
  88. if __name__ == '__main__':
  89. draw_5_star_flag()

●result:###:用Python五星红旗.gif


Acknowledgments

Thanks to Yang Zongmeng,Luo Jiajia,Zhihu(知乎)

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