In [1]:
import adaptive_trapezint_new as p1
import sinesum1_new as p2
import centered_diff_new as p3
import find_primes_new as p4

Homework 1

Andrew Malfavon

note: file name changes are due to sage being weird while trying to upload.

Exercise 3.8 $\mathit{Make\ an\ adaptive\ trapezoidal\ rule.}$ (adaptive_trapezint.py)

Improves on the trapezoidal integration rule by creating a method to determine an apropriate $n$ trapezoids. The table shows the error of the estimation and the calculated $n$ for three different cases.


In [2]:
p1.table()


    Function                    Error            Estimated n
cos(x) from 0 to pi        1.70002900646e-16        508
sin(x) from 0 to pi        6.37413631988e-06        508
sin(x) from 0 to pi/2      6.41730976758e-06        179

Exercise 3.15 $\mathit{Approximate\ a\ function\ by\ a\ sum\ of\ sines.}$ (sinesum1.py)

Consider the following piecwise function:

$ f(t) = \left\{\def\arraystretch{1.2}% \begin{array}{@{}c@{\quad}l@{}} 1 & 0<t<\frac{T}{2}\\ 0 & t=\frac{T}{2}\\ -1 & \frac{T}{2}<t<T\\ \end{array}\right. $

Approximate $f(t)$ by the sum $$S(t,n)= \frac{4}{\pi} \sum_{i=1}^{n} \frac{1}{2i-1} sin{\frac{2(2i-1)\pi t}{T}}$$

Display the error $f(t)-S(t,n)$ for various $n$ and $\alpha$, given that $t=\alpha T$ and $T=2 \pi$. Use $n=1,3,5,10,30,100$ and $\alpha = 0.01, 0.25, 0.49$.


In [3]:
p2.table()


                alpha = 0.01        alpha = 0.25            alpha = 0.49    
n = 1:        0.920052627501        -0.273239544735        0.920052627501
n = 3:        0.761834996162        -0.103474272104        0.761834996162
n = 5:        0.608585435311        -0.0630539690963        0.608585435311
n = 10:        0.266797434877        0.0317523771092        0.266797434877
n = 30:        -0.14481610778        0.0106073863054        -0.14481610778
n = 100:        0.050094008341        0.00318301929431        0.050094008341

Exercise 3.18 $\mathit{Write\ a\ function\ for\ numerical\ differentiation.}$ (centered_dif.py)

The formula $$f'(x) \approx \frac{f(x+h)-f(x-h)}{2h}$$ approximates a derivative a function $f(x)$ if $h$ is small. Use $h=0.01$ and show the error for the approximations of the following functions:

$$f(x) = e^{x}\ \textrm{at}\ x=0$$$$f(x) = e^{-2x^{2}}\ \textrm{at}\ x=0$$$$f(x) = cos(x)\ \textrm{at}\ x=2\pi$$$$f(x) = \ln x\ \textrm{at}\ x=1$$

In [4]:
p3.table()


Function                    Error
exp(x) at x=0            -1.66667499921e-05
exp(-2x^2) at x=0            0.0
cos(x) at x=2*pi            0.0
ln(x) at x=1            -3.33353334772e-05

Exercise 3.34 $\mathit{Find\ prime\ numbers.}$ (find_primes.py)

Use the $\mathit{Sieve\ of\ Eratosthenes}$ to find prime numbers less than or equal to a number $N$.


In [5]:
print p4.find_primes(10)


[2, 3, 5, 7]

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


[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

In [7]:
print p4.find_primes(100)


[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]

In [ ]: