コード例 #1
0
    def test_newton(self):
        """
        뉴튼 랩슨법 함수를 검증하는 method
        :return:
        """

        #1 변수 방정식의 해를 뉴튼 랩슨법으로 찾음
        result = rf.newton(f, dfdx, 100, epsilon=1e-8)

        # 유효숫자 6번쨰 자리까지 f(x) ==0임을 확인함
        self.assertAlmostEqual(0.0, f(result), places=6)
コード例 #2
0
# -*- coding: utf8 -*-
# [2011112047] [한소운]
"""
1변수 방정식의 근을 찾는 방법 중 Newton-Raphson method 를 사용하여
어떤 함수 g(x)의 근을 찾고자 함
아래 예는 Newton-Raphson method 를 사용하기 곤란한 경우임
"""
# "Newton's method," Wikipedia. [Online]. Available: http://en.wikipedia.org/wiki/newton's_method.

# 1변수 방정식의 근을 찾는 함수를 모아둔 root_finding 모듈을 불러들임

import root_finding as rf
help(root_finding)


def g(x):
    # 근을 구하고자 하는 함수
    return x ** 3 - 2 * x + 2


def dgdx(x):
    # g(x) 의 x 에 대한 미분
    return 3.0 * x ** 2.0 - 2.0


if "__main__" == __name__:
    # 주어진 초기값에서 시작하여 g(x) = 0 인 x 를 찾고자 함
    # 생각보다 시간이 많이 걸릴 수 있음
    x_nr = rf.newton(g, dgdx, 0)
    print('x = %g, f(%g) = %g' % (x_nr, x_nr, g(x_nr)))
コード例 #3
0
def bmain():
    """
    Test of the first root finding method
    """
    # Initialization
    x0 = np.array([3, -1], dtype='float64')
    dx = np.array([1e-9, 1e-9], dtype='float64')

    print('Check part A')
    print('Solve the system A*x*y = 1')
    print(', where exp(-x) + exp(-y) = 1 + 1/A, and A=10000.')
    print('Starting point: x0 =\n', x0)
    print('f(x0) =\n', systems.sys(x0))
    print('The roots using Newton method:')
    roots = root_finding.newton_jacobian(systems.sys, x0, systems.jacobian_sys)
    print(roots)
    print('f(roots) =\n', systems.sys(roots))
    print('Number of calls:', globvar.ncalls)
    globvar.ncalls = 0
    roots = root_finding.newton(systems.sys, x0, dx)
    print('Using the Newton method but without the Jacobian, the roots were')
    print(roots)
    print('Using the method from part A, the number of calls were',
          globvar.ncalls)
    print('\n\n')

    # Initialization
    globvar.ncalls = 0
    x0 = np.array([1, 400], dtype='float64')
    dx = np.array([1e-9, 1e-9], dtype='float64')

    print('Check part B')
    print('Solve the Rosenbrock valley function')
    print('Starting point: x0 =\n', x0)
    print('f(x0) =\n', systems.rosenbrock(x0))
    print('The roots using Newton method:')
    roots = root_finding.newton_jacobian(systems.rosenbrock, x0,
                                         systems.jacobian_rosenbrock)
    print(roots)
    print('f(roots) =\n', systems.rosenbrock(roots))
    print('Number of calls:', globvar.ncalls)
    globvar.ncalls = 0
    roots = root_finding.newton(systems.rosenbrock, x0, dx)
    print('Using the Newton method but without the Jacobian, the roots were')
    print(roots)
    print('Using the method from part A, the number of calls were',
          globvar.ncalls)
    print('\n\n')

    # Initialization
    globvar.ncalls = 0
    x0 = np.array([1, -1], dtype='float64')
    dx = np.array([1e-9, 1e-9], dtype='float64')

    print('Check part C')
    print('Solve the Himmelblau function')
    print('Starting point: x0 =\n', x0)
    print('f(x0) =\n', systems.himmelblau(x0))
    print('The roots using Newton method:')
    roots = root_finding.newton_jacobian(systems.himmelblau, x0,
                                         systems.jacobian_himmelblau)
    print(roots)
    print('f(roots) =\n', systems.himmelblau(roots))
    print('Number of calls:', globvar.ncalls)
    globvar.ncalls = 0
    roots = root_finding.newton(systems.himmelblau, x0, dx)
    print('Using the Newton method but without the Jacobian, the roots were')
    print(roots)
    print('Using the method from part A, the number of calls were',
          globvar.ncalls)
    print('\n\n')

    # Initialization
    globvar.ncalls = 0
    x0 = np.array([1, -1], dtype='float64')
    dx = np.array([1e-9, 1e-9], dtype='float64')

    print('Check part D: Make some interesting example')
    print('Solve the Matya function')
    print('Starting point: x0 =\n', x0)
    print('f(x0) =\n', systems.matya(x0))
    print('The roots using Newton method:')
    roots = root_finding.newton_jacobian(systems.matya, x0,
                                         systems.jacobian_matya)
    print(roots)
    print('f(roots) =\n', systems.matya(roots))
    print('Number of calls:', globvar.ncalls)
    globvar.ncalls = 0
    roots = root_finding.newton(systems.matya, x0, dx)
    print('Using the Newton method but without the Jacobian, the roots were')
    print(roots)
    print('Using the method from part A, the number of calls were',
          globvar.ncalls)
    print('\n\n')
コード例 #4
0
ファイル: bmain.py プロジェクト: amaliestokholm/numeric
def bmain():
    """
    Test of the minimization method using the Quasi-Newton method
    """
    # Initialization
    globvar.ncalls = 0
    x0 = np.array([-2, 2], dtype='float64')
    dx = np.array([1e-9, 1e-9], dtype='float64')
    alpha = 1e-4

    print('Testing the quasi-Newton method with Broydens update')
    print('Check part A')
    print('Minimize the Rosenbrock valley function')
    print('f(x, y) = (1 - x)^2 + 100 * (y - x^2)^2')
    print('Starting point: x0 =\n', x0)
    print('f(x0) =\n', systems.rosenbrock(x0))
    print('The minimum is found as:')
    mini = minimize.qnewton_minimize(systems.rosenbrock,
                                     systems.grad_rosenbrock, x0, alpha)
    print(mini)
    print('f(min) =\n', systems.rosenbrock(mini))
    print('Number of steps used:', globvar.ncalls)
    print('We can now compare the number of steps used by different methods')
    globvar.ncalls = 0
    mini = minimize.newton_minimize(systems.rosenbrock,
                                    systems.grad_rosenbrock,
                                    systems.hessian_rosenbrock, x0, alpha)
    print('Number of steps used in the Newton minimization (A) is',
          globvar.ncalls)

    globvar.ncalls = 0
    mini = root.newton(systems.grad_rosenbrock, x0, dx)
    print('Number of steps used in the Newton root-finding is', globvar.ncalls)

    globvar.ncalls = 0
    mini = root.newton_jacobian(systems.grad_rosenbrock, x0,
                                systems.hessian_rosenbrock)
    print('Number of steps using Newton root-finding with the Jacobian is',
          globvar.ncalls)
    print('\n\n')

    # Initialization
    x0 = np.array([2.5, 2.0], dtype='float64')
    alpha = 1e-4
    globvar.ncalls = 0

    print('Check part B')
    print('Minimize the Himmelblau function')
    print('f(x, y) = (x^2 + y -11)^2 + (x + y^2 - 7)^2')
    print('Starting point: x0 =\n', x0)
    print('f(x0) =\n', systems.himmelblau(x0))
    print('The minimum is found as:')
    mini = minimize.qnewton_minimize(systems.himmelblau,
                                     systems.grad_himmelblau, x0, alpha)
    print(mini)
    print('f(min) =\n', systems.himmelblau(mini))
    print('Number of steps used:', globvar.ncalls)
    print('We can now compare the number of steps used by different methods')
    globvar.ncalls = 0
    mini = minimize.newton_minimize(systems.himmelblau,
                                    systems.grad_himmelblau,
                                    systems.hessian_himmelblau, x0, alpha)
    print('Number of steps used in the Newton minimization (A) is',
          globvar.ncalls)

    globvar.ncalls = 0
    mini = root.newton(systems.grad_himmelblau, x0, dx)
    print('Number of steps used in the Newton root-finding is', globvar.ncalls)

    globvar.ncalls = 0
    mini = root.newton_jacobian(systems.grad_himmelblau, x0,
                                systems.hessian_himmelblau)
    print('Number of steps using Newton root-finding with the Jacobian is',
          globvar.ncalls)
    print('\n\n')
コード例 #5
0
ファイル: nr_problem_case.py プロジェクト: kangwonlee/15ecaa
# -*- coding: utf8 -*-
# [학번] [이름]
"""
1변수 방정식의 근을 찾는 방법 중 Newton-Raphson method 를 사용하여
어떤 함수 g(x)의 근을 찾고자 함
아래 예는 Newton-Raphson method 를 사용하기 곤란한 경우임
"""
# "Newton's method," Wikipedia. [Online]. Available: https://en.wikipedia.org/wiki/newton's_method. [Accessed: 21-Aug-2016].

# 1변수 방정식의 근을 찾는 함수를 모아둔 root_finding 모듈을 불러들임

import root_finding as rf


def g(x):
    # 근을 구하고자 하는 함수
    return x ** 3 - 2 * x + 2


def dgdx(x):
    # g(x) 의 x 에 대한 미분
    return 3.0 * x ** 2.0 - 2.0


if "__main__" == __name__:
    # 주어진 초기값에서 시작하여 g(x) = 0 인 x 를 찾고자 함
    # 생각 보다 시간이 많이 걸릴 수 있음
    x_nr = rf.newton(g, dgdx, 0)
    print('x = %g, f(%g) = %g' % (x_nr, x_nr, g(x_nr)))
コード例 #6
0
 def test_newton(self):
     result = rf.newton(f, dfdx, 0.0001, epsilon=1e-8)
     self.assertAlmostEqual(0.0, f(result), places=3)