Solving Nonlinear Equation Using Regula Falsi Method

How to solve nonlinear equation in science and engineering numerically using Regula Falsi method
June 27, 2024 by
Solving Nonlinear Equation Using Regula Falsi Method
Hamed Mohammadi
| No comments yet

Introduction

In many science and engineering there is occasions that we need to solve nonlinear equation, that is find the roots of an equation, which is the points where the equation graph meets the x axis. The regula falsi method (also called false position and linear interpolation methods) is a bracketing method for finding a numerical solution of a nonlinear equation of the form f(x) = 0.


Formulation


The regula falsi method is a bracketing method for finding a numerical solution of a nonlinear equation of the form f(x) = 0 when it is known that, within a given interval [ a , b ] , f ( x ) is continuous and the equation has a solution. The solution starts by finding an initial interval [ a 1 , b 1 ] that brackets the solution. The values of the function at the endpoints are f ( a 1 ) and f ( b 1 ) . The endpoints are then connected by a straight line, and the first estimate of the numerical solution, x N S 1 , is the point where the straight line crosses the x-axis. This is in contrast to the bisection method, where the midpoint of the interval was taken as the solution. For the second iteration a new interval, [ a 2 , b 2 ] is defined. The new interval is a subsection of the first interval that contains the solution. It is either [ a 1 , x N S 1 ] or [ x N S 1 , b 1 ] . The endpoints of the second interval are next connected with a straight line, and the point where this new line crosses the x-axis is the second estimate of the solution, x N S 2 . For the third iteration, a new sub-interval [ a 3 , b 3 ] is selected, and the iterations continue in the same way until the numerical solution is accurate enough.
For a given interval [ a , b ] , the equation of a straight line that connects point ( b f ( b ) ) to point ( a , f ( a ) ) is given by:
y = f ( b ) - F ( a ) b - a ( x - b ) + f ( b )
The point x N S where the line intersects the x-axis is determined by substituting y = 0 in this line equation, and solving the equation for x:
x N S = a f ( b ) - b f ( a ) f ( b ) - f ( a )

Algorithm For Regula Falsi Method

The algorithm for finding a solution with the regula falsi method is almost the same as for bisection method:
  1. Choose the first interval by finding points a and b such that a solution exists between them. This means that f ( a ) and f ( b ) have different signs such that f ( a ) f ( b ) < 0 . The points can be determined by looking at a plot of f ( x ) versus x .
  2. Calculate the first estimate of the numerical solution x N s 1 by using the formula in last section.
  3. Determine whether the true solution is between a and x N S 1 , or between x N S 1 and b . This is done by checking the sign of the product f ( a ) f ( x N S 1 ) :
    If f ( a ) f ( x N S 1 ) < 0 , the true solution is between a and x N S 1 .
    If f ( a ) f ( x N S 1 ) > 0 , the true solution is between x N S 1 and b .
  4. Select the sub-interval that contains the true solution as the new interval [ a , b ] , and go to step 2.
    Step 2 through 4 are repeated until a specified tolerance or error bound is attained.


When Should the Bisection Process Stopped?

Ideally, the regula falsi method should be stopped when the true solution is obtained. This means that the value of x N S is such that f ( x N S ) = 0 . In reality, this true solution generally cannot be found computationally. In practice, therefor, the process is stopped when the estimated error, according to one of measures, is smaller than some predetermined value. The choice of termination criteria may depend on the problem that is actually solved.


An Example

Here’s an example of a nonlinear equation that can be solved using the Regula Falsi method along with a Python program implementation:
f ( x ) = x 3 - x - 1 = 0
This equation represents a cubic function where we want to find the value of x that makes the function equal to zero.


The Python Program

The python program for solving this equation is listed here:

def regula_falsi(f, a, b, tol):
  """
  This function implements the Regula Falsi method to find an approximate root of a nonlinear equation.

  Args:
      f: The function for which we want to find the root.
      a: Lower bound of the initial interval.
      b: Upper bound of the initial interval.
      tol: Tolerance level for the approximation.

  Returns:
      The approximate root of the equation within the given tolerance.
  """
  fa = f(a)
  fb = f(b)
  if fa * fb > 0:
    print("The given interval does not contain a root.")
    return None
  
  iterations = 0
  while abs(b - a) > tol:
    c = (a * fb - b * fa) / (fb - fa)
    fc = f(c)
    if fa * fc < 0:
      b = c
    else:
      a = c
    iterations += 1
  
  print(f"Root found approximately after {iterations} iterations: x = {c}")
  return c

# Define the function
def f(x):
  return x**3 - x - 1

# Set initial interval and tolerance
a = 1
b = 2
tol = 0.001

# Find the approximate root
root = regula_falsi(f, a, b, tol)

if root:
  print(f"f({root}) = {f(root):.4f}")


Explanation:


  1. The regula_falsi function takes the equation (f), the initial interval (a, b), and the tolerance (tol) as input.
  2. It checks if the function values at a and b have the same sign. If they do, there’s no root in the interval (since the function goes from positive to negative or vice versa when the root is crossed).
  3. It iteratively calculates a new point (c) within the interval based on the function values at a and b. This is essentially the x-intercept of the line connecting the points (a, f(a)) and (b, f(b)).
  4. It checks the signs of f(a) and f(c). If they are opposite, the root lies between a and c, so we update the upper bound (b) to c. Otherwise, the root lies between c and b, so we update the lower bound (a) to c.
  5. This process continues until the difference between a and b becomes less than the tolerance (tol), indicating we’re close enough to the root.
  6. The function then prints the number of iterations and the approximate root along with its function value (f(root)) for verification (ideally close to zero).


This program demonstrates how to use the Regula Falsi method to solve the given cubic equation ( f ( x ) = x 3 - x - 1 = 0 ). You can modify the code to try the method with other nonlinear equations by changing the definition of the f function.

Additional Notes About Regula falsi Method


  • The method always converge to an answer, if a root is trapped in the interval [ a , b ] .
  • Frequently, the function in the interval [ a , b ] is either concave up or concave down. In this case, one of the endpoints of the interval remains the same during the iteration, while the other endpoint advances to ward the root. In other words, the numerical solution advances toward the root only from one side. The convergence toward the solution could be faster if the other endpoint would also move toward the root. Several modifications have been introduced to the regula falsi method that makes the sub-interval in successive iteration approach the root from both sides. We will discuss these methods in later blog posts.


Solving Nonlinear Equation Using Regula Falsi Method
Hamed Mohammadi June 27, 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