In [20]:
import numpy as np
import sympy as sp
import math
import loan as p1
import root_finder_examples as p2
import arclength as p3
import sin_Taylor_series_diffeq as p4
import matplotlib.pyplot as plt
# Needed only in Jupyter to render properly in-notebook
%matplotlib inline
Computes the development of a loan over time.
The below function calculates the amount paid per month (the first array) and the amount left to be paid (the second array) at each month of the year at a principal of $10,000 to be paid over 1 year at annual interest rate of 6%
In [2]:
p1.loan(6, 10000, 12)
Out[2]:
In [3]:
p2.graph(p2.f1, 100, -2 * np.pi, 2 * np.pi)
In [3]:
p2.Newton(p2.f1, p2.f1prime, -4)
Out[3]:
In [2]:
p2.bisect(p2.f1, -4, -2)
Out[2]:
In [3]:
p2.secant(p2.f1, -4.5, -3.5)
Out[3]:
In [4]:
p2.graph(p2.f2, 100, -np.pi, np.pi)
In [5]:
p2.Newton(p2.f2, p2.f2prime, 1)
Out[5]:
In [8]:
p2.bisect(p2.f2, -1, 1)
Out[8]:
In [9]:
p2.secant(p2.f2, -2, -1)
Out[9]:
In [7]:
p2.graph(p2.f3, 100, -np.pi / 2, np.pi / 2)
In [7]:
p2.Newton(p2.f3, p2.f3prime, -1)
Out[7]:
In [5]:
p2.bisect(p2.f3, -1, 1)
Out[5]:
In [6]:
p2.secant(p2.f3, -1, -0.5)
Out[6]:
In [8]:
p2.graph(p2.f4, 100, -2 * np.pi, 2 * np.pi)
In [12]:
p2.Newton(p2.f4, p2.f4prime, -4)
Out[12]:
In [13]:
p2.bisect(p2.f4, -4, -2)
Out[13]:
In [14]:
p2.secant(p2.f4, -5, -4)
Out[14]:
In [9]:
p2.graph(p2.f5, 100, -2 * np.pi, 2 * np.pi)
In [15]:
p2.Newton(p2.f5, p2.f5prime, -3)
Out[15]:
In [17]:
p2.bisect(p2.f5, -3, -1)
Out[17]:
In [18]:
p2.secant(p2.f5, -4, -3)
Out[18]:
In [10]:
p2.graph(p2.f6, 100, -2 * np.pi, 2 * np.pi)
In [2]:
p2.Newton(p2.f6, p2.f6prime, 2)
Out[2]:
In [5]:
p2.bisect(p2.f6, 0, 2)
Out[5]:
In [6]:
p2.secant(p2.f6, 3, 2)
Out[6]:
In [11]:
p2.graph(p2.f7, 100, -2 * np.pi, 2 * np.pi)
In [8]:
p2.Newton(p2.f7, p2.f7prime, 1)
Out[8]:
In [11]:
p2.bisect(p2.f7, 0.5, 2)
Out[11]:
In [15]:
p2.secant(p2.f7, 3, 2)
Out[15]:
In [36]:
h1 = -4 * (x)**2
x = sp.Symbol('x')
h2 = sp.exp(h1)
h3 = 1 / np.sqrt(2 * np.pi) * h2
length = p3.arclength(h3, -2, 2, 10)
print length
The arclength of the function f(x) from -2 to 2 is 4.18
In [38]:
fig = plt.figure(1)
x = np.linspace(-2, 2, 100)
y = 1 / np.sqrt(2 * np.pi) * np.exp(-4 * x**2)
x1 = length[0]
y1 = length[1]
plt.plot(x, y, 'r-', x1, y1, 'b-')
plt.xlabel('x')
plt.ylabel('y')
plt.title('1/sqrt(2pi) * e^(-4t^2)')
plt.show(fig)
The accuracy of a Taylor polynomial improves as x decreases (moves closer to zero).
In [11]:
x = [-3 * np.pi / 4.0, -np.pi / 4.0, np.pi / 4.0, 3 * np.pi / 4]
N = [5, 5, 5, 5]
n = 0
Sn = []
while n < 4:
Sn.append(p4.sin_Taylor(x[n], N[n])[0])
n += 1
print Sn
The accuracy of a Taylor polynomial also improves as n increases.
In [14]:
x = [np.pi / 4, np.pi / 4, np.pi / 4, np.pi / 4]
N = [1, 3, 5, 10]
n = 0
Sn = []
while n < 4:
Sn.append(p4.sin_Taylor(x[n], N[n])[0])
n += 1
print Sn