# https://en.wikipedia.org/wiki/Finite_difference

Three forms are commonly considered: forward, backward, and central differences.[1][2][3]

A forward difference is an expression of the form

$$ \displaystyle \Delta _{h}[f](x)=f(x+\Delta x)-f(x).$$
Depending on the application, the spacing h may be variable or constant. When omitted, $\Delta x=h$ is taken to be 1: Δ[ f ](x) = Δ1[ f ](x).

A backward difference uses the function values at x and x − \Delta, instead of the values at x + \Delta and x:

$$ \displaystyle \nabla _{h}[f](x)=f(x)-f(x-\Delta x).$$

Finally, the central difference is given by

$$\displaystyle \delta _{h}[f](x) = f\left(x+{\tfrac {1}{2}}\Delta x\right)-f\left(x-{\tfrac {1}{2}}\Delta x \right) $$

The derivative of a function f at a point x is defined by the limit.

$$ f'(x)=\lim_{h\to 0} {\frac {f(x+h)-f(x)}{h}} $$


```python
# red dashes, blue squares and green triangles
#Example: [a,b], n
# https://matplotlib.org/users/pyplot_tutorial.html
import numpy as np
import matplotlib.pyplot as plt
a=0
b=1
n=3
deltax=(b-a)/n
deltax
# evenly sampled time at delta x intervals
x = np.arange(a, b+deltax, deltax)
#x = np.linspace(a, b, n+1)
x
x = np.linspace(-3, 3, 50)
y2 = x**2+1


plt.figure()
#set x limits
plt.xlim((0, 2))
plt.ylim((0, 3))

# set new sticks
new_sticks = np.linspace(0, 2, 5)
plt.xticks(new_sticks)
# set tick labels
plt.yticks(np.arange(0, 5, step=1))

# set line styles

l2, = plt.plot(x, y2, color='red', linewidth=1.0, linestyle='--', label='f(x)= x^2+1')

plt.legend(loc='upper left')

plt.show()
```

plot a secant line pass the points (0,1) and (1,2)



```python
import matplotlib.pyplot as plt
import numpy as np

def main():
    # x = np.linspace(-2,2,100)
    a=-2
    b=3
    divx=0.01
    x = np.arange(a, b, divx)
    x1=0
    p1 = int((x1-a)/divx)  #starts from zero
    deltax=1
    count_deltax=int(deltax/divx)
    p2 = p1+ count_deltax #starts from zero

    y1 = main_func(x)
    y2 = calculate_secant(x, y1, p1, p2)
    plot(x, y1, y2)
    plt.show()

def main_func(x):
    return x**2+1

def calculate_secant(x, y, p1, p2):
    points = [p1, p2]
    m, b = np.polyfit(x[points], y[points], 1)
    return m * x + b

def plot(x, y1, y2):
    plt.plot(x, y1)
    plt.plot(x, y2)
    #set x limits
    plt.xlim((-2, 2))
    #set x limits
    plt.ylim((0, 4))

main()
```

forward difference


```python
x=0
deltax=1
main_func(x+deltax)-main_func(x)
```




    1




```python
plot a tangent secant line pass the points (0,1)
```


```python
import matplotlib.pyplot as plt
import numpy as np

def main():
    # x = np.linspace(-2,2,100)
    a=-2
    b=3
    divx=0.01
    x = np.arange(a, b, divx)
    x1=1
    p1 = int((x1-a)/divx)  #starts from zero
    deltax=0.02
    count_deltax=int(deltax/divx)
    p2 = p1+ count_deltax #starts from zero

    y1 = main_func(x)
    y2 = calculate_secant(x, y1, p1, p2)
    plot(x, y1, y2)
    plt.show()

def main_func(x):
    return x**2+1

def calculate_secant(x, y, p1, p2):
    points = [p1, p2]
    m, b = np.polyfit(x[points], y[points], 1)
    return m * x + b

def plot(x, y1, y2):
    plt.plot(x, y1)
    plt.plot(x, y2)
    #set x limits
    plt.xlim((-2, 2))
    #set x limits
    plt.ylim((0, 4))

main()
```


```python
import matplotlib.pyplot as plt
import numpy as np

def main():
    # x = np.linspace(-2,2,100)
    a=-2
    b=3
    divx=0.01
    x = np.arange(a, b, divx)
    x1=1
    p1 = int((x1-a)/divx)  #starts from zero
    deltax=0.01
    count_deltax=int(deltax/divx)
    p2 = p1+ count_deltax #starts from zero

    y1 = main_func(x)
    y2 = calculate_secant(x, y1, p1, p2)
    plot(x, y1, y2)
    plt.show()

def main_func(x):
    return x**2+1

def calculate_secant(x, y, p1, p2):
    points = [p1, p2]
    m, b = np.polyfit(x[points], y[points], 1)
    print(m)
    return m * x + b

def plot(x, y1, y2):
    plt.plot(x, y1)
    plt.plot(x, y2)
    
    #set x limits
    plt.xlim((-2, 2))
    #set x limits
    plt.ylim((0, 4))
    

main()

```


```python
x=1
deltax=0.00000000001
(main_func(x+deltax)-main_func(x))/deltax
```




    2.000000165480742



### $$ f'(x)=\lim_{h\to 0} {\frac {f(x+h)-f(x)}{h}} $$
$$ f'(x)={\frac {f(1+2)-f(1)}{2}} = 4$$

The derivative of a function f at a point x is defined by the limit.

$$ f'(x)=\lim_{h\to 0} {\frac {f(x+h)-f(x)}{h}} $$

http://www.math.unl.edu/~s-bbockel1/833-notes/node23.html
forward difference approximation:
$$ f'(x)={\frac {f(x+h)-f(x)}{h}}+O(h) $$

$$ f'(1)=?$$


```python
from sympy import diff, Symbol, sin, tan, limit
x = Symbol('x')
diff(main_func(x), x)
limit(main_func(x), x, 1)
```




    2




```python
import matplotlib.pyplot as plt
import numpy as np

def main():
    # x = np.linspace(-2,2,100)
    a=-2
    b=3
    divx=0.01
    x = np.arange(a, b, divx)
    x1=1
    p1 = int((x1-a)/divx)  #starts from zero
    deltax=0.01
    count_deltax=int(deltax/divx)
    p2 = p1+ count_deltax #starts from zero

    y1 = main_func(x)
    y2 = calculate_secant(x, y1, p1, p2)
    plot(x, y1, y2)
    plt.show()

def main_func(x):
    return x**2+1

def calculate_secant(x, y, p1, p2):
    points = [p1, p2]
    m, b = np.polyfit(x[points], y[points], 1)
    print(m)
    return m * x + b

def plot(x, y1, y2):
    plt.plot(x, y1)
    plt.plot(x, y2)
    
    #set x limits
    plt.xlim((-2, 2))
    #set x limits
    plt.ylim((0, 4))
    

main()
```


A backward difference uses the function values at x and x − \Delta, instead of the values at x + \Delta and x:

$$ f'(x)=\lim_{h\to 0} {\tfrac{f(x)-f(x-\Delta x)}{\Delta x}}$$




```python
x=1
deltax=0.0001
(main_func(x)-main_func(x-deltax))/deltax
```




    1.9999000000003875




Finally, the central difference is given by

$$f'(x)=\lim_{h\to 0} {\tfrac {f\left(x+{\tfrac {1}{2}}\Delta x\right)-f\left(x-{\tfrac {1}{2}}\Delta x \right)}{\Delta x}} $$


```python
x=1
deltax=0.0001
(main_func(x+deltax*(1/2))-main_func(x-deltax*(1/2)))/deltax
```




    1.9999999999997797




```python

```

cf13ca4a52764c291cef3170c48940cc3daf0309
ipynb
Jupyter Notebook
student_hw_20181029.ipynb
tccnchsu/Numerical_Analysis
6bc60672c0c417f1194cb507d1b65ac1691fbb8d
MIT
2019-03-16T15:26:47.000Z
2019-03-16T15:26:47.000Z
student_hw_20181029.ipynb
tccnchsu/Numerical_Analysis
6bc60672c0c417f1194cb507d1b65ac1691fbb8d
MIT
student_hw_20181029.ipynb
tccnchsu/Numerical_Analysis
6bc60672c0c417f1194cb507d1b65ac1691fbb8d
MIT
2018-10-29T03:52:02.000Z
2018-12-10T02:43:51.000Z
Qwen/Qwen-72B
1. YES
2. YES