Taylor Series using Recursion

Taylor Series using Recursion

Introduction

In this article, we are going to have a look at Taylor Series using Recursion.

Taylor Series is a mathematical concept that approximates any continuous and differentiable function as an infinite sum of powers of x. It is a powerful tool in calculus and is used extensively in engineering, physics, and other sciences. Taylor Series is represented by the following formula:

f(x) = f(a) + f'(a)*(x-a)/1! + f''(a)*(x-a)^2/2! + f'''(a)*(x-a)^3/3! + ...

where f(x) is the function to be approximated, a is the point around which the function is being approximated, and f'(a), f''(a), f'''(a), and so on are the derivatives of the function evaluated at a. The notation n! represents the factorial of n.

Taylor Series using Recursion

One way to calculate the Taylor Series of a function is to use recursion. Recursion is a technique in computer programming where a function calls itself with smaller subproblems until a base case is reached. In the context of Taylor Series, recursion can be used to calculate the derivatives of the function at a given point a.

Let’s consider the following C++ code that calculates the Taylor Series of the sin(x) function using recursion:

double taylor(double x, double a, int n) {
    static double term = x;
    if (n == 0) {
        return term;
    }
    term = (-(term * (x-a)*(x-a))) / ((2*n)*(2*n+1)) + term;
    return taylor(x, a, n-1);
}

The taylor function takes three parameters: x, the value of x for which the Taylor Series is being calculated; a, the point around which the Taylor Series is being approximated; and n, the number of terms in the Taylor Series to be calculated. The function uses a static variable term to store the current term of the Taylor Series. The base case of the recursion is when n is zero, in which case the function returns the current value of term.

The recursive step of the function calculates the next term of the Taylor Series by using the formula for the nth derivative of the sin(x) function evaluated at a. The new term is then added to the previous terms of the Taylor Series using the term variable. Finally, the function calls itself recursively with the smaller subproblem of n-1.

Taylor Series using Horner’s Rule

See also  Check whether the given number is a palindrome.

Another way to calculate the Taylor Series of a function is to use Horner’s Rule. Horner’s Rule is an algorithm that reduces the number of multiplications needed to evaluate a polynomial. It works by factoring out the x term from each subsequent term of the polynomial. Horner’s Rule is represented by the following formula:

f(x) = a_0 + x(a_1 + x(a_2 + x(a_3 + ... + x(a_{n-1} + xa_n)...)))

where a_0, a_1, a_2, …, a_n are the coefficients of the polynomial, and x is the value at which the polynomial is being evaluated.

Let’s consider the following C++ code that calculates the Taylor Series of the sin(x) function using Horner’s Rule:

double taylor(double x, double a, int n) {
    static double sum = 1.0;
    if (n == 0) {
        return sum;
    }
    double term = ((x-a)*(x-a))/(2*n*(2*n-1)) * taylor(x, a, n-1);
    sum = term + sum;
    return sum;
}

This function calculates the nth term of the Taylor Series for sin(x) centered at a. The base case of the recursion is when n is zero, in which case the function returns the current value of sum.

The recursive step of the function calculates the next term of the Taylor Series using the formula for the nth derivative of the sin(x) function evaluated at a. The function then adds this term to the previous sum of the Taylor Series using the sum variable. Finally, the function calls itself recursively with the smaller subproblem of n-1.

It is important to note that the static keyword is used to declare the sum variable, which means that the variable retains its value across function calls. This is necessary in recursive functions to ensure that the previous sum of the Taylor Series is not lost between recursive calls.

See also  Linear Search in DSA

To use this function, you can call it with the desired values of x, a, and n, and it will return the nth term of the Taylor Series.

The taylor function takes the same three parameters as before: x, a, and n. It uses a static variable sum to store the current sum of the Taylor Series. The base case of the recursion is when n is zero, in which case the function returns the current value of sum.

The recursive step of the function calculates the next term of the Taylor Series using the formula for the nth derivative of the sin(x) function evaluated at a. The function then adds this term to the previous sum of the Taylor Series using the sum variable. Finally, the function calls itself recursively with the smaller subproblem of n-1.

Horner’s Rule is used to evaluate the polynomial 1 + ((x-a)^2/2!) + ((x-a)^4/4!) + ((x-a)^6/6!) + .... The for loop in the function iterates from n down to 1, factoring out the x-a term from each subsequent term of the polynomial and adding it to the previous sum of the Taylor Series using the sum variable.

In the next article, we will look at the concepts Horner’s Rule and time complexity in depth.

Leave a Comment

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

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.