[关闭]
@355073677 2016-03-26T22:17:58.000000Z 字数 5451 阅读 1358

Instruction of ode.py


Name: Feng Chen
Student number: 2013301020145
March 24, 2016

Abstract

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.

Numerical Algorithms Introduction

For initial value problem of first-order ordinary differential equation:

where a and b are real constant.
Therefore, when we make a Taylor expression of y at position x, we will get:
Changing the form of this formula, we can get:
This is the recursion equation of simple Euler method. If we try to use different recursion equation, which includes the higher order terms' contribution, we will get a more accurate result than simple Euler method.
Here are some useful recursion equations I used in this programme.

1. Simple Euler method

2. Modified Euler method

3. Second-order Runge-Kutta method

4. Third-order Runge-Kutta method

Usage and Examples

Firstly, we should set the function on the right side and the name of independent variable and dependent variable.

  1. ode(step,left limit,right limit,initial condition)
  2. set_fx(f_expression,independet_variable,dependent_variable)

For example:

  1. import ode
  2. A = ode.ode(0.05,0,1,10)
  3. ode.set_fx('y','x','y')

Then we can use different algorithm to solve the problem.

  1. ode.euler() #### simple euler method
  2. ode.eulerplus() #### modified euler method
  3. ode.rgkt_2() #### 2nd Runge-Kutta method
  4. ode.rgkt_3() #### 3rd Runge-Kutta method

Example:

  1. import ode as solving_ode
  2. import matplotlib.pyplot as plt
  3. A = solving_ode.ode(0.1,0,2,5) ##### setting the initial population N(0) = 10
  4. A.set_fx('N',['t','N'])
  5. eulerplus_record = A.euler()
  6. rgkt_3_record = A.rgkt_3()
  7. plt.figure(figsize = (10,6))
  8. plt.plot(eulerplus_record[0],eulerplus_record[1][0],'*',label = 'simple euler method')
  9. plt.plot(rgkt_3_record[0],rgkt_3_record[1][0],label = '3rd Runge-Kutta method')
  10. plt.legend()
  11. plt.savefig('chapter1_1.6.png',dpi = 144)
  12. plt.show()

Algorithms Testing

comparison
Solving equation: , setting the initial value: , step:
speed_test
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.

Solving differential equation systems or Higher order differential equation

Equation systems:

Giving a set of equation systems:


What we should do is to make Taylor expension on each independent variable , then we will get:
which means:
These are recursion equations for Simple Euler Method.
Similarly, we can use other algorithms to solve this problem by improving these recursion equations just like what I showed in Algorithms Introduction

Higher-order equation:

Giving a n-order differential equation:

Setting:
Then the differential equation will be transferred into a set of equations:
Therefore, we can use the same methods as equation systems to solve this problem.

Example: solve

Firstly, simpilify this equation into:

where

  1. import ode as solving_ode
  2. import matplotlib.pyplot as plt
  3. import math
  4. A = solving_ode.ode(0.005,3.8,4,(2,8))
  5. A.set_fx(('y1','-x*y+y1*math.e**x+3*math.sin(2*x)'),['x','y','y1'])
  6. rgkt_3_record = A.rgkt_3()
  7. B = solving_ode.ode(0.005,3.8,4,(2,8))
  8. B.set_fx(('y1','-x*y+y1*math.e**x+3*math.sin(2*x)'),['x','y','y1'])
  9. euler_record = B.euler()
  10. plt.figure(figsize = (10,6))
  11. plt.plot(euler_record[0],euler_record[1][0],label = 'simple euler method')
  12. plt.plot(rgkt_3_record[0],rgkt_3_record[1][0],label = '3rd Runge-Kutta method')
  13. plt.legend(loc = 'left upper')
  14. plt.savefig('example2.png',dpi = 144)
  15. plt.show()

Figure_example2

Package code

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