# Prova II- Computação Centífica II
> Autor: Gil Miranda<br>
> Contato: gil.neto@ufrj.br; gilsmneto@gmail.com

## Set-up of dependencies and libraries


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

## The equation to solve

$$
\epsilon \frac{\mathrm{d}^2u}{\mathrm{d}x^2} + 2(1-x^2)u + u^2 = 1
$$

$$
\frac{\mathrm{d}^2u}{\mathrm{d}x^2} = \frac{1 - 2(1-x^2)u - u^2}{\epsilon}
$$

Tomando $\frac{\mathrm{d}^2u}{\mathrm{d}x^2} = f(x)$, e uma solução como $u(x) \approx u_h(x) = \displaystyle\sum^N_{j = 1} c_j \varphi_j(x)$, podemos reescrever o problema em forma de um sistema
$$
A\vec{c} = \vec{f}
$$

Onde
$$
f_i = \int^1_{-1}f(x)\varphi_i \mathrm{d}x
$$
$$
A_{ij} = \int^1_{-1}\frac{\mathrm{d}\varphi_j}{\mathrm{d}x}\frac{\mathrm{d}\varphi_i}{\mathrm{d}x} \mathrm{d}x
$$
e $c_j$ podemos descobrir resolvendo o sistema, daí podemos utilizar a solução aproximada $u_h(x)$

### Calculando a matriz A

Vamos calcular primeiro os termos da matriz A, para isso resolvemos a integral acima, temos:
$$
\varphi_j = sen(j\pi x)\\
\frac{\mathrm{d}\varphi_j}{\mathrm{d}x} = j\pi cos(j \pi x)
$$
Então a integral a ser resolvida é:
$$
\int^1_{-1}(j\pi cos(j \pi x))(i\pi cos(i \pi x)) \mathrm{d}x
$$
E utilizando a identidade: $cos(a)cos(b) = \frac{1}{2}(cos(a+b)+cos(a-b))$, podemos resolver a integral
$$
\begin{align}
\int^1_{-1}\frac{\pi^2 ij}{2} cos(\pi x(j+i)) + cos(\pi x(j-i)) \mathrm{d}x &= \frac{\pi^2 ij}{2} \left(\int^1_{-1} cos(\pi x(j+i))\mathrm{d}x + \int^1_{-1}cos(\pi x(j-i)) \mathrm{d}x \right)\\
&= \frac{\pi^2 ij}{2} \left(\frac{sen(\pi x(j+i))}{\pi(i+j)} + \frac{sen(\pi x(j-i))}{\pi(j-i)}\right)\Big|_{-1}^{1}\\
&= \frac{\pi^2 ij}{2} \left(\frac{sen(\pi(j+i))}{\pi(i+j)} + \frac{sen(\pi(j-i))}{\pi(j-i)} - \frac{sen(-\pi(j+i))}{\pi(i+j)} - \frac{sen(-\pi(j-i))}{\pi(j-i)}\right)\\
&\text{Como $j, i$ são números inteiros, temos que sua soma também é, e como $sen(n\pi) = 0, n \in \mathbb{N}$}\\
&= 0
\end{align}
$$
Portanto a integral é $0$, mas temos uma restrição em $i,j$ por conta do termo $(j-i)$ sendo dividido não podemos ter $j = i$ nesta integral, logo só é válida para $i \neq j$, portanto
$$A_{ij} = 0, \, \forall i \neq j$$
Olhando para o caso $i = j$, temos:
$$
\int^1_{-1}(j\pi cos(j \pi x))(i\pi cos(i \pi x)) \mathrm{d}x = \int^1_{-1}j^2\pi^2 cos^2(j \pi x) \mathrm{d}x
$$
E usando a identidade $cos^2(x) =  \frac{1}{2}(1 + cos(2x)$
$$
\begin{align}
\int^1_{-1}j^2\pi^2 cos^2(j \pi x) \mathrm{d}x &= \int^1_{-1}\frac{j^2\pi^2}{2} (1 + cos(2j \pi x) ) \mathrm{d}x\\
&= \frac{j^2\pi^2}{2}\int^1_{-1} (1 + cos(2j \pi x) ) \mathrm{d}x\\
&= \frac{j^2\pi^2}{2}\left(\int^1_{-1} 1 \mathrm{d}x + \int^1_{-1} cos(2j \pi x) ) \mathrm{d}x\right)\\
&= \frac{j^2\pi^2}{2}\left(x\Big|_{-1}^{1} + \frac{sen(2j \pi x)}{2\pi j}\Big|_{-1}^{1}\right)\\
&= j^2\pi^2
\end{align}
$$
Logo, para os elementos de $A$ com $i = j$, temos $A_{ij} = j^2 \pi^2$

### Defining necessaries


```python
N = 5
xs = np.linspace(-1,1,N)
def base(j, x):
    return np.sin(j*np.pi*x)
```


```python
## Defining the Integrals of the basis function

def F1(j,i,x):
    a = (np.pi*i*j/2)
    b = np.sin(np.pi*x*(i+j))/(i+j)
    c = np.sin(np.pi*x*(j-i))/(j-i)
    return a*(b+c)

def F2(j):
    #a = np.sin(2*np.pi*j*x)/np.pi*j
    return (j**2)*(np.pi**2)
```


```python
def F3(i,x):
    return (i**2)*(np.pi**2)
```


```python
A = np.zeros((N,N))
```


```python
for i in range(N):
    for j in range(N):
        if i == j:
            A[i][j] = F2(i+1)
        else:
            A[i][j] = F1(j+1,i+1,1) - F1(j+1,i+1, -1)
```


```python
A
```


```python

```

718f45c98aac95469ac04ec713b655fd779ee878
ipynb
Jupyter Notebook
comp-cientifica-II-2019-2/.ipynb_checkpoints/Prova2_GilMiranda-Copy1-checkpoint.ipynb
mirandagil/university-courses
e70ce5262555e84cffb13e53e139e7eec21e8907
MIT
2019-12-23T16:39:01.000Z
2019-12-23T16:39:01.000Z
comp-cientifica-II-2019-2/.ipynb_checkpoints/Prova2_GilMiranda-Copy1-checkpoint.ipynb
mirandagil/university-courses
e70ce5262555e84cffb13e53e139e7eec21e8907
MIT
comp-cientifica-II-2019-2/.ipynb_checkpoints/Prova2_GilMiranda-Copy1-checkpoint.ipynb
mirandagil/university-courses
e70ce5262555e84cffb13e53e139e7eec21e8907
MIT
Qwen/Qwen-72B
1. YES
2. YES