Skip to content

Instantly share code, notes, and snippets.

@santiago-salas-v
Last active September 23, 2019 18:47
Show Gist options
  • Save santiago-salas-v/837d6d1b1c682a470d10d6fd6f973380 to your computer and use it in GitHub Desktop.
Save santiago-salas-v/837d6d1b1c682a470d10d6fd6f973380 to your computer and use it in GitHub Desktop.
logarithmic extrapolation
from matplotlib import pyplot as plt
from numpy.random import random
from numpy import linspace, log
%matplotlib inline
x = linspace(0.01, 2, 40)
rand_list = (random(len(x))-0.5)
y = log(x-1e-3) + rand_list
y2 = log(x+1e-2*8) + rand_list
x_mid, y_mid = x[int(len(x)/2)], y[int(len(x)/2)]
x_low, y_low = x[3], y[3]
ex_x = -1e-4
d = -1e-3
a = (y_low-y_mid)/log((x_low+d)/(x_mid+d))
c = y_low - a * log(x_low + d)
print('a\tc\td')
print(('{:0.4g}\t'*3).format(a,c,d))
x_ex = linspace(1e-2, 2, 30)
y_ex = a * log(x_ex + d) + c
x_ex_lin = linspace(-1e-1, 0.75, 3)
y_ex_lin = y_low + a/(x_low + d) * (x_ex_lin - x_low)
plt.plot(x, y, 'o', label='$y=ln(x+d_1)+c\pm rand$')
plt.plot(x, y2, 'o', label='$y=ln(x+d_2)+c\pm rand$')
plt.plot([x_low, x_mid], [y_low, y_mid], 'o', markersize=15)
plt.plot(x_ex, y_ex, '--', color='black',
label='$y=ln(x+d_1)+c$')
plt.plot(x_ex_lin, y_ex_lin, ':',
label=r'$y=\left(\frac{dy}{dx}\right)|_{x1}(x-x_1)+y_1$')
plt.xlabel('x'); plt.ylabel('y')
plt.legend();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment