Solve ordinary differential equations in Python

Introduction

I will explain how to solve ordinary differential equations using Python.

OS / Python module version The OS used is Windows 10. The Python module version is:

console


> python --version
Python 3.4.3
> pip freeze
matplotlib==1.4.3
numpy==1.13.1+mkl
pandas==0.16.2
pyparsing==2.0.3
python-dateutil==2.4.2
pytz==2015.4
scipy==0.19.1
six==1.9.0

sample code Solve the following differential equation.

y'' = -y \\

Introduce the variable z. By increasing the variables, one second-order differential equation is considered as a simultaneous equation of two first-order differential equations.

y' = z \\
z' = -y \\

The initial conditions are $ t = 0, y_0 = 1, z_0 = 0 $. The solution is $ cos (t) $. Since the correct solution can be easily obtained, the error can be evaluated.

The following is a Python program that solves differential equations. Solve using scipy.integrate.ode.

test.py


import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import ode

def f(t, v): # t, y:v[0], y'=z:v[1]
    return [v[1], -v[0]] # y':return[0] y''=z':return[1]
v0 = [1.0, 0.0] # y0, y'0=z0

solver = ode(f)
solver.set_integrator(name="dop853")
solver.set_initial_value(v0)
tw = 10.0*2.0*np.pi
dt = tw / 1000;
t = 0.0
ts = []
ys = []
while solver.t < tw:
    solver.integrate(solver.t+dt)
    ts += [solver.t]
    ys += [solver.y[0]]
plt.figure(0)
plt.plot(ts, ys)
plt.figure(1)
plt.plot(ts, np.cos(ts)-ys)
plt.show()
print(np.max(np.cos(ts)-ys))

I will do it.

console


> python .\test.py

When executed, the graph will be displayed.

The following graph is the solution of the differential equation. The graph of cos (t) is displayed for 10 cycles. figure_0.png

The following graph plots the error. As t increases, so does the error. The error is on the scale of $ 10 ^ {-15} $. figure_1.png

Recommended Posts

Solve ordinary differential equations in Python
Solve simultaneous ordinary differential equations with Python and SymPy.
Solve ABC168D in Python
Solve ABC167-D in Python
Solve ABC146-C in Python
Solve ABC098-C in Python
Solve ABC159-D in Python
Solve ABC169 in Python
Solve ABC160-E in Python
Solving ordinary differential equations with Python ~ Universal gravitation
Solve simultaneous equations in an instant using Python
Solve ABC176 E in Python
Solve Wooldridge exercises in Python
Solve ABC175 D in Python
Solve optimization problems in Python
[Numerical calculation method, python] Solving ordinary differential equations by Eular method
Solve Atcoder ABC169 A-D in Python
Solve ABC036 A ~ C in Python
Solve ABC175 A, B, C in Python
ABC 157 D --Solve Friend Suggestions in Python!
I wanted to solve ABC159 in Python
Solve ABC165 A, B, D in Python
Solve the maximum subarray problem in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
[Scientific / technical calculation by Python] Solving ordinary differential equations, mathematical formulas, sympy
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python