Qiita Teams that are logged in
You are not logged in to any team

Community
Service
Qiita JobsQiita ZineQiita Blog
12
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

@tobira-code

# はじめに

Pythonを使って常微分方程式を解く方法を説明します。

# OS / Python module version

Python module versionは次の通りです。

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

y'' = -y \\

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

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))

console
> python .\test.py

Why not register and get more from Qiita?
1. We will deliver articles that match you
By following users and tags, you can catch up information on technical fields that you are interested in as a whole
2. you can read useful information later efficiently
By "stocking" the articles you like, you can search right away
12
Help us understand the problem. What are the problem?