```python
import sympy as sp
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
```


```python
x = sp.symbols('x')
f = sp.sin(x) / sp.sqrt(1 - x)
start, end = 0, 1
```


```python
f
```




$\displaystyle \frac{\sin{\left(x \right)}}{\sqrt{1 - x}}$



We will be integrating the following function from 0 to 1

# Let's plot it first!


```python
x_plot = np.linspace(start, end, 300, endpoint=False)
y_plot = sp.lambdify(x, f, 'numpy')(x_plot)

sns.set_style('whitegrid')
plt.figure(figsize=(12, 6))
sns.lineplot(x_plot, y_plot);
```

# Exact value


```python
true_value = 1.18698444
# Thanks, Wolfram Alpha!
```

# Midpoint Riemann sum


```python
nodes_count = 3
nodes = np.linspace(start, end, nodes_count, endpoint=False)
step = (nodes[1] - nodes[0])
nodes += step / 2

values = sp.lambdify(x, f, 'numpy')(nodes)
mid_riemann_value = step * values.sum()
```


```python
mid_riemann_value
```




    0.8909319389164732



# Using weights


```python
p = 1 / sp.sqrt(1 - x)
nodes = [sp.Rational(1, 6), 0.5, sp.Rational(5, 6)]
phi = f / p
w = (x - nodes[0]) * (x - nodes[1]) * (x - nodes[2])
dw = w.diff()
```


```python
coeffs = [
    11 / 20,
    -1 / 10,
    31 / 20
]
coeffs
```




    [0.55, -0.1, 1.55]




```python
weights_value = sum([coeffs[i] * phi.evalf(subs={x: nodes[i]}) for i in range(len(nodes))])
weights_value
```




$\displaystyle 1.19057444157482$



# Gauss time!



```python
roots = [-1 / sp.sqrt(3), 1 / sp.sqrt(3)]
coeffs = [1, 1]
nodes = [(start + end + (end - start) * r) / 2 for r in roots]
```


```python
gauss_value = sum([coeffs[i] * f.evalf(subs={x: nodes[i]}) for i in range(len(nodes))]) * (end - start) / 2
```


```python
gauss_value
```




$\displaystyle 0.889706408692229$



# Gauss-like formulas


```python
p = 1 / sp.sqrt(1 - x)
nodes_count = 2
mus = [
    float(
        sp.integrate(
            p * x ** k,
            (x, 0, 1)
        )
    )
    for k in range(2 * nodes_count)
]
for i in range(2 * nodes_count):
    print(f'mu_{i} = {mus[i]}')
```

    mu_0 = 2.0
    mu_1 = 1.3333333333333333
    mu_2 = 1.0666666666666667
    mu_3 = 0.9142857142857143



```python
# Huge thanks to Wolfram Alpha (again)!
poly_coeffs = [-8 / 7, 8 / 35]
polynom = x**2 + x * poly_coeffs[0] + poly_coeffs[1]
```


```python
nodes = sp.solve(polynom)
```


```python
phi = f / p
```


```python
coeffs = [
    (mus[1] - mus[0] * nodes[1]) / (nodes[0] - nodes[1]),
    (mus[1] - mus[0] * nodes[0]) / (nodes[1] - nodes[0])
]
```


```python
gauss_like_value = sum([coeffs[i] * phi.evalf(subs={x: nodes[i]}) for i in range(nodes_count)])
```


```python
gauss_like_value
```




$\displaystyle 1.18673193986058$



8182ffb7ffa32740ec9f0af96f266eecab3170a7
ipynb
Jupyter Notebook
year-2/computational-workshop/task-6.ipynb
Sergobot/university
7cd8c07fc660f1e19127c6488991ddd59d99643c
MIT
2019-09-05T08:43:47.000Z
2019-09-05T08:43:52.000Z
year-2/computational-workshop/task-6.ipynb
Sergobot/university
7cd8c07fc660f1e19127c6488991ddd59d99643c
MIT
year-2/computational-workshop/task-6.ipynb
Sergobot/university
7cd8c07fc660f1e19127c6488991ddd59d99643c
MIT
2020-10-04T07:40:20.000Z
2021-09-18T07:12:08.000Z
Qwen/Qwen-72B
1. YES
2. YES