198
7 Solving Nonlinear Algebraic Equations
# Here, either a solution is found, or too many iterations
if abs(F_norm) > eps:
iteration_counter = -1
return x, iteration_counter
We can test the function Newton_system with the 2×2 system (7.10) and (7.11):
def test_Newton_system1():
from numpy import cos, sin, pi, exp
def F(x):
return np.array(
[x[0]**2 - x[1] + x[0]*cos(pi*x[0]),
x[0]*x[1] + exp(-x[1]) - x[0]**(-1.)])
def J(x):
return np.array(
[[2*x[0] + cos(pi*x[0]) - pi*x[0]*sin(pi*x[0]), -1],
[x[1] + x[0]**(-2.), x[0] - exp(-x[1])]])
expected = np.array([1, 0])
tol = 1e-4
x, n = Newton_system(F, J, x=np.array([2, -1]), eps=0.0001)
print(n, x)
error_norm = np.linalg.norm(expected - x, ord=2)
assert error_norm < tol, ’norm of error ={:g}’.format(error_norm)
print(’norm of error ={:g}’.format(error_norm))
Here, the testing is based on the L2 norm 6 of the error vector. Alternatively,
we could test against the values of x that the algorithm finds, with appropriate
tolerances. For example, as chosen for the error norm, if eps=0.0001, a tolerance
of 10 −4 can be used for x[0] and x[1].
7.7 Exercises
Exercise 7.1: Understand Why Newton’s Method Can Fail
The purpose of this exercise is to understand when Newton’s method works and
fails. To this end, solve tanh x = 0 by Newton’s method and study the intermediate
details of the algorithm. Start with x 0 = 1.08. Plot the tangent in each iteration of
Newton’s method. Then repeat the calculations and the plotting when x 0 = 1.09.
Explain what you observe.
Filename: Newton_failure.*.
Exercise 7.2: See If the Secant Method Fails
Does the secant method behave better than Newton’s method in the problem
described in Exercise 7.1? Try the initial guesses
1. x 0 = 1.08 and x 1 = 1.09
2. x 0 = 1.09 and x 1 = 1.1
6 https://en.wikipedia.org/wiki/Norm_(mathematics)#Euclidean_norm.
7 Solving Nonlinear Algebraic Equations
# Here, either a solution is found, or too many iterations
if abs(F_norm) > eps:
iteration_counter = -1
return x, iteration_counter
We can test the function Newton_system with the 2×2 system (7.10) and (7.11):
def test_Newton_system1():
from numpy import cos, sin, pi, exp
def F(x):
return np.array(
[x[0]**2 - x[1] + x[0]*cos(pi*x[0]),
x[0]*x[1] + exp(-x[1]) - x[0]**(-1.)])
def J(x):
return np.array(
[[2*x[0] + cos(pi*x[0]) - pi*x[0]*sin(pi*x[0]), -1],
[x[1] + x[0]**(-2.), x[0] - exp(-x[1])]])
expected = np.array([1, 0])
tol = 1e-4
x, n = Newton_system(F, J, x=np.array([2, -1]), eps=0.0001)
print(n, x)
error_norm = np.linalg.norm(expected - x, ord=2)
assert error_norm < tol, ’norm of error ={:g}’.format(error_norm)
print(’norm of error ={:g}’.format(error_norm))
Here, the testing is based on the L2 norm 6 of the error vector. Alternatively,
we could test against the values of x that the algorithm finds, with appropriate
tolerances. For example, as chosen for the error norm, if eps=0.0001, a tolerance
of 10 −4 can be used for x[0] and x[1].
7.7 Exercises
Exercise 7.1: Understand Why Newton’s Method Can Fail
The purpose of this exercise is to understand when Newton’s method works and
fails. To this end, solve tanh x = 0 by Newton’s method and study the intermediate
details of the algorithm. Start with x 0 = 1.08. Plot the tangent in each iteration of
Newton’s method. Then repeat the calculations and the plotting when x 0 = 1.09.
Explain what you observe.
Filename: Newton_failure.*.
Exercise 7.2: See If the Secant Method Fails
Does the secant method behave better than Newton’s method in the problem
described in Exercise 7.1? Try the initial guesses
1. x 0 = 1.08 and x 1 = 1.09
2. x 0 = 1.09 and x 1 = 1.1
6 https://en.wikipedia.org/wiki/Norm_(mathematics)#Euclidean_norm.
