In [28]:
%matplotlib inline
import numpy
import matplotlib.pyplot as plt
Errors in fundamental formulation
Unfortunatley we cannot control model and data error directly but we can use methods that may be more robust in the presense of these types of errors.
Given a true value of a function $f$ and an approximate solution $\hat{f}$ define:
Absolute Error: $e = |f - \hat{f}|$
Relative Error: $r = \frac{e}{|f|} = \frac{|f - \hat{f}|}{|f|}$
Decimal precision p (number of significant digits): $\text{round}(10^{-n} \cdot x) \cdot 10^n$
with $n = \text{floor}(\log_{10} x) + 1 - p$.
In [10]:
f = numpy.exp(1)
f_hat = 2.71
# Error
print "Absolute Error = ", numpy.abs(f - f_hat)
print "Relative Error = ", numpy.abs(f - f_hat) / numpy.abs(f)
# Precision
p = 3
n = numpy.floor(numpy.log10(f_hat)) + 1 - p
print "%s = %s" % (f_hat, numpy.round(10**(-n) * f_hat) * 10**n)
Taylor's Theorem: Let $f(x) \in C^{m+1}[a,b]$ and $x_0 \in [a,b]$, then for all $x \in (a,b)$ there exists a number $c = c(x)$ that lies between $x_0$ and $x$ such that
$$ f(x) = T_N(x) + R_N(x)$$where $T_N(x)$ is the Taylor polynomial approximation
$$T_N(x) = \sum^N_{n=0} \frac{f^{(n)}(x_0)\cdot(x-x_0)^n}{n!}$$and $R_N(x)$ is the residual (the part of the series we left off)
$$R_N(x) = \frac{f^{(n+1)}(c) \cdot (x - x_0)^{n+1}}{(n+1)!}$$
In [19]:
import sympy
x = sympy.symbols('x')
f = sympy.symbols('f', cls=Function)
f = exp(x)
f.series(x0=0, n=3)
Out[19]:
Using this we can find expressions for the relative and absolute error as a function of $x$ assuming $N=2$.
$$T_N(x) = \sum^N_n=0 \frac{x^n}{n!} = 1 + x + \frac{x^2}{2}$$$$R_N(x) = e^c \frac{x^{n+1}}{(n+1)!} = e^c \cdot \frac{x^3}{6}$$$$e^1 = 2.718\ldots$$$$T_2(1) = 2.5 \Rightarrow e \approx 0.2 ~~ r \approx 0.1$$Lets do this numerically for a section of $x$.
In [33]:
x = numpy.linspace(-1, 1, 100)
T_N = 1.0 + x + x**2 / 2.0
R_N = numpy.exp(1) * x**3 / 6.0
plt.plot(x, T_N, 'r', x, numpy.exp(x), 'k', x, R_N, 'b')
plt.xlabel("x")
plt.ylabel("$f(x)$, $T_N(x)$, $R_N(x)$")
plt.legend(["$f(x)$", "$T_N(x)$", "$R_N(x)$"], loc=2)
plt.show()