In [1]:
import numpy as np
import math

import fit_pendulum_data as p1
import midpoint_vec as p2
import Lagrange_poly1 as p3
import Lagrange_poly2 as p4
import Lagrange_poly3 as p5

Homework 2

Taylor Patti

2/15/2015

Excercises Completed

  1. Exercise 5.18 (fit_pendulum_data.py)
  2. Exercise 5.22 (midpoint_vec.py)
  3. Exercise 5.23 (Lagrange_poly1.py)
  4. Exercise 5.24 (Lagrange_poly2.py)
  5. Exercise 5.25 (Lagrange_poly2b.py)

fit_pendulum_data

Provides various functions which facilitate the harvesting of data from a separate file, the plotting of this data, and the creation of composite plots consisting of this data and polynomial fits thereof of various orders. Ultimately, the third order polynomial had the best fit, although only marginally so over the 2nd order. This seems pretty common though; that a higher order polynomial usually beats out its competitors, especially over small data sets, even if they are more physically relevant.


In [4]:
p1.pendulum_plotter()



In [5]:
p1.poly_plotter()


midpoint_vec

This module called for the design of a midpoint integration function in three separate implementations, one list iterative, one semi-vectorized, and one fully vectorized. The supreme accuracy of this algorithm is illustrated in the sine integration below. The purpose of devising 3 different implementations was to be able to compare their relative efficiency. As denoted below, the list iterative midpointint function faired the fastest, most likely due to the relatively small number of iterations required. Moreover, the fully vectorized midpointvec was faster than the semi-vectorized midpointvecdefault, most likely due to the superior efficiency of the numpy sum function. Had the number of elements in these iterations been greater, the fully vectorized version would have faired better than both of its counterpart functions.


In [9]:
p2.midpointvec(np.sin, 0, np.pi/2, 1000)


Out[9]:
1.0000001028083867

In [6]:
%timeit p2.midpointint(p2.x_func, 1, 3, 1000)


1000 loops, best of 3: 244 µs per loop

In [7]:
%timeit p2.midpointvecdefault(p2.x_func, 1, 3, 1000)


1000 loops, best of 3: 313 µs per loop

In [8]:
%timeit p2.midpointvec(p2.x_func, 1, 3, 1000)


1000 loops, best of 3: 292 µs per loop

Lagrange_poly1

In this module, the Lagrange Interpolation Formula was used to construct an interpolating function; a function who takes in n data points and devises an nth order polynomial which it then implements to extrapolate the value of all points within the boundaries of the data set, whether included or not. This first excercise shows how accurate this process is, with the interpolating function returning a nearly perfect value for the sine function when the input is given at the midpoint between two of the data points provided.


In [16]:
xp = np.linspace(0, math.pi, 5)
yp = np.sin(xp)
middle = xp[2] - xp[1] / 2
print 'Function interpolates: ',
print p3.p_L(middle, xp, yp)
print 'Actual sine result is: ',
print math.sin(middle)


Function interpolates:  0.924095869121
Actual sine result is:  0.923879532511

Lagrange_poly2

A graphing function was constructed that permitted the simultaneous plotting of the actual function and the interpolating function, both with indicated interval perpherations.


In [17]:
p4.graph(np.sin, 5, 0, math.pi)


Lagrange_poly3

The oscillatory shortcomings of high peripheration interpolating functions is demonstrated by the creation of two plots. The first shows how the general fit quality increases with greater peripheration frequency while this quantity is reasonably small. The later indicates that too many terms can result in wild oscillations at the end points of the function indicated.


In [18]:
p5.multigrapher()