# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3 (Anaconda)
#     language: python
#     name: anaconda3
# ---

import array_calculus as calc
import numpy as np
import matplotlib.pyplot as plt

# # Classwork 06
#
# <NAME>, <NAME>
#
# October 20, 2017

# ## First Derivative as a Matrix
#
# The range of a function is represetned as an $n$ dimension array:
# $$f = \left( \begin{array}{ccc}
# f_0 \\
# f_1 \\
# ... \\
# f_{n-1}\end{array} \right) $$
# while the derivative is represented as an $n$ x $n$ matrix:
# $$D=  \left( \begin{array}{ccc}
# a & b & c \\
# d & e & f \\
# g & h & i \end{array} \right) .$$
# If the derivative is known using the forward difference, central difference, and backwards difference formulas, one can begin to think of values to fill in the $D$ matrix. The forwards difference formula shows that the derivative at $f_0$ must be $\frac{f_1- f_0}{dx}$; the backwards difference shows that the derivative at $f_{n-1}$ must be $\frac{f_{n-1} - f_{n-2}}{dx}$; the central difference formula shows that the derivative at an interior point $f_k$ must be $\frac{f_{k+1} - f_{k-1}}{2dx}$. If we turn this into an $n$ dimension array like the range, it looks like this:
# $$D f = \left( \begin{array}{ccc}
# \frac{f_1- f_0}{dx} \\
# ...\\
# \frac{f_{k+1} - f_{k-1}}{2dx} \\
# ... \\
# \frac{f_{n-1} - f_{n-2}}{dx}\end{array} \right). $$
# If $D f$ is the product of the two matrices, $f$ and $D$, then one can determine the values of the points within the $D$ matrix.
# For a 5x5 matrix, $f$ is $[f_0,f_1,f_2,f_3,f_4]$. If these values are plugged into the previous formulas, points within the $D$ matrix will be filled. The matrix becomes:
# $$D=  \left( \begin{array}{ccc}
# -1&  1 &  0 &  0 &  0 \\
# -0.5 &  0 &  0.5 &  0 & 0\\
# 0 & -0.5 &  0 &  0.5 &  0\\
# 0 & 0 & -0.5 &  0 &  0.5\\
# 0 & 0 & 0 & -1 & 1\end{array} \right) .$$

# ## Second Derivative as a Matrix

# Using the same logic to find the $D$ matrix, the $D_2$ matrix is very formulaic. For each $f_k$, where k = [0,n-1], in the $Df$ matrix, the forward, central, or backwards difference formula is applied in order to obtain the second derivative. This yields a $D_2 f$ matrix such as:
# $$D_2 f = \left( \begin{array}{ccc}
# \frac{\frac{f_2- f_0}{2dx}-\frac{f_1-f_0}{dx}}{dx} \\
# \frac{\frac{f_3- f_1}{2dx}-\frac{f_1-f_0}{dx}}{2dx} \\
# ...\\
# \frac{\frac{f_{k+2} - f_k}{2dx}-\frac{f_k - f_{k-2}}{2dx}}{2dx} \\
# ... \\
# \frac{\frac{f_{n-2} - f_{n-2}}{2dx}-\frac{f_{n-2}-f_{n-4}}{2dx}}{2dx} \\
# \frac{\frac{f_{n-1} - f_{n-2}}{dx}- \frac{f_{n-1}-f_{n-3}}{2dx}}{dx} \end{array} \right). $$
# This time, there are still special cases for the endpoints, but also the points right next to the endpoints. The matrix needed to generate the $D_2 f$ matrix for the same 5x5 matrix from above is:
# $$D_2=  \left( \begin{array}{ccc}
# 2 & -4 & 2 & 0 & 0 \\
# 2 & 3  & 0 & 1 & 0 \\
# 1 & 0 & -2 & 0 & 1 \\
# 0 & 1  & 0 & 3 & 2\\
# 0 & 0 & 2 & -4 & 2\end{array} \right) .$$
#
# This shows that for the very interior points (not including the points right next to the endpoints), the second derivative is calculated using whatever point it is at, as well as a point 2 spaces to the left and 2 spaces to the right. If we were to derive a matrix for the third derivative, we would see the third derivative using the same logic, but with points 3 spaces away on either side.

# ## Relationship between the Matrices

# The second derivative matrix is the product of the first derivatve matrix with itself.
# $$D_2 = D*D = \left( \begin{array}{ccc}
# -1&  1 &  0 &  0 &  0 \\
# -0.5 &  0 &  0.5 &  0 & 0\\
# 0 & -0.5 &  0 &  0.5 &  0\\
# 0 & 0 & -0.5 &  0 &  0.5\\
# 0 & 0 & 0 & -1 & 1\end{array} \right) \left( \begin{array}{ccc}
# -1&  1 &  0 &  0 &  0 \\
# -0.5 &  0 &  0.5 &  0 & 0\\
# 0 & -0.5 &  0 &  0.5 &  0\\
# 0 & 0 & -0.5 &  0 &  0.5\\
# 0 & 0 & 0 & -1 & 1\end{array} \right) =  \left( \begin{array}{ccc}
# 2 & -4 & 2 & 0 & 0 \\
# 2 & 3  & 0 & 1 & 0 \\
# 1 & 0 & -2 & 0 & 1 \\
# 0 & 1  & 0 & 3 & 2\\
# 0 & 0 & 2 & -4 & 2\end{array} \right).
# $$

# ### Example Plots

# #### $x^2$

x,y = calc.gen_f_array(-5,5,500)
calc.plot(x,y,calc.derivative(-5,5,500),calc.second_derivative(-5,5,500), title = "y = $x^2$")

# #### $sin(x)$

x,y = calc.gen_s_array(-np.pi,np.pi,500)
calc.plot(x,y,calc.derivative(-np.pi,np.pi,500),calc.second_derivative(-np.pi,np.pi,500), title = "y = $sin(x)$")

# #### Bellcurve

x,y = calc.gen_g_array(-5,5,500)
calc.plot(x,y,calc.derivative(-5,5,500),calc.second_derivative(-5,5,500), title = "y = $gauss(x)$")

StarcoderdataJupyterScriptsDedupFiltered