def test_taylor_approx_x():
    x = np.array([3,4,5,6,7,8])
    f = tp.tanh(x)
    trial = tp.taylor(x,f,0,100)[0]
    actual = x
    print("Trial: ", trial)
    print("Actual: ", actual)
    np.testing.assert_equal(actual, trial)
    
    x = np.array([3,4,5,6,7,8])
    func = np.vectorize(tp.inverse)
    f = func(x)
    trial = tp.taylor(x,f,0,100)[0]
    actual = x
    print("Trial: ", trial)
    print("Actual: ", actual)
    np.testing.assert_equal(actual, trial)
    
    x = np.array([3,4,5,6,7,8])
    func = np.vectorize(tp.f)
    f = func(x)
    trial = tp.taylor(x,f,0,100)[0]
    actual = x
    print("Trial: ", trial)
    print("Actual: ", actual)
    np.testing.assert_equal(actual, trial)
def test_taylor_approx_acc():
    x = np.array([3,4,5,6,7,8])
    f = tp.tanh(x)
    trial = tp.taylor(x,f,0,100)[1] #returns 2 arrays, takes the second entry
    actual = tp.tanh(3)
    print("Trial: ", trial[0])
    print("Actual: ", actual)
    nose.tools.assert_almost_equal(actual, trial[0], 2)
    
    x = np.array([3,4,5,6,7,8])
    func = np.vectorize(tp.inverse)
    f = func(x)
    trial = tp.taylor(x,f,0,100)[1]
    actual = tp.inverse(3)
    print("Trial: ", trial[0])
    print("Actual: ", actual)
    nose.tools.assert_almost_equal(actual, trial[0], 2)
    
    x = np.array([3,4,5,6,7,8])
    func = np.vectorize(tp.f)
    f = func(x)
    trial = tp.taylor(x,f,0,100)[1]
    actual = tp.f(3)
    print("Trial: ", trial[0])
    print("Actual: ", actual)
    nose.tools.assert_almost_equal(actual, trial[0], 2)
Пример #3
0
def make_plots(x, i):
    for f, s in (
        (np.sin(x), "sin(x)"), (np.tanh(x), "tanh(x)"),
        (my_functions.poly(x), "poly(x)"), (my_functions.denom(x), "denom(x)"),
        (my_functions.theta(x), "theta(x)")
    ):  #ensures all functions are being plotted, including the sin(x) test function.
        for n in range(
                0, 6):  #range enables 0-5th order approximations to be plotted
            out_X, out_fapprox = taylor_approx.taylor(x, f, i,
                                                      n)  #plots approximations
            plt.figure(figsize=(3, 3))
            plt.ylabel('y')  #y-axis label
            plt.xlabel('x')  #x-axis label
            font = {'size': 16}  #font size
            title = n  #enables the title to be each function's name
            plt.title(title)
            plt.plot(out_X, out_fapprox, label=s + "approx", color="green"
                     )  #green plot demonstrates the ordered approximation
            plt.plot(x, f, label=s,
                     color="blue")  #blue plot demonstrates the actual function
            plt.legend(loc='upper left', frameon=False)
            plt.plot(
                x[500], f[500], 'r*'
            )  #marks the specific point which function is being approximated around
            plt.show()
def test_taylor_5():
    """test_taylor_5()
    Tests for the accuracy of the Taylor formula approximation at point x[50] by calling sin(x) and taylor; fifth order derivative approximation
    """
    a = -5
    b = 5
    numberOfPoints = 101
    x = np.linspace(a, b, numberOfPoints)
    f = my_functions.sin(x)
    i = 50
    out_X, out_fapprox = taylor_approx.taylor(
        x, f, i - 10, n=5)  #approximating x[50] at x[40], described as i-10
    desired = -0.0005
    np.testing.assert_almost_equal(out_fapprox[i], desired, 1)
Пример #5
0
def test_taylor_approx():
    """Tests our function taylor(x,f,i,n) to approximate the Gaussian function around
    some point arbitrarily chosen."""
    a, b, n = 0, 3, 1000
    t = np.linspace(a, b, n)
    desired = ac.g(a, b, n)
    # Values obtained from taylor approximation
    actual = ta.taylor(t, desired, 501, 10)
    # Debug message
    print("We expected this: ", desired[500:503], " but got this: ",
          actual[1][500:503])
    # Testing accuracy
    for k in range(500, 502):
        nose.tools.assert_almost_equal(desired[k], actual[1][k], 4)