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

Chinmai Raman

Homework 3

A.4 Solving a system of difference equations

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]:
(array([   0.        ,  883.33333333,  879.16666667,  875.        ,
         870.83333333,  866.66666667,  862.5       ,  858.33333333,
         854.16666667,  850.        ,  845.83333333,  841.66666667,  837.5       ]),
 array([  1.00000000e+04,   9.16666667e+03,   8.33333333e+03,
          7.50000000e+03,   6.66666667e+03,   5.83333333e+03,
          5.00000000e+03,   4.16666667e+03,   3.33333333e+03,
          2.50000000e+03,   1.66666667e+03,   8.33333333e+02,
         -7.95807864e-13]))

A.11 Testing different methods of root finding

$f(x) = Sin(x)$


In [3]:
p2.graph(p2.f1, 100, -2 * np.pi, 2 * np.pi)



In [3]:
p2.Newton(p2.f1, p2.f1prime, -4)


Out[3]:
array([-4.        , -2.84217872, -3.15087294, -3.14159239, -3.14159265])

In [2]:
p2.bisect(p2.f1, -4, -2)


Out[2]:
[-3.0,
 -3.5,
 -3.25,
 -3.125,
 -3.1875,
 -3.15625,
 -3.140625,
 -3.1484375,
 -3.14453125,
 -3.142578125,
 -3.1416015625]

In [3]:
p2.secant(p2.f1, -4.5, -3.5)


Out[3]:
array([-4.5       , -3.5       , -2.94031118, -3.14349649, -3.14157986])

$f(x) = x - sin(x)$


In [4]:
p2.graph(p2.f2, 100, -np.pi, np.pi)



In [5]:
p2.Newton(p2.f2, p2.f2prime, 1)


Out[5]:
array([ 1.        ,  0.65514507,  0.43359037,  0.2881484 ,  0.19183231,
        0.12780967,  0.08518323,  0.05678195,  0.0378526 ,  0.02523446,
        0.0168228 ,  0.01121515,  0.00747675])

In [8]:
p2.bisect(p2.f2, -1, 1)


Out[8]:
[0.0,
 -0.5,
 -0.25,
 -0.125,
 -0.0625,
 -0.03125,
 -0.015625,
 -0.0078125,
 -0.00390625,
 -0.001953125,
 -0.0009765625]

In [9]:
p2.secant(p2.f2, -2, -1)


Out[9]:
array([-2.        , -1.        , -0.82993616, -0.59447066, -0.45395989,
       -0.33964378, -0.25661945, -0.19337445, -0.14595533, -0.11013331,
       -0.08312856, -0.06274507, -0.04736282, -0.035752  ])

$f(x) = x^5 - sin x$


In [7]:
p2.graph(p2.f3, 100, -np.pi / 2, np.pi / 2)



In [7]:
p2.Newton(p2.f3, p2.f3prime, -1)


Out[7]:
array([-1.        , -0.96445297, -0.96106599, -0.96103694])

In [5]:
p2.bisect(p2.f3, -1, 1)


Out[5]:
[0.0,
 -0.5,
 -0.75,
 -0.875,
 -0.9375,
 -0.96875,
 -0.953125,
 -0.9609375,
 -0.96484375,
 -0.962890625,
 -0.9619140625]

In [6]:
p2.secant(p2.f3, -1, -0.5)


Out[6]:
array([-1.        , -0.5       , -0.86935238, -1.41540126, -0.89878402,
       -0.91950244, -0.96874678, -0.96018937, -0.96102068])

$f(x) = x^4sinx$


In [8]:
p2.graph(p2.f4, 100, -2 * np.pi, 2 * np.pi)



In [12]:
p2.Newton(p2.f4, p2.f4prime, -4)


Out[12]:
array([-4.        , -3.46343041, -3.22270163, -3.14886404, -3.14165908,
       -3.14159266, -3.14159265])

In [13]:
p2.bisect(p2.f4, -4, -2)


Out[13]:
[-3.0,
 -3.5,
 -3.25,
 -3.125,
 -3.1875,
 -3.15625,
 -3.140625,
 -3.1484375,
 -3.14453125,
 -3.142578125,
 -3.1416015625]

In [14]:
p2.secant(p2.f4, -5, -4)


Out[14]:
array([-5.        , -4.        , -3.52231752, -3.32221563, -3.19821657,
       -3.15222126, -3.14231362, -3.14160231, -3.14159266])

$f(x) = x^4 - 16$


In [9]:
p2.graph(p2.f5, 100, -2 * np.pi, 2 * np.pi)



In [15]:
p2.Newton(p2.f5, p2.f5prime, -3)


Out[15]:
array([-3.        , -2.39814815, -2.08863379, -2.00548401, -2.00002245, -2.        ])

In [17]:
p2.bisect(p2.f5, -3, -1)


Out[17]:
[-2.0,
 -2.5,
 -2.25,
 -2.125,
 -2.0625,
 -2.03125,
 -2.015625,
 -2.0078125,
 -2.00390625,
 -2.001953125,
 -2.0009765625]

In [18]:
p2.secant(p2.f5, -4, -3)


Out[18]:
array([-4.        , -3.        , -2.62857143, -2.27412522, -2.09269719,
       -2.01647656, -2.00109513, -2.00001343, -2.00000001])

$f(x) = x^{10} - 1$


In [10]:
p2.graph(p2.f6, 100, -2 * np.pi, 2 * np.pi)



In [2]:
p2.Newton(p2.f6, p2.f6prime, 2)


Out[2]:
array([ 2.        ,  1.80019531,  1.62067942,  1.45990784,  1.31723655,
        1.19388869,  1.09479185,  1.02957313,  1.00354395,  1.00005579,
        1.00000001,  1.        ])

In [5]:
p2.bisect(p2.f6, 0, 2)


Out[5]:
[1.0,
 0.5,
 0.75,
 0.875,
 0.9375,
 0.96875,
 0.984375,
 0.9921875,
 0.99609375,
 0.998046875,
 0.9990234375]

In [6]:
p2.secant(p2.f6, 3, 2)


Out[6]:
array([ 3.        ,  2.        ,  1.98236967,  1.79214077,  1.68328166,
        1.55903311,  1.45252881,  1.35151274,  1.26056759,  1.17916623,
        1.10992776,  1.05600265,  1.02090694,  1.00459446,  1.0004126 ,
        1.00000845])

$tanh(x) - x^{10}$


In [11]:
p2.graph(p2.f7, 100, -2 * np.pi, 2 * np.pi)



In [8]:
p2.Newton(p2.f7, p2.f7prime, 1)


Out[8]:
array([ 1.        ,  0.97511428,  0.97162416,  0.97156264,  0.97156262])

In [11]:
p2.bisect(p2.f7, 0.5, 2)


Out[11]:
[1.25,
 0.875,
 1.0625,
 0.96875,
 1.015625,
 0.9921875,
 0.98046875,
 0.974609375,
 0.9716796875,
 0.97021484375,
 0.970947265625]

In [15]:
p2.secant(p2.f7, 3, 2)


Out[15]:
array([ 3.        ,  2.        ,  1.98236904,  1.79213013,  1.68325462,
        1.55895892,  1.45235015,  1.35108605,  1.25958823,  1.17698582,
        1.10530645,  1.04691452,  1.00508266,  0.98156429,  0.97307408,
        0.97163565,  0.97156317])

A.13 Computing the arc length of a curve


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


(array([-2. , -1.6, -1.2, -0.8, -0.4,  0. ,  0.4,  0.8,  1.2,  1.6,  2. ]), array([ 0.        ,  0.4       ,  0.80001457,  1.20388767,  1.64883793,
        2.08992964,  2.53102135,  2.97597161,  3.37984471,  3.77985928,
        4.17985928]))

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)


A.14 Finding difference equations for computing sin(x)

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


[-0.7070959900908971, -0.7071067811796194, 0.7071067811796194, 0.7070959900908971]

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


[0.7046526512091675, 0.7071064695751781, 0.7071067811796194, 0.7071067811865475]