@355073677
2016-03-26T14:17:58.000000Z
字数 5451
阅读 1545
Name: Feng Chen
Student number: 2013301020145
March 24, 2016
This is the instruction file for the first order ordinary differential equation algorithms package ode.py. There are totally four kinds of numerical algorithms in this package temporarily, including simple Euler method, modified Euler method, Second-order Runge-Kutta method, Third-order Runge-Kutta method.
On the other hand, this package can be also used to solve differential equation systems, which I will talk about in the latter part of insturction.
For initial value problem of first-order ordinary differential equation:
Firstly, we should set the function on the right side and the name of independent variable and dependent variable.
ode(step,left limit,right limit,initial condition)set_fx(f_expression,independet_variable,dependent_variable)
For example:
import odeA = ode.ode(0.05,0,1,10)ode.set_fx('y','x','y')
Then we can use different algorithm to solve the problem.
ode.euler() #### simple euler methodode.eulerplus() #### modified euler methodode.rgkt_2() #### 2nd Runge-Kutta methodode.rgkt_3() #### 3rd Runge-Kutta method
Example:
import ode as solving_odeimport matplotlib.pyplot as pltA = solving_ode.ode(0.1,0,2,5) ##### setting the initial population N(0) = 10A.set_fx('N',['t','N'])eulerplus_record = A.euler()rgkt_3_record = A.rgkt_3()plt.figure(figsize = (10,6))plt.plot(eulerplus_record[0],eulerplus_record[1][0],'*',label = 'simple euler method')plt.plot(rgkt_3_record[0],rgkt_3_record[1][0],label = '3rd Runge-Kutta method')plt.legend()plt.savefig('chapter1_1.6.png',dpi = 144)plt.show()
Solving equation: , setting the initial value: , step:
The bigger, the faster
As the figures showed above, though Simple Euler method is the fastest algorithm, it makes more mistakes than other three kinds of algorithms.
It shows that Modified Euler method is a very good approach to solve ordinary differential equation. You can choose an appropriate method to solve your own problem.
Giving a set of equation systems:
Giving a n-order differential equation:
Firstly, simpilify this equation into:
import ode as solving_odeimport matplotlib.pyplot as pltimport mathA = solving_ode.ode(0.005,3.8,4,(2,8))A.set_fx(('y1','-x*y+y1*math.e**x+3*math.sin(2*x)'),['x','y','y1'])rgkt_3_record = A.rgkt_3()B = solving_ode.ode(0.005,3.8,4,(2,8))B.set_fx(('y1','-x*y+y1*math.e**x+3*math.sin(2*x)'),['x','y','y1'])euler_record = B.euler()plt.figure(figsize = (10,6))plt.plot(euler_record[0],euler_record[1][0],label = 'simple euler method')plt.plot(rgkt_3_record[0],rgkt_3_record[1][0],label = '3rd Runge-Kutta method')plt.legend(loc = 'left upper')plt.savefig('example2.png',dpi = 144)plt.show()
