# First and Second Derivatives

## Taylor's series expansion

$
\begin{align}
f(t_{n+1}) &= f(t_{n}) + f'(t_{n}) (t_{n+1}-t_{n}) + \frac{f"(t_{n})}{2} (t_{n+1}-t_{n})^2 + \dots \\
&= f(t_{n}) + \Sigma_{d=1}^{\infty} \frac{f^{(d)}(t_{n})}{d!} (t_{n+1}-t_{n})^d 
\end{align}
$

Where $f^{(d)}(t_{n})$ is the $d^{th}$ derivative of function $f()$ at $t=t_n$.

## Forward difference approximation

$
\begin{align}
f(t_{n+1}) &= f(t_{n}) + f'(t_{n}) (t_{n+1}-t_{n}) + O(2)
\end{align}
$

Assuming $O(2)<<1$, and $(t_{n+1}-t_{n}) =h$ then, 

$
\begin{align}
f'(t_{n})  &= \frac{f(t_{n+1})-f(t_{n})}{ (t_{n+1}-t_{n})} \\
&= \left (f(t_{n+1})-f(t_{n})\right)/h
\end{align}
$

## Expansion at $t_{n-1}$

$
\begin{align}
f(t_{n-1}) &= f(t_{n}) - f'(t_{n}) (t_{n}-t_{n-1}) - \frac{f"(t_{n})}{2} (t_{n}-t_{n-1})^2 + \dots \\
&= f(t_{n}) - \Sigma_{d=1}^{\infty} \frac{f^{(d)}(t_{n})}{d!} (t_{n}-t_{n-1})^d 
\end{align}
$

## Second derivative at $t_n$

$
\begin{align}
f(t_{n+1}) &\approx f(t_{n}) - h f'(t_{n}) - \frac{h^2}{2} f"(t_{n})  \\
f(t_{n-1}) &\approx f(t_{n}) - h f'(t_{n}) - \frac{h^2}{2} f"(t_{n})
\end{align}
$

Therefore, 

$
\begin{align}
h^2 f"(t_{n}) &\approx f(t_{n+1}) + f(t_{n-1}) - 2 f(t_{n}) \\
\rightarrow f"(t_{n}) &\approx \frac{f(t_{n+1}) + f(t_{n-1}) - 2 f(t_{n})}{h^2}
\end{align}
$

# Forward difference matrix operation

For example $D^+_1$ for a vector $y$ with 4 elements can be written as the following:

$
\begin{align}
D^+_1 = 
\begin{bmatrix}
    -1& 1& 0& 0 \\
     0&-1& 1& 0 \\
     0& 0&-1& 1 
\end{bmatrix}
\end{align}
$

This can be extended to larger matrices. $y' = D^+_1 \times y$

## Fun program

The derivative should be one, let's check. 


```julia
using Plots

y = collect(1:10)
D = zeros(9,10)
for i=1:9
    D[i,i]  = -1
    D[i,i+1] = 1
end

scatter(D*y, label="y'")
```




    

    



# Backward difference

For example $D_1^-$ for a vector $y$ with 4 elements can be written as the following:

$
\begin{align}
D^-_1 = 
\begin{bmatrix}
     1&-1& 0& 0 \\
     0& 1&-1& 0 \\
     0& 0& 1&-1 
\end{bmatrix}
\end{align}
$

This can be extended to larger matrices. $y' = D^-_1 \times y$

# Second derivative

For example $D_2$ for a vector $y$ with 4 elements can be written as the following:

$
\begin{align}
D_2 = 
\begin{bmatrix}
     1&-2& 1& 0 \\
     0& 1&-2& 1 
\end{bmatrix}
\end{align}
$

This can be extended to larger matrices. $y" = D_2 \times y$

## Fun program

This is the same as second derivative! 


```julia
using Plots

D⁺ = zeros(9,10)        
for i=1:9
    D⁺[i,i]  = -1
    D⁺[i,i+1] = 1
end
D⁻ = -D⁺

D⁻*D⁺'
```




    9×9 Array{Float64,2}:
     -2.0   1.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0
      1.0  -2.0   1.0   0.0   0.0   0.0   0.0   0.0   0.0
      0.0   1.0  -2.0   1.0   0.0   0.0   0.0   0.0   0.0
      0.0   0.0   1.0  -2.0   1.0   0.0   0.0   0.0   0.0
      0.0   0.0   0.0   1.0  -2.0   1.0   0.0   0.0   0.0
      0.0   0.0   0.0   0.0   1.0  -2.0   1.0   0.0   0.0
      0.0   0.0   0.0   0.0   0.0   1.0  -2.0   1.0   0.0
      0.0   0.0   0.0   0.0   0.0   0.0   1.0  -2.0   1.0
      0.0   0.0   0.0   0.0   0.0   0.0   0.0   1.0  -2.0



## Adjourn


```julia
using Dates
println("mahdiar")
Dates.format(now(), "Y/U/d HH:MM")  
```

    mahdiar





    "2021/February/25 13:12"



0ed27e359d423ec6f2366ef1cefce8318244c45d
ipynb
Jupyter Notebook
HW05/1.ipynb
mahdiarsadeghi/NumericalAnalysis
95a0914c06963b0510971388f006a6b2fc0c4ef9
MIT
HW05/1.ipynb
mahdiarsadeghi/NumericalAnalysis
95a0914c06963b0510971388f006a6b2fc0c4ef9
MIT
HW05/1.ipynb
mahdiarsadeghi/NumericalAnalysis
95a0914c06963b0510971388f006a6b2fc0c4ef9
MIT
Qwen/Qwen-72B
1. YES
2. YES