예제 #1
0
def test_gauss_newton_nonlinear_solve():
    """Test that gauss_newton solves nonlinear systems."""
    # The function being optimized is c0 + c1^2 * x^2 + c2^3 * x^3
    # The points are evaluated on [1., 2., 3.]
    x0 = np.array([20., 2.5, 500.])
    result = gauss_newton_solve(f1, j1, x0, maxiter=5000)

    f_expt = np.array([0.] * 3)
    x_expt = np.array([2., 3., 5.])
    message_expt = "Convergence obtained."
    jac_expt = j1(x_expt)
    assert np.allclose(f_expt, result['f'], rtol=1e-5, atol=1e-5)
    assert np.allclose(jac_expt, result['J'], rtol=1e-5, atol=1e-5)
    assert np.allclose(x_expt, result['x'])
    assert result['success']
    assert result['message'] == message_expt

    # Because of symmetry the second coefficient of -3 should work
    x0 = np.array([200., -200., 200.])
    result = gauss_newton_solve(f1, j1, x0, maxiter=5000)
    x_expt = np.array([2., -3., 5.])
    jac_expt = j1(x_expt)
    assert np.allclose(f_expt, result['f'], rtol=1e-5, atol=1e-5)
    assert np.allclose(jac_expt, result['J'], rtol=1e-5, atol=1e-5)
    assert np.allclose(x_expt, result['x'])
    assert result['success']
    assert result['message'] == message_expt
예제 #2
0
def test_gauss_newton_nonlinear_overdetermined_solve():
    """Test that gauss_newton solves nonlinear, overdetermined systems."""
    # The function being optimized is c0 + c1^2 * x^2 + c2^3 * x^3
    x0 = np.array([20., 2.5, 500.])
    result = gauss_newton_solve(f3, j3, x0)

    f_expt = np.array([0.] * 4)
    x_expt = np.array([2., 3., 5.])
    message_expt = "Convergence obtained."
    jac_expt = j3(x_expt)
    assert np.allclose(f_expt, result['f'], rtol=1e-5, atol=1e-5)
    assert np.allclose(jac_expt, result['J'], rtol=1e-5, atol=1e-5)
    assert np.allclose(x_expt, result['x'])
    assert result['success']
    assert result['message'] == message_expt

    # Because of symmetry the second coefficient of -3 should work
    x0 = np.array([1000., -200., 500.])
    result = gauss_newton_solve(f3, j3, x0)
    x_expt = np.array([2., -3., 5.])
    message_expt = "Convergence obtained."
    jac_expt = j3(x_expt)
    assert np.allclose(f_expt, result['f'], rtol=1e-5, atol=1e-5)
    assert np.allclose(jac_expt, result['J'], rtol=1e-5, atol=1e-5)
    assert np.allclose(x_expt, result['x'])
    assert result['success']
    assert result['message'] == message_expt
예제 #3
0
def test_gauss_newton_singular_matrix_error():
    """Test that gauss_newton raises error when Jaocbian is singular."""
    x0 = np.array([5., 5.])
    result = gauss_newton_solve(f5, j5, x0)
    message_expt = "Singular Jacobian; no solution found."
    assert not result['success']
    assert result["message"] == message_expt
    assert result["eps"] == 1e-6
예제 #4
0
def test_gauss_newton_nonlinear_underdetermined_solve():
    """Test that gauss_newton solves nonlinear, underdetermined systems."""
    # Non-Exact Initial Guess. Here a really Good Initial guess is needed
    x_expt = np.array([2., 3., 5., 8.])
    f_expt = np.array([0.] * 3)
    x0 = np.array([2.001, 3.001, 5.001, 10.01])
    result = gauss_newton_solve(f4, j4, x0, maxiter=12)
    message_expt = "Maximum number of iterations reached."
    assert result['message'] == message_expt

    # Repeat with a higher number of iterations and a really good initial guess
    # Still it only converges to the first decimal place.
    x0 = np.array([2.00001, 3.00001, 5.00001, 8.00001])
    result = gauss_newton_solve(f4, j4, x0, maxiter=10000)
    print(result)
    jac_expt = j4(result["x"])
    assert np.allclose(f_expt, result['f'], rtol=1e-5, atol=1e-5)
    assert np.allclose(jac_expt, result['J'], rtol=1e-5, atol=1e-5)
    assert np.allclose(x_expt, result['x'], rtol=1e-1, atol=1e-1)
    assert result['message'] == "Convergence obtained."
    assert result['success']
예제 #5
0
def test_gauss_newton_linear_solve():
    """Test that gauss_newton solves linear systems in 1 step."""
    # Obtained from pg 489 of Numerical Mathematics and Computing Sixth Edition.
    x0 = np.array([100., -200.])
    result = gauss_newton_solve(f_lin, j_lin, x0, eps=1e-20)

    x_expt = np.array([0.4864, -1.6589])
    f_expt = np.array([0., 0.])
    jac_expt = j_lin(1.)
    message_expt = "Convergence obtained."
    assert np.allclose(f_expt, result['f'], rtol=1e-5, atol=1e-5)
    assert np.allclose(jac_expt, result['J'], rtol=1e-5, atol=1e-5)
    assert np.allclose(x_expt, result['x'], rtol=1e-4, atol=1e-4)
    assert result['success']
    assert result['message'] == message_expt