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
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()
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]:
In [6]:
%timeit p2.midpointint(p2.x_func, 1, 3, 1000)
In [7]:
%timeit p2.midpointvecdefault(p2.x_func, 1, 3, 1000)
In [8]:
%timeit p2.midpointvec(p2.x_func, 1, 3, 1000)
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)
In [17]:
p4.graph(np.sin, 5, 0, math.pi)
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()