# Resolución del problema de Laplace 1D con condiciones Dirichlet

Calcular $u:[a,b] \to R$ solución de
$$
\begin{align}
  \label{pb1d}
  -\frac{d^2 u(x) }{dx^2} = f(x) \mbox{ en } [a,b], \\
  u(a)=u_a, \ u(b)=u_b,
\end{align}
$$


## Sistema de ecuaciones MDF 1D


```python
from numpy import *
```


```python
# Datos:
a, b = 0, 1 #Extremos del intervalo
n = 10 # Números de puntos interiores en la partición, x_1,..., x_n
h = 1./(n+1) # Tamaño de la partición
ua, ub = 0, 1  # Datos de contorno
f = lambda x: pi**2/4 * sin(x*pi/2)
```

### Matriz


```python
# Caso n=3
A=array( 
    [
    [2,-1,0], # Fila1
    [-1,2,-1], # Fila 2
    [0,-1,2]  # Fila 3
    ]
)
print("n=3:\n", A)
# Caso general:
A_h = (1./h**2) * (
    2*diag( ones(n) ) 
    - diag( ones(n-1), +1 ) 
    - diag( ones(n-1), -1)
)
print("Caso general:\n", A_h)
```

    n=3:
     [[ 2 -1  0]
     [-1  2 -1]
     [ 0 -1  2]]
    Caso general:
     [[ 242. -121.    0.    0.    0.    0.    0.    0.    0.    0.]
     [-121.  242. -121.    0.    0.    0.    0.    0.    0.    0.]
     [   0. -121.  242. -121.    0.    0.    0.    0.    0.    0.]
     [   0.    0. -121.  242. -121.    0.    0.    0.    0.    0.]
     [   0.    0.    0. -121.  242. -121.    0.    0.    0.    0.]
     [   0.    0.    0.    0. -121.  242. -121.    0.    0.    0.]
     [   0.    0.    0.    0.    0. -121.  242. -121.    0.    0.]
     [   0.    0.    0.    0.    0.    0. -121.  242. -121.    0.]
     [   0.    0.    0.    0.    0.    0.    0. -121.  242. -121.]
     [   0.    0.    0.    0.    0.    0.    0.    0. -121.  242.]]


### Segundo miembro


```python
f_h = []
x = linspace(0, 1, num=n) # x_0, ..., x_{n-1}
#for i in range(n): # i 0,...n-1
#    f_h.append( f(x[i]) )
f_h = f(x) # f_h es el array resultante de aplicar f a cada elmento del array x
f_h[0]  += ua/h**2
f_h[-1] += ub/h**2
f_h 
```




    array([  0.        ,   0.4284597 ,   0.84390088,   1.23370055,
             1.58601486,   1.8901389 ,   2.13683203,   2.31859861,
             2.42991573, 123.4674011 ])



### Resolver sistema


```python
from numpy.linalg import solve
u_h = solve(A_h, f_h)

from matplotlib.pylab import plot, show, grid, legend
plot(x, u_h, label="Solución aproximada", linewidth=3, color="green")
grid()
legend()
show()

```

## Papel sucio....


```python

```


```python
# Trabajando...
size = 24
print("El tamaño es %i" %size)

# Mucho más abajo:
from numpy import *
x=array([1,2,3])
size(x) # No me he dado cuenta de que el import ha pisado la variable size

# De nuevo más abajo
print("El tamaño es %i", size)


```

    El tamaño es 24
    El tamaño es %i <function size at 0x0000000004F2A048>



```python
import numpy as np
x = np.array([1,2,3])
y = np.sin(x)
y
```




    array([0.84147098, 0.90929743, 0.14112001])




```python
2*identity(3)
```




    array([[2., 0., 0.],
           [0., 2., 0.],
           [0., 0., 2.]])




```python

```


```python
ones(3)
```




    array([1., 1., 1.])




```python
diag([1,3,88],-2)
```




    array([[ 0,  0,  0,  0,  0],
           [ 0,  0,  0,  0,  0],
           [ 1,  0,  0,  0,  0],
           [ 0,  3,  0,  0,  0],
           [ 0,  0, 88,  0,  0]])




```python
x = linspace(0,1, n) # x_0, ..., x_{n-1}
x[9]
```




    1.0




```python
lista = [1,2,3,4,5,6]
lista[:5:2]
```




    [1, 3, 5]




```python
%pylab
```

    Using matplotlib backend: Qt5Agg
    Populating the interactive namespace from numpy and matplotlib


    C:\Users\Usuario\Anaconda3\lib\site-packages\IPython\core\magics\pylab.py:160: UserWarning: pylab import has clobbered these variables: ['f']
    `%matplotlib` prevents importing * from pylab and numpy
      "\n`%matplotlib` prevents importing * from pylab and numpy"



```python
plo
```

42554788e47612a6d9e3e78ce6dac0450a4c9c36
ipynb
Jupyter Notebook
diferencias-finitas/Laplace MDF 1D.ipynb
rrgalvan/PythonIntroMasterMatemat
a8b22f2b8146f5be0350f8aea7f4d524e34df3a7
CC0-1.0
diferencias-finitas/Laplace MDF 1D.ipynb
rrgalvan/PythonIntroMasterMatemat
a8b22f2b8146f5be0350f8aea7f4d524e34df3a7
CC0-1.0
diferencias-finitas/Laplace MDF 1D.ipynb
rrgalvan/PythonIntroMasterMatemat
a8b22f2b8146f5be0350f8aea7f4d524e34df3a7
CC0-1.0
Qwen/Qwen-72B
1. YES
2. YES