#+title: Envelope
#+PROPERTY: header-args:jupyter-python  :session /home/ethan/.local/share/jupyter/runtime/kernel-4e3a8cd0-e74a-4b89-b546-3f613d76b5a6.json
#+PROPERTY: header-args :async "yes"

#+begin_src jupyter-python
  import numpy as np
  import matplotlib.pyplot as plt
  from scipy.interpolate import CubicSpline
#+end_src

#+RESULTS:

#+begin_src jupyter-python
  x = np.linspace(0, np.pi / 2.5, 100)
  y = np.sin(x ** 2.0)
  idx = [0, 33, 40, 80, 90]
  plt.figure()
  plt.plot(x, y, linewidth=3, label='$\lambda(t)$')
  for i in range(len(idx) - 1):
      xx = [x[idx[i]], x[idx[i + 1]]]
      yy = [y[idx[i]], y[idx[i + 1]]]
      if i == 0:
          plt.plot(xx, yy, 'r--', linewidth=5, alpha=0.75, label='$h(t)$')
          plt.scatter([xx[0]], [yy[0]], c='blue', linewidths=5, label='$T$')
      else:
          plt.plot(xx, yy, 'r--', linewidth=5, alpha=0.75)
          plt.scatter([xx[0]], [yy[0]], c='blue', linewidths=5)

  plt.legend()
  plt.show()
  print(xx[0])
  print(yy[0])
#+end_src

#+RESULTS:
:RESULTS:
[[file:./.ob-jupyter/9873bec16a0227f8da71d900515973d1aedf6ad6.png]]
: 1.0154642920694281
: 0.857899573171145
:END:

#+begin_src jupyter-python
  x = np.linspace(0, np.pi / 2, 100)
  y = np.sin(x)
  idx = [0, 33, 40, 80, 90]
  plt.figure()
  plt.plot(x, y, linewidth=3, label='$\lambda(t)$')
  for i in range(len(idx) - 1):
      xx = [x[idx[i]], x[idx[i + 1]]]
      yy = [y[idx[i]], y[idx[i + 1]]]
      if i == 0:
          plt.plot(xx, yy, 'r--', linewidth=5, alpha=0.75, label='$h(t)$')
          plt.scatter([xx[0]], [yy[0]], c='blue', linewidths=5, label='$T$')
      else:
          plt.plot(xx, yy, 'r--', linewidth=5, alpha=0.75)
          plt.scatter([xx[0]], [yy[0]], c='blue', linewidths=5)

  plt.legend()
  plt.show()
  print(xx[0])
  print(yy[0])
#+end_src

#+RESULTS:
:RESULTS:
[[file:./.ob-jupyter/57317c382b3f261c67381d7be1949712c887ed27.png]]
: 1.269330365086785
: 0.9549022414440739
:END:

#+begin_src jupyter-python
  def slope_intercept(x0, x1, y0, y1):
     slope = (y1 - y0) / (x1 - x0)
     intercept = y1 - slope * x1
     return slope, intercept
#+end_src



#+RESULTS:

#+begin_src jupyter-python
plt.rcParams.update({
    "text.usetex": True,
    "font.family": "Helvetica"
})
#+end_src

#+RESULTS:


#+begin_src jupyter-python
  plt.close()
  gamma = 1.2
  x = np.linspace(0.001, np.pi / 2, 100)
  y = 3 * np.sin(x)
  # if np.min(y) < 0:
  y = y - np.min(y)
  idx = [0, 30, 40, 60, 95]
  fig, axs = plt.subplots(4, 1, figsize=(4, 12))
  for i in range(len(idx) - 1):
      axs[i].plot(x, y, linewidth=3, label='$\lambda(t)$')
      for j in range(0, i):
          xx = [x[idx[j]], x[idx[j + 1]]]
          yy = [y[idx[j]], y[idx[j + 1]]]
          yy_offset = [gamma * y[idx[j]], gamma * y[idx[j + 1]]]
          axs[i].plot(xx, yy_offset, 'r--', linewidth=3, alpha=0.75, label='$h(t)$')
          # axs[i].scatter(xx, yy, c='blue', linewidths=5, label='$T$')
          axs[i].annotate(f'$t_{j + 1}$', xy=(xx[1],-0.5), xycoords='data')
          axs[i].vlines(x=x[idx[j]], ymin=0, ymax=y[idx[j]], linestyle='--', color='black', alpha=0.4)
          axs[i].vlines(x=x[idx[j+1]], ymin=0, ymax=y[idx[j+1]], linestyle='--', color='black', alpha=0.4)

      axs[i].annotate(f'$t_{0}$', xy=(x[idx[0]],-0.5), xycoords='data')
      if i == 0:
          axs[i].annotate('$t_{init}$', xy=(x[idx[1] - 20],-0.5), xycoords='data')
          axs[i].vlines(x=x[idx[1]- 20], ymin=0, ymax=y[idx[1] - 20], linestyle='--', color='black', alpha=0.4)
          axs[i].scatter(x[idx[1]- 20], y[idx[1]- 20],  marker='X', c='black', s=100, label='$T$', alpha=0.8)
          print('here', y[idx[1] - 20])
      # axs[i].set_axis_off()
      # get slope and intercept
      slope, intercept = slope_intercept(x[idx[i + 1]], x[idx[i]], gamma * y[idx[i + 1]], gamma * y[idx[i]]  )
      x_extrapolate = np.array([x[idx[i]], x[-1]])
      print(x_extrapolate)
      y_extrapolate = slope * x_extrapolate + intercept
      axs[i].set_ylim(-1, np.max(y_extrapolate))
      axs[i].plot(x_extrapolate, y_extrapolate)
  # plt.show()
#+end_src

#+RESULTS:
:RESULTS:
: here 0.4736670892806964
: [1.00000000e-03 1.57079633e+00]
: [0.47669586 1.57079633]
: [0.63526114 1.57079633]
: [0.95239171 1.57079633]
#+attr_org: :width 428
[[file:./.ob-jupyter/d9954dc923cdbe1aec2c035e53b839bf6b2b4998.png]]
:END:

#+begin_src jupyter-python
  gamma = 1.2
  x = np.linspace(0.001, np.pi / 2, 100)
  y = 3 * np.sin(x)
  # if np.min(y) < 0:
  y = y - np.min(y)
  idx = [0, 30, 40, 60, 95]
  for i in range(len(idx) - 1):
      fig, axs = plt.subplots(1, 1, figsize=(4, 4))
      axs.plot(x, y, linewidth=3, label='$\lambda(t)$')
      for j in range(0, i):
          xx = [x[idx[j]], x[idx[j + 1]]]
          yy = [y[idx[j]], y[idx[j + 1]]]
          yy_offset = [gamma * y[idx[j]], gamma * y[idx[j + 1]]]
          axs.plot(xx, yy_offset, 'r--', linewidth=3, alpha=0.75, label='$h(t)$')
          axs.scatter(xx[1], yy_offset[1], c='black', alpha=0.25, linewidths=2, label='$T$')
          axs.annotate(f'$t_{j + 1}$', xy=(xx[1],-0.5), xycoords='data', fontsize=20)
          axs.vlines(x=x[idx[j]], ymin=0, ymax=y[idx[j]], linestyle='--', color='black', alpha=0.4)
          axs.vlines(x=x[idx[j+1]], ymin=0, ymax=y[idx[j+1]], linestyle='--', color='black', alpha=0.4)

      axs.annotate(f'$t_{0}$', xy=(x[idx[0]],-0.5), xycoords='data', fontsize=20)
      if i == 0:
          axs.annotate('$t_{init}$', xy=(x[idx[1] - 20],-0.5), xycoords='data', fontsize=20)
          axs.vlines(x=x[idx[1]- 20], ymin=0, ymax=y[idx[1] - 20], linestyle='--', color='black', alpha=0.4)
          axs.scatter(x[idx[1]- 20], y[idx[1]- 20],  marker='X', c='black', s=100, label='$T$', alpha=0.8)
      axs.set_axis_off()
      # get slope and intercept
      slope, intercept = slope_intercept(x[idx[i + 1]], x[idx[i]], gamma * y[idx[i + 1]], gamma * y[idx[i]]  )
      x_extrapolate = np.array([x[idx[i]], x[-1]])
      print(x_extrapolate)
      y_extrapolate = slope * x_extrapolate + intercept
      axs.set_ylim(-1, 4)
      axs.set_xlim(-0.1, 1.8)
      axs.plot(x_extrapolate, y_extrapolate)

      fig.savefig(f'/home/ethan/Documents/uai2023/figs/interpolation_{i}.png')
      fig.savefig(f'/home/ethan/Documents/uai2023/figs/interpolation_{i}.pdf')

#+end_src

#+RESULTS:
:RESULTS:
: [0.47669586 1.57079633]
: [0.63526114 1.57079633]
: [0.95239171 1.57079633]
#+attr_org: :width 396
[[file:./.ob-jupyter/6b152895e78844f9942935c3b46608aff146204d.png]]
#+attr_org: :width 396
[[file:./.ob-jupyter/3c92b35b69e08010498b0704e0d4bfde870b7009.png]]
#+attr_org: :width 396
[[file:./.ob-jupyter/dc92f769a0b45bcb621cf4d056d163d3c81ebb0a.png]]
#+attr_org: :width 396
[[file:./.ob-jupyter/b5103ad0e308d8a7015d132c6b39d8ae92e5c2d4.png]]
: [1.00000000e-03 1.57079633e+00]
: [0.47669586 1.57079633]
: [0.63526114 1.57079633]
: [0.95239171 1.57079633]
#+attr_org: :width 396
[[file:./.ob-jupyter/6b152895e78844f9942935c3b46608aff146204d.png]]
#+attr_org: :width 396
[[file:./.ob-jupyter/3c92b35b69e08010498b0704e0d4bfde870b7009.png]]
#+attr_org: :width 396
[[file:./.ob-jupyter/dc92f769a0b45bcb621cf4d056d163d3c81ebb0a.png]]
#+attr_org: :width 396
[[file:./.ob-jupyter/b5103ad0e308d8a7015d132c6b39d8ae92e5c2d4.png]]
:END:
