Central Difference Method Python

Central Difference Method Python | The central difference method is a numerical method used to approximate the derivative of a function at a given point. It is based on the idea that the derivative of a function can be approximated by the slope of a secant line passing through two points on the function. In the central difference method, the two points are symmetrically placed around the point where the derivative is to be estimated. This method is commonly used in numerical analysis, scientific computing, and engineering applications.

The formula for the central difference method is:-
f'(x) ≈ (f(x + h) – f(x – h)) / (2 * h)

Where h is a small step size. The central difference method is a second-order method, which means that the error in the approximation is proportional to h^2. This makes it more accurate than first-order methods, such as the forward difference and backward difference methods.

In Python, we can implement the central difference method as a function that takes as input the function to be differentiated, the point at which the derivative is to be estimated, and the step size. The function then computes the approximation of the derivative using the formula above.

Central Difference Method Implementation in Python

In Python, we can implement the central difference method as a function. Here is an example of implementation.

def central_difference(f, x, h):
    """
    Computes the derivative of a function using the central difference method.

    Args:
        f (function): The function to be differentiated.
        x (float): The point at which the derivative is to be estimated.
        h (float): The step size.

    Returns:
        The estimate of the derivative of f at x.
    """
    return (f(x + h) - f(x - h)) / (2 * h)

We can then use this function to estimate the derivative of a function at a given point. For example, suppose we want to estimate the derivative of the function f(x) = x^2 at x = 1 using a step size of h = 0.01.

def central_difference(f, x, h):
    """
    Computes the derivative of a function using the central difference method.

    Args:
        f (function): The function to be differentiated.
        x (float): The point at which the derivative is to be estimated.
        h (float): The step size.

    Returns:
        The estimate of the derivative of f at x.
    """
    return (f(x + h) - f(x - h)) / (2 * h)


def f(x):
    return x**2

x = 1
h = 0.01
dfdx = central_difference(f, x, h)
print(dfdx)

Output:-

2.0000000000000018

This will output the estimate of the derivative of f at x = 1, which should be approximately 2. The output should be approximately 2, which is the exact derivative of f(x) = x^2 at x = 1.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *