Newton-Raphson Method for Solving Nonlinear Equations

Unveiling Roots with Newton's Method: An Introduction to Solving Nonlinear Equations Using Newton-Raphson Method
July 5, 2024 by
Newton-Raphson Method for Solving Nonlinear Equations
Hamed Mohammadi
| No comments yet

1 Introduction

Nonlinear equations, unlike their linear counterparts, can’t be solved with simple algebra. But fear not, for Newton’s method comes to the rescue! This iterative technique is a powerful tool for finding the roots (also called zeros) of these nonlinear equations.

Imagine a curvy graph and you’re looking for the x-value where the graph intersects the x-axis (i.e., the root). Newton’s method works by taking an initial guess for this x-value. Then, it cleverly uses the concept of the tangent line: it draws a line tangent to the curve at your guess, and finds where this tangent line meets the x-axis. This new x-axis intercept becomes a better approximation for the root.

The beauty lies in the repetition. You keep using the same idea, drawing a tangent line at your new guess, and iteratively getting closer and closer to the actual root. With each step, the approximation gets refined, leading to a more accurate solution.

Newton’s method or Newton-Raphson method is a numeric method for finding a numerical solution of an equation of the form f(x) = 0 where f(x) is continuous and differentiable and also it is know that the equation have a solution approximate to a given point. The method is shown in figure below.


The procedure for finding the solution starts by choosing an start point x 1 as the first estimate of the solution. The second estimate x 2 is obtained by taking the tangent line to f ( x ) at the point ( x 1 , f ( x 1 ) ) and finding the intersection point of the tangent line with the x-axis. The next estimate x 3 is the intersection of the tangent line to f ( x ) at the point ( x 2 , f ( x 2 ) ) with the x-axis, and so on. Formulating it in mathematical terms, the first line in the first iteration has the slope of f ( x 1 ) , of the the tangent at point ( x 1 , f ( x 1 ) , and its slope is:
f ( x 1 ) = f ( x 1 ) - 0 x 1 - x 2
Solving the above for x 2 gives:
x 2 = x 1 - f ( x 1 ) f ( x 1 )
And generally for determining the next solution x i + 1 from the present x i we have:
x i + 1 = x i - f ( x i ) f ( x i )
This is the general iteration formula for Newton-Raphson method. It is called an iteration formula because the solution is found by repeated application of this equation for each iteration.


2 Algorithm for Newton-Raphson Method

Algorithm for Newton-Raphson method is simple:


  1. Choose a point x 1 as an initial guess of the solution.
  2. For each iteration until the error is smaller than a specified value, calculate the next solution using the formula in the last section.


3 When Should Iterations Stop?


Ideally, the iterations should be stopped when an exact solution is obtained. This means that the value of x is such that f ( x ) = 0 . Generally, this exact solution cannot be found computationally. In practice, the iterations are stopped when as estimated error is smaller than some predetermined value. A tolerance in the solution, as in the bisection method, cannot be calculated since bounds are not known. Two error estimates are typically used for Newton-Raphson method.
Estimated relative error: The iterations are stopped when the estimated relative error is smaller than a specified value ϵ :
| x i + 1 - x i x i | ϵ
Tolerance in f ( x ) : The iterations are stopped when the absolute value of f ( x i ) is smaller than some number γ :
| f ( x i ) | γ

Sample Problem: Finding the Square Root

Let's use Newton's method to find the square root of a number (say, 2). The square root of a number x satisfies the equation x^2 - y = 0, where y is the desired square root. We can rewrite this as f(y) = y^2 - 2 = 0.

Here's the Python program to solve it:

def f(y):
  """Function representing the equation (y^2 - 2 = 0)."""
  return y**2 - 2

def df(y):
  """Derivative of the function f(y) (2*y)."""
  return 2*y

def newton_method(f, df, x0, tol, max_iter):
  """
  Implements Newton's method for finding the root of a function.

  Args:
      f: The function for which we want to find the root.
      df: The derivative of the function f.
      x0: The initial guess for the root.
      tol: The tolerance level for convergence.
      max_iter: The maximum number of iterations allowed.

  Returns:
      The root (if found) or None.
  """
  for i in range(max_iter):
    x_new = x0 - f(x0) / df(x0)
    if abs(x_new - x0) < tol:
      return x_new
    x0 = x_new
  return None

# Define initial guess, tolerance, and max iterations
x_guess = 1.0  # Initial guess (can be adjusted)
tolerance = 1e-6
max_iterations = 10

# Solve for the square root using Newton's method
root = newton_method(f, df, x_guess, tolerance, max_iterations)

if root:
  print(f"Square root of 2 (approximately): {root:.6f}")
else:
  print(f"No root found within tolerance or {max_iterations} iterations.")


This program defines the function f(y) and its derivative df(y) representing the equation y^2 - 2 = 0. The newton_method function remains the same. It then defines the initial guess, tolerance, and maximum iterations. Finally, it calls the newton_method to find the root and prints the result.

This example demonstrates how to apply Newton's method to solve a simple nonlinear equation. You can modify the f(y) and df(y) functions to solve other nonlinear equations following the same approach.

4 Consideration When Using Newton-Raphson Method

The method, when successful, works well and converges fast. When it does not converge, it is usually because the starting point is not close enough to the solution. Convergence problems typically occur when the value of f ( x ) is close to zero in the proximity of the solution. It is possible to show that Newton-Raphson method converges if the function f ( x ) and its first and second derivatives f ( x ) and f ( x ) are all continuous, if f ( x ) is not zero at the solution, and if the starting value x 1 is near the actual solution.
A function f ( x ) , which is the derivative of the function f ( x ) , has to be substituted in the iteration formula. In many cases, it is simple to write the derivative, but sometimes it can be difficult to determine. When an expression for the derivative is not available, it might be possible to determine the slope numerically or to use another method such as the secant method that is similar to Newton-Raphson method but does not need an expression for derivative.



Newton-Raphson Method for Solving Nonlinear Equations
Hamed Mohammadi July 5, 2024
Share this post
Archive

Please visit our blog at:

https://zehabsd.com/blog

A platform for Flash Stories:

https://readflashy.com

A platform for Persian Literature Lovers:

https://sarayesokhan.com

Sign in to leave a comment