Exemplo n.º 1
0
def Jacobian(func, inputs, step_size=1e-7, fdtype='c'):
    """Calculate the Jacobian of a function.

    INPUTS:
        func:       Function handle to use for Jacobian
        inputs:     Input values to function
        step_size:  Step size
        fdtype:     finite difference type ('c'entered, 'f'orward, 'b'ackward)

    RETURNS:
        jacobian:   An mxn array as specified by the tuple dim.
    """

    #test our return
    #try to find dimensions by being smart
    try:
        ndim = len(func(*inputs)), len(inputs)
    except TypeError:
        ndim = 1, len(inputs)

    jacobian = sp.zeros(ndim)

    if fdtype is 'c':
        for j in range(ndim[1]):
            jacobian[:,j] = FD.cdiff(func, inputs, vary=[j], accur=6, degree=1, tol=step_size)
    elif fdtype in ['f', 'b']:
        for j in range(ndim[1]):
            jacobian[:,j] = FD.fbdiff(func, inputs, vary=[j], accur=3, degree=1, direction=fdtype, tol=step_size)

    return jacobian
Exemplo n.º 2
0
def convergence():
    """Plot the convergence of the derivative approximation."""
    f = np.cos
    actual = -np.sin(3.0)
    hvals = np.linspace(1e-5, 1e-1)
    err1 = np.zeros(hvals.shape)
    err2 = np.zeros(hvals.shape)
    for i in xrange(len(hvals)):
        err1[i] = np.abs(actual - FiniteDiff.der(f, np.array([3.0]), mode='forward', h=hvals[i], degree=1))
        err2[i] = np.abs(actual - FiniteDiff.der(f, np.array([3.0]), mode='forward', h=hvals[i], degree=2))
    plt.subplot(121)
    plt.loglog(hvals, err1)
    plt.ylim((1e-11, 1e-1))
    plt.subplot(122)
    plt.loglog(hvals, err2)
    plt.ylim((1e-11, 1e-1))
    plt.savefig('figures/convergence.pdf')
    plt.clf()
Exemplo n.º 3
0
def convergence():
    """Plot the convergence of the derivative approximation."""
    f = np.cos
    actual = -np.sin(3.0)
    hvals = np.linspace(1e-5, 1e-1)
    err1 = np.zeros(hvals.shape)
    err2 = np.zeros(hvals.shape)
    for i in xrange(len(hvals)):
        err1[i] = np.abs(actual - FiniteDiff.der(
            f, np.array([3.0]), mode='forward', h=hvals[i], degree=1))
        err2[i] = np.abs(actual - FiniteDiff.der(
            f, np.array([3.0]), mode='forward', h=hvals[i], degree=2))
    plt.subplot(121)
    plt.loglog(hvals, err1)
    plt.ylim((1e-11, 1e-1))
    plt.subplot(122)
    plt.loglog(hvals, err2)
    plt.ylim((1e-11, 1e-1))
    plt.savefig('figures/convergence.pdf')
    plt.clf()
Exemplo n.º 4
0
"""
EPS = 1e-6
H = 1e-2
Y0 = 2
Y1 = 1.3
ALPHA0 = 0
ALPHA1 = 45
A = 0.7
B = 1

if __name__ == '__main__':
    shoot = Shoot(A, B, Y0, Y1, H, EPS, ALPHA0, ALPHA1)
    result = shoot.getAns()
    x = list(np.arange(A, B, (B - A) / len(result)))
    plt.plot(x, result)
    plt.title("Методо пристрілки")
    plt.show()
    print("Shoot method")
    k = 0
    for i in result:
        print('y%d' % k, i)
        k += 1

    finite = FiniteDiff(A, B, Y0, Y1, H, EPS)
    print("\nFinite difference method")
    rеsult = finite.getAns()
    finite.print(result)
    plt.plot(x, result)
    plt.title("Методо скінчених різниць з використання метода прогонки")
    plt.show()