[关闭]
@Zemel-Yang 2016-10-04T10:29:05.000000Z 字数 3295 阅读 906

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

homework python


Abstract


Content

  1. import pylab as pl
  2. class uranium_decay:
  3. def __init__(self,number_of_a = 100, number_of_b = 0,time_constant = 1, time_of_duration = 5, time_step = 0.05):
  4. # unit of time is second
  5. self.nuclei_a = [number_of_a]
  6. self.nuclei_b = [number_of_b]
  7. self.t = [0]
  8. self.tau = time_constant
  9. self.dt = time_step
  10. print("Initial number of nuclei ->", number_of_a)
  11. print("Time constant ->", time_constant)
  12. print("time step -> ", time_step)
  13. print("total time -> ", time_of_duration)
  14. def calculate(self):
  15. for i in range(100):
  16. tmpa = self.nuclei_a[i] + (self.nuclei_b[i] - self.nuclei_a[i]) / self.tau * self.dt
  17. self.nuclei_a.append(tmpa)
  18. tmpb = self.nuclei_b[i] + (self.nuclei_a[i] - self.nuclei_b[i]) / self.tau * self.dt
  19. self.nuclei_b.append(tmpb)
  20. self.t.append(self.t[i] + self.dt)
  21. def store(nuclei_a,nuclei_b,t):
  22. data = open('data.txt','w')
  23. for i in range(100):
  24. data.write(str(t[i]))
  25. data.write(' ')
  26. data.write(str(nuclei_a[i]))
  27. data.write(' ')
  28. data.write(str(nuclei_b[i]))
  29. data.write('\n')
  30. data.close
  31. def show_results(self):
  32. plot1, = pl.plot(self.t,self.nuclei_a,'g')
  33. plot2, = pl.plot(self.t,self.nuclei_b,'r')
  34. pl.xlabel('Time(s)')
  35. pl.ylabel('Number of Nuclei')
  36. pl.legend([plot1, plot2], ['NA', 'NB'], loc="best")
  37. pl.xlim(0, 5)
  38. pl.ylim(0, 100)
  39. pl.savefig('chapter1_1.5.png',dpi=144)
  40. pl.show()
  41. b = uranium_decay()
  42. b.calculate()
  43. b.show_results()
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注