In [ ]:
import math
import numpy as np
import matplotlib.pyplot as plt
from finitediff.grid import locate_discontinuity, pool_discontinuity_approx, adapted_grid, plot_convergence
%matplotlib inline
In [ ]:
n = 50
x = np.linspace(0, 10, n*2)
y = np.empty_like(x)
y[:n] = np.exp(-x[:n])
y[n:] = np.exp(-(x[n:]-x[n]))
print(x[n])
In [ ]:
def plot_discont(x, y, ref, n=None, dn=7, consider=5):
loc_res = locate_discontinuity(x, y, consider)
avg, s = pool_discontinuity_approx(loc_res)
fig, axes = plt.subplots(1, 2, figsize=(14, 4))
for ax in axes:
ax.axvline(ref, color='k', linestyle='-')
ax.axvline(avg, color='r', linestyle=':')
ax.axvline(avg-s, color='g', linestyle='--')
ax.axvline(avg+s, color='g', linestyle='--')
x, y = map(lambda a: np.asarray(a, dtype=np.float64), [x, y])
axes[0].plot(x, y)
if n is None:
n = np.argmin(np.abs(x - ref))
axes[1].plot(x[n-dn:n+dn], y[n-dn:n+dn])
return avg, s
In [ ]:
plot_discont(x, y, x[n])
In [ ]:
f = np.vectorize(lambda t: math.exp(-t) if t < 5 else math.exp(5-t))
plt.plot(x, f(x))
In [ ]:
g, v = adapted_grid(0, 10, f, grid_additions=(8,)*4)
In [ ]:
plot_convergence(None, [None], f, grid_additions=(8,)*8, xstart=0, xstop=10)
In [ ]:
plot_discont(g, v, x[n], n)
In [ ]:
f2 = np.vectorize(lambda t: (math.exp(-t) + (.2 if int(round(t*1234567)) % 3 == 0 else 0)) if t < 5 else 0.5*math.exp(5-t))
plot_convergence(None, [None], f2, grid_additions=(16,)*8, xstart=0, xstop=10)
In [ ]:
plot_convergence(None, [None], f2, grid_additions=(16,)*8, xstart=0, xstop=10, snr=True)
In [ ]:
plot_discont(*adapted_grid(0, 10, f2, grid_additions=(22,)*12, snr=True), ref=5, consider=5)
In [ ]:
pool_discontinuity_approx(locate_discontinuity(*adapted_grid(0, 10, f2, grid_additions=(16,)*8, snr=True), consider=5))