@355073677
2016-03-19T23:30:57.000000Z
字数 4094
阅读 1371
Name: Feng Chen
Student number: 2013301020145
March 19, 2016
This is a programme to solve a specific first order ODE system via Euler method and store the datas in a txt file. Finally, I plotthe datas and compare this result to the analytical result. Also, I write a programme to read the datas in the .txt files to plot.
This is a decay problem with two types of nuclei A ans B, which can decay into each other with the same time constant τ. The corresponding rate equations are:
Firstly, we do the Taylor expansion for the particle numbers N at time t :
Firstly, we combine two rate equations:
Setting the initial number of A is 100 and that of B is zero and the same time constant 1s.
As the figures showed, when we make the time step smaller, the numerical result will be much more accurate. Also, due to the same time constant, the final number of both particles will be equal.
In general case, I set the different time constant for particel A and particle B. Sstting the initial number of A is 1000 and that of B is zero.
In this case the ratio of final number of B and A is equal to their time constant ratio. (For the particel number will reach the constant term in their expressions as time grows up.)
Finally, I add particle B at the initial time as well.
In the previous programme, all the datas will be recorded in a .txt file and the format of datas are as follows:
As showed in the figure, the datas corresponding to the same time t are in the same row and each of them are seperated by a blank space.
Thus, we should transfer this data list into a 2-dimension list and each column is what we want.
The core codes are as follows:
def read_initial(initial_file):
itxt= open(initial_file)
initial_data=[]
data =[]
for lines in itxt.readlines():
lines=lines.replace("\n","").split(",")
initial_data.append(lines)
for i in range(len(initial_data[0][0].split(' '))):
data.append([])
for j in range(len(initial_data)):
data[i].append(initial_data[j][0].split(' ')[i])
itxt.close()
return data ### data[0] = x, data[1] = y1, data[2] = y2,...
After transferring the data into a 2D list, we can plot the number of particles versus time:
In this project, we can learn how to use Euler method to solve simple first order ordinary differential equation or equations. On the other hand, all the high order differential equation can be transferred into equation systems, which means Euler method is also a way to solve high order differential equation.