<jupyter_start><jupyter_text>Numerical differentiation**Symbolic differentiation** uses algebra and symbolic rules to find explicit formula for a derivative of a function, $f(x)$, at some value, $x$, $$ \frac{df(x)}{dx} \equiv \lim_{h \to 0} \frac{f(x+h)-f(x)}{h} $$ **Numerical differentiation** instead says lets allow h to stay finite (but _small_) and approximate the derivative as $$ \frac{df(x)}{dx}  \approx \frac{f(x+h)-f(x)}{h} $$<jupyter_code>#
# Try a numerical derivative for x^2
#
x=2;
h=0.001;
print( ( (x+h)**2.-x**2. )/h )<jupyter_output><empty_output><jupyter_text>Lets think about how accurate the derivative is<jupyter_code># First lets define a function for f, its true derivative and a numerical derivative
def f(x):
    return x**2

def a_dfdx(x):
    return 2*x

def n_dfdx(x,h):
    return (f(x+h)-f(x))/h
# Now lets define some different sizes of h using a "generator" expression
h0=0.1
hvals=[h0**n for n in [1,2,3,4,5,6,7,8,9,10] ]
x=2
n_deriv=[n_dfdx(x,h) for h in hvals ]
a_deriv=a_dfdx(x)
import matplotlib.pyplot as plt
plt.plot(hvals,n_deriv-a_deriv)
import numpy as np
n_deriv_np=np.array(n_deriv)
plt.plot(np.log10(np.abs(n_deriv_np-a_deriv)),np.log10(hvals),'o');
plt.xlim(0,-10)
plt.xlabel("log10(error)")
plt.ylabel("log10(h)");<jupyter_output><empty_output><jupyter_text>What about high-order derivativese.g. $ \frac{d^{2}\phi}{dx^{2}} $ we can create these as derivatives of lower order derivativese.g. $ \frac{d^{2}\phi}{dx^{2}} \equiv \frac{d}{dx}\frac{d\phi}{dx}$This approach yields one formula for the second derivative$$ \frac{d^{2}f(x)}{dx^{2}}  \approx \frac{f(x-h)+f(x+h)-2f(x)}{h^{2}} $$<jupyter_code># We can try this here
def n_d2fdx2(x,h):
    return (f(x-h)+f(x+h)-2*f(x))/(h**2)
def a_d2fdx2(x):
    return 2
x=2
n_deriv2=[n_d2fdx2(x,h) for h in hvals ]
n_deriv2_np=np.array(n_deriv2)
a_deriv2=a_d2fdx2(x)
plt.plot(np.log10(np.abs(n_deriv2_np-a_deriv2)),np.log10(hvals),'o');
plt.xlim(0,-14)
plt.xlabel("log10(error)")
plt.ylabel("log10(h)");<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1885488647581441, 'fraction_numerical': 0.037018756169792694, 'mean_word_length': 6.062717770034843, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '409487', 'license': 'MIT', 'n_tokens_mistral': 910, 'n_tokens_neox': 863, 'n_words': 188, 'path': 'Lec05-deriv.ipynb', 'repo_name': 'christophernhill-binders/fall-2021-12.010', 'size': '2026'}