Skip to content

Instantly share code, notes, and snippets.

@ti-nspire
ti-nspire / FehlbergClass.lua
Last active October 13, 2017 20:21
FehlbergClass.lua
-- 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
@ti-nspire
ti-nspire / extrapoClass.lua
Last active December 21, 2017 09:13
extrapoClass.lua
-- 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
@ti-nspire
ti-nspire / graggClass.lua
Last active December 18, 2017 02:20
graggClass.lua
-- 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
@ti-nspire
ti-nspire / RK4.py
Last active September 23, 2020 00:04
Python による常微分方程式の数値解法 / 古典的 Runge-Kutta 法
# 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)]
@ti-nspire
ti-nspire / Shanks8.py
Last active January 30, 2018 23:28
Python による常微分方程式の数値解法 / Shanks による 12 段 8 次の Runge-Kutta 法
# 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)]
@ti-nspire
ti-nspire / Fehlberg.py
Last active December 9, 2022 09:33
Python による常微分方程式の数値解法 / Fehlberg 法
# 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
@ti-nspire
ti-nspire / extrapo.py
Last active January 22, 2018 23:39
Python による常微分方程式の数値解法 / 補外法
# 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):
@ti-nspire
ti-nspire / spODE.py
Last active February 4, 2018 00:18
Python による常微分方程式の数値解法 / scipy.integrate.odeint
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
@ti-nspire
ti-nspire / cheby.py
Last active February 26, 2018 23:30
チェビシェフ補間で多項式近似式を求める
# 参考: 『インターフェース』 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
@ti-nspire
ti-nspire / epsilon.py
Last active March 31, 2018 23:43
近似多項式との誤差の極値を求める
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