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

Homework 2

Michael Seaman

2/12/16

Problem 3.8: Adaptive Trapazoid Approximation

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))


642665.509138

Problem 3.15: Fourier series Approximation

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()


alphas:    0.01    0.25    0.49
n: 1      0.92    -0.273    0.92
n: 3      0.762    -0.103    0.762
n: 5      0.609    -0.0631    0.609
n: 10      0.267    0.0318    0.267
n: 30      -0.145    0.0106    -0.145
n: 100      0.0501    0.00318    0.0501

Problem 3.18 Numerical differentiation

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()


Result: 1.00001666675, answer: 1, difference -1.66667499921e-05
Result: 0.0, answer: 0, difference 0.0
Result: 0.0, answer: 0, difference 0.0
Result: 1.00003333533, answer: 1, difference -3.33353334772e-05

3.34 Finding Prime numbers

This application uses the sieve of Eratosthenes algorithm to find pirme numbers less than or equal to the input. The algorithm checks off multiples of the lowest candidate that is still prime and removes them from the candidates; then moving up to the next.


In [4]:
p4.find_primes(10)


Out[4]:
[2, 3, 5, 7]

In [6]:
print(p4.find_primes(500))


Out[6]:
[2,
 3,
 5,
 7,
 11,
 13,
 17,
 19,
 23,
 29,
 31,
 37,
 41,
 43,
 47,
 53,
 59,
 61,
 67,
 71,
 73,
 79,
 83,
 89,
 97,
 101,
 103,
 107,
 109,
 113,
 127,
 131,
 137,
 139,
 149,
 151,
 157,
 163,
 167,
 173,
 179,
 181,
 191,
 193,
 197,
 199,
 211,
 223,
 227,
 229,
 233,
 239,
 241,
 251,
 257,
 263,
 269,
 271,
 277,
 281,
 283,
 293,
 307,
 311,
 313,
 317,
 331,
 337,
 347,
 349,
 353,
 359,
 367,
 373,
 379,
 383,
 389,
 397,
 401,
 409,
 419,
 421,
 431,
 433,
 439,
 443,
 449,
 457,
 461,
 463,
 467,
 479,
 487,
 491,
 499]