# 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()
```


    <matplotlib.figure.Figure at 0x7fef985b8e48>


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()
```

Q1: Please draw a tangent line at the poit (1,2) of the function f(x).


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

def main():
    # x = np.linspace(-2,2,100)
    a=-2
    b=2
    divx=0.001
    x = np.arange(a, b, divx)
    x1=1
    p1 = int((x1-a)/divx)  #starts from zero
    deltax=0.01
    x2=x1+deltax
    count_deltax=int((x2-x1)/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()
```

Q2: Please calculate the approximate value of the slope of a tangent line at the poit (1,2) of the function f(x).

The slope of the tangent line at a point $x=1$ is equal to the value of the derivative of a function f at a point $x=1$.

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

$$ f'(x)=\lim_{h\to 0} {\frac {f(x+h)-f(x)}{h}}=2x $$
$$ f'(x)={\frac{d}{dx}} f(x)={\frac{d}{dx}}x^2=2x $$
$$ f'(x=1)=2*1=2 $$



The derivative of a function f at a point $x=1$ is defined by the limit.
h=0.01
$$ f'(x=1)=\lim_{h\to 0} {\frac {f(x+h)-f(x)}{h}} $$
h=0.01


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




    2*x



forward difference


```python
x=1
h=0.01
slope=(main_func(x+h)-main_func(x))/h
print(slope)
```

    2.010000000000023


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)=?$$

Newton's method
https://en.wikipedia.org/wiki/Newton%27s_method

86c7c9f5e9f8e6cf62e8a98cda443cd771aac6c9
ipynb
Jupyter Notebook
derivative.ipynb
bonnielin1111/Numerical_Analysis
2a8b9193a04f2f206e191f7c1791b18301232752
MIT
2019-03-16T15:26:47.000Z
2019-03-16T15:26:47.000Z
derivative.ipynb
bonnielin1111/Numerical_Analysis
2a8b9193a04f2f206e191f7c1791b18301232752
MIT
derivative.ipynb
bonnielin1111/Numerical_Analysis
2a8b9193a04f2f206e191f7c1791b18301232752
MIT
2018-10-29T03:52:02.000Z
2018-12-10T02:43:51.000Z
Qwen/Qwen-72B
1. YES
2. YES