# Introduction
In the falling parachutist problem we encountered an error - there was a difference between the numerical calculation and the analytical solution. There are numerous types of errors associated with an engineering mathematical model:
1. Modeling errors: These have to do with the fact that we can never describe reality exactely and/or our model neglects certain effects (e.g. sidewind in the parachutist example).
2. Numerical errors: These have to do directly with the numerical method used and can be generally categorized as
    * Round-off errors: due to the limited representation of numbers on computers
    * Truncation error: due to approximating exact mathematical operators

## Errors
The basic definition of an error is
\begin{equation}
\text{True Error = True Value - Approximation}
\end{equation}

For simplicity, denote the "True Error" as $E_\text{t}$.
There is a problem with this definition however - it does not take into account the "magnitude" of the true value. For example, an error of one centimeter is much more significant if we are measuring a rivet rather than a bridge. To fix this, we define the true relative error, $\epsilon_\text{t}$:
\begin{equation}
\text{True Relative Error} = \frac{\text{True Error}}{\text{True Value}}
\end{equation}
or
\begin{equation}
\epsilon_\text{t} = \frac{E_\text{t}}{\text{True Value}}
\end{equation}

### Example
Suppose that you have the task of measuring the lengths of a bridge and a rivet and come up with 9999 and 9 cm, respectively. If the true values are 10,000 and 10 cm, respectively, compute (a) the true error and (b) the true percent relative error for each case.

#### Solution
For the bridge, the True Error is:
\begin{equation}
E_\text{t} = 10,000 - 9999 = 1 \text{cm}
\end{equation}
while for the rivet
\begin{equation}
E_\text{t} = 10 - 9 = 1 \text{cm}
\end{equation}
Both measurements have the same true error. But we known intuitively that the 1 cm is a bigger deal for the rivet. We can show this by using the relative error.
For the bridge, we have
\begin{equation}
\epsilon_\text{t} = \frac{E_\text{t}}{\text{True Value}} \times 100\%= \frac{1}{10,000} \times 100\% = 0.01 \%
\end{equation}
while for the rivet,
\begin{equation}
\epsilon_\text{t} = \frac{1}{10}\times 100\% = 10 \%
\end{equation}

Note that we used the words "True Error" - this is because the definition is based on the "True Value" - but this doesn't make sense since if we know the true value then we don't need an approximation! 

This definition is still important however because it lays the foundations of error analysis. In addition, when testing any numerical method, we first try it out on problems where the True Value is known (e.g. analytical solution). What we often do is figure out an approximation for the true value or find the best estimate of the true value.

Another common situation in numerical methods is iteration: repeating the same task over and over again. What we can do in that case is define an iterative error as the difference between current and previous iterations:
\begin{equation}
\epsilon_\text{a} = \frac{ \text{current approximation} - \text{previous approximation} }{ \text{current approximation} }\times 100\%
\end{equation}

The usefulness of this definition is that it allows us to get an idea of how good our approximation is. Specifically, one can use a well known formula to determine the number of significant digits in an error. If your computed error $|\epsilon_\text{a}|$ (in \%) is less than
\begin{equation}
\epsilon_\text{s} = (0.5\times 10^{2-n})\%
\end{equation}
then your results are accurate to at least $n$ significant digits.

For example, if you want your results to be significant up to at least four digits, ($n = 4$) then your results must satify $ |\epsilon_\text{a}| < 0.5\times10^{-2}\%$ or $ |\epsilon_\text{a}| < 0.005\%$.

This formula can be inverted to obtain the number of significant digits. Taking the logarigthm
\begin{equation}
\log \epsilon_\text{s} = \log 0.5 + 2 - n
\end{equation}
and
\begin{equation}
n = \log 0.5 + 2 - \log \epsilon_\text{s}
\end{equation}

## Example
The exponential function $e^x$ can be approximated with the following (Taylor) series:
\begin{equation}
e^x = 1 + x + \frac{x^2}{2} + \frac{x^3}{3!} + \frac{x^4}{4!} + \frac{x^5}{5!} + \ldots
\end{equation}
Compute the true and approximate relative errors when using this formula for 1, 2, 3, 4, 5, and 6 terms. Use this formula to compute $e^{0.5}$ up to four significant digits

### Solution
We will first need to define a function that computes the exponential up to a certain number of terms


```python
# need to import the factorial and exponential functions from the math module
import math
from math import factorial, exp
def my_exponential(x,n):
    '''x is the exponent and n is the number of terms'''
    result = 0.0 # initialize the result to zero
    for i in range(0,n): # loop over the number of terms: this goes from 0 to n-1 (n terms)
        result = result + x**i/factorial(i) # add each term at a time        
    return result
```


```python
def sig_fig(error):
    return math.floor(math.log10(0.5) + 2 - math.log10(error))
```


```python
# Now run the function for different number of terms
oldval = 0.0
x = 0.5
trueval = exp(x)
for n in range(1,10):
    val = my_exponential(x,n)
    ϵt = abs( (val - trueval)/trueval ) * 100
    ϵa = abs( (val - oldval)/val ) * 100
    oldval = val
    print (n,'\t', "%.8f" % val, '\t', "%.8f" % ϵt, '\t', "%.8f" % ϵa, '\t', sig_fig(ϵa))
```

    1 	 1.00000000 	 39.34693403 	 100.00000000 	 -1
    2 	 1.50000000 	 9.02040104 	 33.33333333 	 0
    3 	 1.62500000 	 1.43876780 	 7.69230769 	 0
    4 	 1.64583333 	 0.17516226 	 1.26582278 	 1
    5 	 1.64843750 	 0.01721156 	 0.15797788 	 2
    6 	 1.64869792 	 0.00141649 	 0.01579529 	 3
    7 	 1.64871962 	 0.00010024 	 0.00131626 	 4
    8 	 1.64872117 	 0.00000622 	 0.00009402 	 5
    9 	 1.64872127 	 0.00000034 	 0.00000588 	 6



```python
from math import factorial, exp
oldval = 0.0
x = 2.0
nterms = 10
for n in range(0,nterms):
    newval = oldval + x**n/ factorial(n)
    ϵa = abs( (newval - oldval)/newval) * 100
    oldval = newval
    print(n, ϵa,'%')
```

    0 100.0 %
    1 66.66666666666666 %
    2 40.0 %
    3 21.052631578947363 %
    4 9.523809523809527 %
    5 3.669724770642201 %
    6 1.2084592145015065 %
    7 0.34408602150537515 %
    8 0.08594757198109124 %
    9 0.019095813242941077 %



```python
from math import factorial, exp
oldval = 0.0
x = 2.0
nterms = 100
tol = 0.01 # percent
ϵa = 10000
n = 0
while ϵa > tol:
    newval = oldval + x**n/ factorial(n)
    ϵa = abs( (newval - oldval)/newval) * 100
    oldval = newval
    n = n + 1
    print(n, ϵa,'%')
```

    1 100.0 %
    2 66.66666666666666 %
    3 40.0 %
    4 21.052631578947363 %
    5 9.523809523809527 %
    6 3.669724770642201 %
    7 1.2084592145015065 %
    8 0.34408602150537515 %
    9 0.08594757198109124 %
    10 0.019095813242941077 %
    11 0.0038190167941204627 %



```python
def mycos(x, nterms):
    result = 0.0
    for n in range(0,nterms):
        result += (-1)**n * x**(2*n) / factorial(2*n)
    return result
```


```python
print(mycos(3, 5), math.cos(3))
```


```python

```

477392921105b113d423bfc431d57c72563fe88f
ipynb
Jupyter Notebook
misc/03. Errors.ipynb
jomorodi/NumericalMethods
e040693001941079b2e0acc12e0c3ee5c917671c
MIT
2019-03-27T05:22:34.000Z
2021-01-27T10:49:13.000Z
misc/03. Errors.ipynb
jomorodi/NumericalMethods
e040693001941079b2e0acc12e0c3ee5c917671c
MIT
misc/03. Errors.ipynb
jomorodi/NumericalMethods
e040693001941079b2e0acc12e0c3ee5c917671c
MIT
2019-12-29T23:31:56.000Z
2021-12-28T19:04:10.000Z
Qwen/Qwen-72B
1. YES
2. YES