In [5]:
import numpy as np
import sympy as sp
import matplotlib.pyplot as plt
%matplotlib inline

import pi_sequences as p3
import boundary_layer_func1 as p1
import sequence_limits as p2
import diffeq_midpoint as p4


import math

Classwork 3

Michael Seaman, Chinmai Raman, Austin Ayers, Taylor Patti

Organized by Andrew Malfavon

Excercise A.2: Computing $\pi$ via sequences

Michael Seaman

The following sequences all converge to pi, although at different rates.

In order: $$a_n = 4\sum_{k=1}^{n}\frac{(-1)^{k+1}}{2k-1}$$

$$b_n = (6\sum_{k=1}^{n}k^{-2})^{1/2} $$$$c_n = (90\sum_{k=1}^{n}k^{-4})^{1/4} $$$$d_n = \frac{6}{\sqrt{3}}\sum_{k=0}^{n}\frac{(-1)^{k}}{3^k(2k+1)}$$$$e_n = 16\sum_{k=0}^{n}\frac{(-1)^{k}}{5^{2k+1}(2k+1)} - 4\sum_{k=0}^{n}\frac{(-1)^{k}}{239^{2k+1}(2k+1)}$$

In [5]:
n = 30
plt.plot([x for x in range(n)],p3.pi_sequence(n, p3.fa),'g.')
plt.show()



In [6]:
plt.plot([x for x in range(n)],p3.pi_sequence(n, p3.fb) ** .5 ,'b.')
plt.show()



In [7]:
plt.plot([x for x in range(n)],p3.pi_sequence(n, p3.fc) ** .25 ,'y.')
plt.show()



In [8]:
plt.plot([x for x in range(n)],p3.pi_sequence(n, p3.fd),'r.')
plt.show()



In [9]:
plt.plot([x for x in range(n)],p3.pi_sequence(n, p3.fd),'c.')
plt.show()



In [10]:
n = 10
plt.plot([x + 20 for x in range(n)],p3.pi_sequence(n + 20, p3.fa)[-n:],'g.')
plt.plot([x + 20 for x in range(n)],(p3.pi_sequence(n + 20, p3.fb) ** .5)[-n:] ,'b.')
plt.plot([x + 20 for x in range(n)],(p3.pi_sequence(n + 20, p3.fc) ** .25)[-n:] ,'y.')
plt.plot([x + 20 for x in range(n)],p3.pi_sequence(n + 20, p3.fd)[-n:],'r.')
plt.plot([x + 20 for x in range(n)],p3.pi_sequence(n + 20, p3.fd)[-n:],'c.')

plt.plot((20, 30), (math.pi, math.pi), 'b')

plt.show()


Chinmai Raman

Classwork 3

5.49 Experience Overflow in a Function

Calculates an exponential function and returns the numerator, denominator and the fraction as a 3-tuple


In [11]:
x = np.linspace(0,1,10000)
y1 = p1.v(x, 1, np.exp)[2]
y2 = p1.v(x, 0.1, np.exp)[2]
y3 = p1.v(x, 0.01, np.exp)[2]

fig = plt.figure(1)
plt.plot(x, y1, 'b')
plt.plot(x, y2, 'r')
plt.plot(x, y3, 'g')
plt.xlabel('x')
plt.ylabel('v(x)')
plt.legend(['(1 - exp(x / mu)) / (1 - exp(1 / mu))'])
plt.axis([x[0], x[-1], min(y3), max(y3)])
plt.title('Math Function')
plt.show(fig)


Austin Ayers

Classwork 3

A.1 Determine the limit of a sequence

Computes and returns the following sequence for N = 100

$$a_n = \frac{7+1/(n+1)}{3-1/(n+1)^2}, \qquad n=0,1,2,\ldots,N$$

In [2]:
p2.part_a()


4.0
2.53846153846
2.43243243243
2.39726027397
2.38016528926
2.37016574586
2.36363636364
2.3590504451
2.35565819861
2.35304990758
2.35098335855
2.34930643127
2.34791889007
2.34675205855
2.34575733545
2.34489937543
2.34415186773
2.34349482852
2.34291281052
2.34239368698
2.34192780643
2.34150739272
2.34112611129
2.34077875038
2.34046098306
2.34016918739
2.33990030857
2.33965175226
2.33942130105
2.33920704846
2.33900734635
2.33882076264
2.33864604703
2.33848210307
2.33832796527
2.33818278006
2.33804579007
2.33791632097
2.33779377038
2.33767759855
2.33756732039
2.33746249879
2.33736273877
2.33726768255
2.3371770053
2.3370904114
2.33700763123
2.33692841841
2.3368525473
2.3367798109



The series converges to: 7/3 or 2.3333..., and a_N was: 2.33671001895 and the difference was: 0.00337668561968

In [2]:
p2.part_b()




The series converges to: 7/3 or 2.3333..., and a_N was: 2.33671001895 and the difference was: 0.00337668561968
The limit exists (to this algorithm's knowledge)
The limit is roughly: 2.3367798109

In [3]:
p2.part_c()


None
The limit is roughly: 1.0

In [2]:
p2.part_d()


None
1.0
notice this fails because the function is oscillatory in behavior (sin(x))

In [3]:
p2.part_e()


None
0.0

In [3]:
p2.part_f()


the computations for x = pi go wrong for large N because sin(pi) = 0 and 2 ** (-n) approaches 0 as well, so the numerator and denominator both go to 0 and that usually leads to problems.

diffeq_midpoint

Taylor Patti

Uses the midpoint integration rule along with numpy vectors to produce a continuous vector which gives integral data for an array of prespecified points.

Here we use it to integrate sin from 0 to pi.


In [6]:
function_call = p4.vector_midpoint(p4.np.sin, 0, p4.np.pi, 10000)
print function_call[1][-1]


1.99999990953

Observe the close adherance to the actual value of this cannonical value.

We can also call it at a different value of x. Let's look at the value of this integral from 0 to pi over 2. Again, the result will have strikingly close adherance to the analytical value of this integral.


In [7]:
print function_call[1][5000]


1.00031411403

In [ ]: