[关闭]
@zy-0815 2016-10-08T21:41:19.000000Z 字数 3074 阅读 1055

1.Problem

1.5 on Textbook page 16.

2.Abstract

In this passage,we are going to talk about a numerical problem called the decay betweeen A and B.With the preceeding knowledge of matplotlib and the Euler method to calcualte the numerical problem.

3.Background Introduction

As is known to all that the great mathematician Euler had developed a simple but useful way to calculate the numerical problem by using the Taylor expansion.

Euler method:


Here,in the discussion of the double decay problem,we again use this method to solve this problme out.Simple and beautiful.We can simulate different situations by inputting different intial conditions,which is very convenient for us to observe subtle differences.Additionally,we are going to discuss the precesion of the plot in details.

4.Main

  1. import pylab as pl
  2. import time
  3. class calcualtion_of_decay(object):
  4. def __init__(self,time_constant=1,duration=10,number_of_nuclei_A=100,number_of_nuclei_B=0,time_step=0.002):
  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.steps=int(duration//time_step+1)
  10. self.dt=time_step
  11. self.time=duration
  12. print('Initial number of nuclei A is',number_of_nuclei_A)
  13. print('Initial number of nuclei B is',number_of_nuclei_B)
  14. print('Duration is(second)',duration)
  15. print('Time steps is',time_step)
  16. print('Time constant is',time_constant)
  17. def calculate(self):
  18. for i in range(self.steps):
  19. tmp_A=self.n_uranium_A[i]+(self.n_uranium_B[i]/self.tau-self.n_uranium_A[i]/self.tau)*self.dt
  20. tmp_B=self.n_uranium_B[i]+(self.n_uranium_A[i]/self.tau-self.n_uranium_B[i]/self.tau)*self.dt
  21. self.n_uranium_A.append(tmp_A)
  22. self.n_uranium_B.append(tmp_B)
  23. self.t.append(self.t[i]+self.dt)
  24. def show(self):
  25. pl.plot(self.t,self.n_uranium_A,'r',label='Nuclei A')
  26. pl.plot(self.t,self.n_uranium_B,'g',label='Nuclei B')
  27. pl.xlabel('time s')
  28. pl.ylabel('Number of nuclei')
  29. pl.xlim(0.0,4)
  30. pl.title('The decay of Nuclei A and B,steps is %s'%self.dt')
  31. pl.show()
  32. pl.legend()
  33. def store_results(self):
  34. myfile = open('nuclei_decay_data.txt', 'w')
  35. for i in range(len(self.t)):
  36. print(self.t[i], self.n_uranium[i], file = myfile)
  37. myfile.close()
  38. print('This program is going to show you the decay of Nuclei A and B')
  39. x1=input('Please input the time constant' '\n')
  40. x2=input('Please input the time duration' '\n')
  41. x3=input('Please input the intial number of Nuclei A' '\n')
  42. x4=input('Please input the intial number of Nuclei B' '\n')
  43. x5=input('Please input the steps(the smaller you input,the more precise plot you get!)' '\n')
  44. a=calcualtion_of_decay(x1,x2,x3,x4,x5)
  45. a.calculate()
  46. a.show()

5.Result

Situation 1:

Parameter:
Number fo Nuclei A is 100
Number of Nuclei B is 0
Time constant is 1s
Steps is 0.1
BB635308-8968-417A-9C18-C8382E16A62E.png-84.6kB

Situation 2:

Parameter:
Number fo Nuclei A is 100
Number of Nuclei B is 0
Time constant is 1s
Steps is 0.05
F756E7F3-A7D5-4BFD-8BDC-9B2289AFC119.png-82.8kB

Situation 3:

Comparison between steps=0.1 and steps=0.5
865DBFDF-DDFC-451B-8445-4119ACCBDC2A.png-83.1kB

Situation 4:

Comparison between steps=0.1 and steps=0.001
171D4A64-907A-4BFB-9419-4A734858F979.png-88.3kB

The more detailed picture:
4A7C904C-5B7C-40CC-91C1-99D2BDECC15C.png-95.5kB

6.Discussion

In part 5 we see,that the smaller the steps is,the lower the plot is going to be than the bigger one.Owing to the o() term is omitted,and the square-term is always positive,so,as the steps is becoming smaller,the positive term is become lesser.That's the reason why there will be a derivation in the picture with different precesion.

7.Acknowldegment

1.Tutorial_Chapter 1 A First Numerical Problem
2.Matplotlib.com

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