In [1]:
import numpy as np
import sympy as sp
import adaptive_trapezint as p1
import sinesum1 as p2
import centered_diff as p3
import find_primes as p4
Using the trapazoid approximation to find areas under the curve, we can get a good guess at bounded integration, however, we can find how many trapazoids we want to use by specifying a maximum error $\epsilon$ and then using the following equation:
$n = (b-a)\sqrt{\frac{(b-a) max\left|f''(x)\right|}{12\epsilon}}$
In this example, "some_function" is $f(x) = x^{4} + x^{2}$ evaluated from 0 to 20.
Actual result? $$\int_{0}^{20} (x^4 + x^2) dx = 642667 $$
In [5]:
def some_function(x):
return x**4 + x**2
print(p1.adaptive_trapezint(some_function, 0, 20))
The function we're trying to model is a piecewise function following:
$f(t) = \left\{ \begin{array}{ll} 1 & 0<t<T/2 \\ 0 & t = T/2 \\ -1 & T/2<t<T \\ \end{array} \right.$
We're trying to approximate it with the sinosoidal Fourier approximation: $S(t;n) = \frac{4}{\pi}\sum_{i=0}^{n}\frac{1}{2i-1}sin(\frac{2(2i+1)\pi t}{T})$
We will see how accurate S is at approximating f by trying different vaules of n (the number of sinewaves used to approximate) and with different vaues for $\alpha$ where $\alpha=\frac{t}{T}$.
In [2]:
p2.print_error_results()
The "diff" function follows the formula:
$f'(x) \approx \frac{f(x+h) - f(x-h)}{2h}$
in order to approximate the derivative of f at x using a very small value for h.
Below we use the functions:
$f_{1}(x) = e^{x}$ at $x=0$
$f_{2}(x) = e^{-2x^{2}}$ at $x=0$
$f_{3}(x) = cos{x}$ at $x=2\pi$
$f_{4}(x) = ln{x}$ at $x=1$
and calculate the difference of our differentiating function's output with h = .01 with the actual result.
In [4]:
p3.application()
In [4]:
p4.find_primes(10)
Out[4]:
In [6]:
print(p4.find_primes(500))
Out[6]: