This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Lua で常微分方程式を解く / フェールベルク法 | |
Fehlberg = class() | |
function Fehlberg:init(funcs, t0, inits, h, tol) | |
self.funcs = funcs | |
self.t0 = t0 | |
self.inits = inits | |
self.h = h | |
self.numOfDiv = false | |
self.dim = #funcs | |
self.tol = tol or 0.001 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Lua による常微分方程式の数値解法 / 補外法 | |
Extrapo = class() | |
function Extrapo:init(funcs, t0, inits, h, numOfDiv) | |
local unpack = unpack or table.unpack | |
self.numOfDiv = numOfDiv or 1 | |
self.funcs = funcs | |
self.step = h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Lua による微分方程式の数値解法 / グラッグ法 | |
Gragg = class() | |
function Gragg:init(funcs, t0, inits, initsDot, h, numOfDiv) | |
local unpack = unpack or table.unpack | |
self.numOfDiv = numOfDiv or 1 | |
self.step = h | |
self.funcs = funcs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Python による常微分方程式の数値解法 / 古典的 Runge-Kutta 法 | |
class RK4: | |
def __init__(self, funcs, t0, inits, h, numOfDiv=1): | |
self.funcs = funcs | |
self.t0 = t0 | |
self.inits = inits | |
self.dim = len(funcs) | |
self.numOfDiv = numOfDiv | |
self.h = h / self.numOfDiv | |
self.f = [[None for i in range(self.dim)] for i in range(4)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Python による常微分方程式の数値解法 / Shanks による 12 段 8 次の Runge-Kutta 法 | |
class Shanks8: | |
def __init__(self, funcs, t0, inits, h, numOfDiv=1): | |
self.funcs = funcs | |
self.t0 = t0 | |
self.inits = inits | |
self.dim = len(funcs) | |
self.numOfDiv = numOfDiv | |
self.h = h / self.numOfDiv | |
self.f = [[None for i in range(self.dim)] for i in range(12)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Python による常微分方程式の数値解法 / Fehlberg 法 | |
from math import * | |
class Fehlberg: | |
def __init__(self, funcs, t0, inits, h, tol=1e-3): | |
self.funcs = funcs | |
self.t0 = t0 | |
self.inits = inits | |
self.h = h | |
self.numOfDiv = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Python による常微分方程式の数値解法 / 補外法 | |
class Extrapo: | |
def __init__(self, funcs, t0, inits, h, numOfDiv=1): | |
self.funcs = funcs | |
self.t0 = t0 | |
self.inits = inits | |
self.h = h / numOfDiv | |
self.numOfDiv = numOfDiv | |
def __midPoints(self, funcs, t0, inits, H, S=7): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scipy as sp | |
from scipy.integrate import odeint | |
class spODE: | |
def __init__(self, funcs, t0, inits, h=1/2**5, numOfDiv=1): | |
self.funcs = lambda v, t: [func(t, *v) for func in funcs] | |
self.t0 = t0 | |
self.inits = inits | |
self.numOfDiv = numOfDiv | |
self.h = h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 参考: 『インターフェース』 CQ 出版, 2017 年 9 月号, p.109-111, 三上直樹 | |
# ある函数 f(x) が下のような N 次の多項式 g(x) で表現できると假定し、そこそこ精度のある近似式を求める。 | |
# f(x) ≈ g(x) | |
# g(x) ≈ c[0] * T[0] + c[1] * T[1] + ... + c[N] * T[N] | |
# T[N] はチェビシェフ多項式である。 | |
# ここでは例として f(x) = Sin(Pi * x / 2) の近似式を具体的に計算してみる。区間は -1 ≦ x ≦ 1 とする。 | |
# 区間が a ≦ x ≦ b である場合は次式 u(a, b) で変数を変換する。 | |
# u = lambda a, b: (2 * x - (b + a)) / (b - a) | |
import sympy as sym | |
import numpy as np |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from scipy.signal import argrelmax | |
import numpy as np | |
def epsilon(f, coeffList): | |
a, b = -1, 1 # 区間 | |
dim = len(coeffList) | |
f = f | |
g = lambda x : sum([coeffList[n] * x**n for n in np.arange(dim)]) | |
ε = lambda f, g : f - g |