Table of contents
1. Finding roots of nonlinear equations
1. Bisection Method (Bisection Method)
1. Finding roots of nonlinear equations
Examples of nonlinear equations:
Finding the roots of nonlinear equations is an important numerical calculation problem. Commonly used methods include the bisection method, iteration method and Newton's iteration method.
1. Bisection Method (Bisection Method)
a. Introduction to theory
(Intermediate value theorem of continuous functions)
The bisection method is a simple and intuitive root finding method suitable for the roots of monotonic functions. Its basic idea is to approximate the position of the root by continuously shrinking the interval where the root is located. Specific steps are as follows:
- First, choose an initial interval [a, b] to ensure that the function is continuous within this interval and the function values have different signs (i.e. f(a) * f(b) < 0).
- Then, calculate the midpoint of the interval c = (a + b) / 2, and calculate the value of the function at c, f(c).
- Next, according to the relationship between f(c) and 0, determine the new interval [a, c] or [c, b], so that the condition of different signs of function values is still satisfied in the new interval.
- Repeat the above steps until the preset accuracy requirements are met, that is, the approximate value of the root falls within the selected interval.
b. python implementation
def f(x):
return 5 * x**4 + 3 * x + 1
def bisection_method(a, b, tolerance=1e-6, max_iterations=100):
if f(a) * f(b) >= 0:
return None
for _ in range(max_iterations):
c = (a + b) / 2
if abs(f(c)) < tolerance:
return c
if f(c) * f(a) < 0:
b = c
else:
a = c
return None
# 调用二分法求解方程的根
root = bisection_method(a=-1, b=0)
if root is not None:
print("方程的一个根为:", root)
else:
print("未找到方程的根")
Note that the bisection method requires that the initial interval [a, b] satisfies f(a) * f(b) < 0, that is, the equation has different values at the two endpoints of the interval.
Output:
a=-0.5, b=1
方程的一个根为: -0.36193275451660156
a=-1, b=0
未找到方程的根
2. Iterative Method
a. Introduction to theory
The iterative method is a method of approaching the roots through continuous iteration, and is suitable for the roots of any function. Its basic idea is to start from an initial approximation and continuously update the position of the approximate root until the preset accuracy requirements are met. Specific steps are as follows:
- First, choose an initial approximate value x0.
- Then, according to the iterative formula x[i+1] = g(x[i]), the next approximate value x[i+1] is calculated.
- Repeat the above steps until the preset accuracy requirements are met, that is, the difference between the approximation and the root is small enough.
b. python implementation
def g(x):
return (-1) / (5 * x**3 + 3)
def iterative_method(initial_guess, tolerance=1e-6, max_iterations=100):
x = initial_guess
for _ in range(max_iterations):
x_next = g(x)
if abs(x_next - x) < tolerance:
return x_next
x = x_next
return None
# 调用迭代法求解方程的根
root = iterative_method(initial_guess=0)
if root is not None:
print("方程的一个根为:", root)
else:
print("未找到方程的根")
Note that the convergence of the iterative method is closely related to the choice of iteration function. For some functions, it may not converge or the convergence speed is very slow.
Output:
方程的一个根为: -0.36193292438672897
3. Newton's Method
a. Introduction to theory
Newton's iteration method is a fast-converging root-finding method suitable for roots of smooth functions. It uses a local linear approximation of the function to approximate the location of the root. Specific steps are as follows:
- First, choose an initial approximate value x0.
- Then, according to the Newton iterative formula x[i+1] = x[i] - f(x[i]) / f'(x[i]), the next approximate value x[i+1] is calculated.
- Repeat the above steps until the preset accuracy requirements are met, that is, the difference between the approximation and the root is small enough.
b. python implementation
def f(x):
return 5 * x**4 + 3 * x + 1
def f_prime(x):
return 20 * x**3 + 3
def newton_method(initial_guess, tolerance=1e-6, max_iterations=100):
x = initial_guess
for _ in range(max_iterations):
delta_x = f(x) / f_prime(x)
x -= delta_x
if abs(delta_x) < tolerance:
return x
return None
# 调用牛顿迭代法求解方程的根
root = newton_method(initial_guess=0)
if root is not None:
print("方程的一个根为:", root)
print(int(f(root)))
else:
print("未找到方程的根")
Note that Newton's method requires that the second-order derivative is not numbered and the first-order derivative is not 0.
Output:
方程的一个根为: -0.3619330489831212