```python
%pylab inline
```

    Populating the interactive namespace from numpy and matplotlib



```python
N = 11
h = 1/(N-1)
x = linspace(0,1,N)
```


```python
f = ones((N,))
```


```python
A = zeros((N,N))
for i in range(1,N-1):
    A[i, i-1] = A[i, i+1] = -1
    A[i,i] = 2
A[0,0] = A[-1,-1] = 1
f[0] = f[-1] = 0

A = A/h**2
```


```python
A, f
```




    (array([[ 100.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
                0.,    0.],
            [-100.,  200., -100.,    0.,    0.,    0.,    0.,    0.,    0.,
                0.,    0.],
            [   0., -100.,  200., -100.,    0.,    0.,    0.,    0.,    0.,
                0.,    0.],
            [   0.,    0., -100.,  200., -100.,    0.,    0.,    0.,    0.,
                0.,    0.],
            [   0.,    0.,    0., -100.,  200., -100.,    0.,    0.,    0.,
                0.,    0.],
            [   0.,    0.,    0.,    0., -100.,  200., -100.,    0.,    0.,
                0.,    0.],
            [   0.,    0.,    0.,    0.,    0., -100.,  200., -100.,    0.,
                0.,    0.],
            [   0.,    0.,    0.,    0.,    0.,    0., -100.,  200., -100.,
                0.,    0.],
            [   0.,    0.,    0.,    0.,    0.,    0.,    0., -100.,  200.,
             -100.,    0.],
            [   0.,    0.,    0.,    0.,    0.,    0.,    0.,    0., -100.,
              200., -100.],
            [   0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,    0.,
                0.,  100.]]),
     array([0., 1., 1., 1., 1., 1., 1., 1., 1., 1., 0.]))




```python
u = linalg.solve(A, f)
```


```python
plot(x, u, 'o-b')
plot(x, 0.5*(x*(1-x)), 'r')
```


```python
exact = 0.5*(x*(1-x))
error = max(abs(exact-u))
```


```python
error
```




    4.163336342344337e-17




```python
exact = x*(1-x)*(x-.85)
```


```python
import sympy as sym
t = sym.var('x')
exact_t = t*(1-t)*(t-.85)
fsymbol = sym.lambdify(t, -exact_t.diff(t, 2) )

x = linspace(0,1,N)
f = fsymbol(x)
```


```python
x = linspace(0,1,N)
f = fsymbol(x)
f[0] = f[-1] = 0
```


```python
plot(x,f)
```


```python
u = linalg.solve(A, f)
```


```python
plot(x, u, 'ob-')
plot(x, exact)
```


```python
max(abs(u - exact))
```




    3.469446951953614e-17




```python
x = sym.var('x')
h = sym.var('h')
g = sym.Function('g')
```


```python
def cfd_II(x,h,g):
    return (g(x+h)- 2*g(x) + g(x-h))/h**2

def back_fd(x,h,g):
    return (g(x+h)- g(x))/h

def forward_fd(x,h,g):
    return (g(x)- g(x-h))/h

def central_fd(x,h,g):
    return (g(x+h)- g(x-h))/(2*h)
```


```python
sym.series(back_fd(x, h, g), x=h, x0=0, n=2)
```




$\displaystyle \left. \frac{d}{d \xi_{1}} g{\left(\xi_{1} \right)} \right|_{\substack{ \xi_{1}=x }} + \frac{h \left. \frac{d^{2}}{d \xi_{1}^{2}} g{\left(\xi_{1} \right)} \right|_{\substack{ \xi_{1}=x }}}{2} + O\left(h^{2}\right)$




```python
sym.series(forward_fd(x, h, g), x=h, x0=0, n=2)
```




$\displaystyle \left. \frac{d}{d \xi_{1}} g{\left(\xi_{1} \right)} \right|_{\substack{ \xi_{1}=x }} - \frac{h \left. \frac{d^{2}}{d \xi_{1}^{2}} g{\left(\xi_{1} \right)} \right|_{\substack{ \xi_{1}=x }}}{2} + O\left(h^{2}\right)$




```python
sym.series(central_fd(x, h, g), x=h, x0=0, n=3)
```




$\displaystyle \left. \frac{d}{d \xi_{1}} g{\left(\xi_{1} \right)} \right|_{\substack{ \xi_{1}=x }} + \frac{h^{2} \left. \frac{d^{3}}{d \xi_{1}^{3}} g{\left(\xi_{1} \right)} \right|_{\substack{ \xi_{1}=x }}}{6} + O\left(h^{3}\right)$




```python
sym.series(cfd_II(x, h, g), x=h, x0=0, n=5)
```




$\displaystyle \left. \frac{d^{2}}{d \xi_{1}^{2}} g{\left(\xi_{1} \right)} \right|_{\substack{ \xi_{1}=x }} + \frac{h^{2} \left. \frac{d^{4}}{d \xi_{1}^{4}} g{\left(\xi_{1} \right)} \right|_{\substack{ \xi_{1}=x }}}{12} + \frac{h^{4} \left. \frac{d^{6}}{d \xi_{1}^{6}} g{\left(\xi_{1} \right)} \right|_{\substack{ \xi_{1}=x }}}{360} + O\left(h^{5}\right)$



92b0ebf27f35b7875311ede4453c019a3563b8ea
ipynb
Jupyter Notebook
slides/.ipynb_checkpoints/Lecture 10 - LH - LAB - Introduction to PDEs - Finite Differences in 1D-checkpoint.ipynb
vitturso/numerical-analysis-2021-2022
d675a6f766a42d0a46e7cd69dbfed8645a0b2590
CC-BY-4.0
slides/.ipynb_checkpoints/Lecture 10 - LH - LAB - Introduction to PDEs - Finite Differences in 1D-checkpoint.ipynb
vitturso/numerical-analysis-2021-2022
d675a6f766a42d0a46e7cd69dbfed8645a0b2590
CC-BY-4.0
slides/.ipynb_checkpoints/Lecture 10 - LH - LAB - Introduction to PDEs - Finite Differences in 1D-checkpoint.ipynb
vitturso/numerical-analysis-2021-2022
d675a6f766a42d0a46e7cd69dbfed8645a0b2590
CC-BY-4.0
Qwen/Qwen-72B
1. YES
2. YES