# Sine Series Problem: Error Analysis (Student notebook)

* *Computational Physics*: Ch 2.5
* In-class Activity


## Problem: Summing Series: sin(x)

Evaluate the $\sin$ function from its series representation
$$
\sin x = x - \frac{x^3}{3!} + \frac{x^5}{5!} - \frac{x^7}{7!} + \dots
$$

### Explicit sum
A naive algorithm is to sum the series up to the $N$th term:
$$
\sin x \approx \sum_{n=1}^N \frac{(-1)^{n-1} x^{2n-1}}{(2n - 1)!}
$$

Problems:

- How to decide when to stop summing?
- Division of large terms (overflows!)
- Powers and factorials are very expensive to compute.

### Recursive sum
Better approach: Build up series terms $a_n$ using previous term $a_{n-1}$ through a recursion:

\begin{align}
a_n &= a_{n-1} \times q_n\\
a_n &= \frac{(-1)^{n-1} x^{2n-1}}{(2n - 1)!} = \frac{(-1)^{n-2} x^{2n-3}}{(2n - 3)!} \frac{-x^2}{(2n - 1)(2n - 2)}\\
a_n & = a_{n-1} \frac{-x^2}{(2n - 1)(2n - 2)}
\end{align}

Accuracy of this approach? Not clear in absolute terms but we can make the assumption that the error is approximately the last term in the sum, $a_N$. Hence we can strive to make the relative error smaller than the machine precision
$$
\left| \frac{a_N}{\sum_{n=1}^N a_n} \right| < \epsilon_m
$$



## Task

In file `series.py` implement a function `sin_recursive(x, N=100)` that 
* computes $\sin(x)$ using the **Recursive sum** approach using $N$ iterations
* returns the value of $\sin(x)$ 



## Error analysis


```python
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
```

Import your `series.py` as a module:


```python
import series
```

NOTE: If you changed code in `series` you will need to **reload the module**: execute the following cell whenever you changed `series.py`.


```python
from importlib import reload
reload(series)
```




    <module 'series' from '/Volumes/ASU/oliver/Documents/Teaching/ASU/CompPhys_PHY494/2021/PHY494-resources/08_errors/erroranalysis/series.py'>



Test the implementation against the "exact" numpy function `np.sin()` and show the relative error as a function of $1 \le N < 1000$.

Report and plot
1. Try out `x =` $\pi/3, 2\pi, 7.89\pi, -12.3\pi, 14.78\pi$
2. relative error `abs(sin_series(x) - sin(x))/abs(sin(x))` as function of $N$

(Use a log-log plot.)

#### Testing code

Implementation of the error calculation against the numpy reference implementation `np.sin`:


```python
def relerror_sin(N_values, x):
    # TODO
    pass
```

#### Analyze recursive implementation

Plot for $x = \pi/3$ and for $x = -7.89\pi$


```python

```

Plot all x values:


```python

```


```python

```

41c0ca27d08a28f4d2aaca7a96878e0e435bf319
ipynb
Jupyter Notebook
08_errors/erroranalysis/sine-series-erroranalysis-students.ipynb
Py4Phy/PHY432-resources
c26d95eaf5c28e25da682a61190e12ad6758a938
CC-BY-4.0
08_errors/erroranalysis/sine-series-erroranalysis-students.ipynb
Py4Phy/PHY432-resources
c26d95eaf5c28e25da682a61190e12ad6758a938
CC-BY-4.0
2022-03-03T21:47:56.000Z
2022-03-03T21:47:56.000Z
08_errors/erroranalysis/sine-series-erroranalysis-students.ipynb
Py4Phy/PHY432-resources
c26d95eaf5c28e25da682a61190e12ad6758a938
CC-BY-4.0
Qwen/Qwen-72B
1. YES
2. YES