Error estimation for iterative method#

Example: Determine the number of terms necessary to approximate \(cos(x)\) to 8 significant figures at \(x = 0.3\pi\) using the following Maclaurin series approximation.

\[ cos(x) = 1 - \frac{x^2}{2!} + \frac{x^4}{4!} - \frac{x^6}{6!} + \frac{x^8}{8!} - ... \]

Assume that you don’t know the true value.

Solution:

To find whether an approximation is accurate upto \(n\) significant figures, following formula can be used:

\[ \epsilon_s = (0.5 \times 10^{2-n})\% \]

Note the percantage sign in the formula. To get 8 significant figures, error should be less than \(\epsilon_s = (5e-7)\% \text{ or } 5e-9\). We can compute the approximate error (\(\epsilon _a\)) while we do the iterations and use \(\epsilon_s\) as a stopping criteria. The code in the next block implements this.

Before running code blocks in notebook, it is important to import all the required packages as shown below.

import math

Following block of code defines two functions. First function is the Maclaurin series approximation of \(cos(x)\) and the second function finds the number of terms required to approximate \(cos(x)\) upto \(n\) significant figures.

def maclaurin_cos(x, num):
    """
        Function for computing maclaurin series approximation for cosine.
        Input:
        x - value at which cosine approximation is needed.
        num - number of terms in the series (includes the leading 1).
    """
    value = 0
    
    for i in range(num):
        power = 2*i
        value = value + (-1)**i * x**power / math.factorial(power)

    return value

def find_req_num_terms(func, x, num):
    """
        Function to find number of terms needed to achieve 
        accuracy upto certain significant numbers.
        
        Input:
        func - python function which computes a series
        x - value of x at which approximation is required
        num - number of significant figures required
    """
    
    # Tolerance for required significant figures
    tolerance = 0.5*10**(2-num)
    
    # Performing 1st iteration
    terms = 1
    approx_value = maclaurin_cos(x, terms)
    
    # Initial value of error for starting the iteration
    print("Number of terms: 1")
    print("Approx value: {}".format(approx_value))
    print("Approx errror: -\n")
    approx_error = 1
    
    # Performing iterations
    while abs(approx_error) >= tolerance:
        # Increment the number of terms
        terms += 1
        
        approx_error = approx_value
        
        # Calculate the approx value
        approx_value = func(x, terms)
        
        # Calculate percentage approx error
        approx_error = (approx_value - approx_error) / approx_value
        approx_error *= 100
        
        # Printing
        print("Number of terms: {}".format(terms))
        print("Approx value: {}".format(approx_value))
        print("Approx errror: {} %\n".format(abs(approx_error)))

Once the functions are defined, you can run that function to get the answer (as shown below).

find_req_num_terms(maclaurin_cos, 0.3*math.pi, 8)
Number of terms: 1
Approx value: 1.0
Approx errror: -

Number of terms: 2
Approx value: 0.5558678019509788
Approx errror: 79.89888899666624 %

Number of terms: 3
Approx value: 0.5887433701749547
Approx errror: 5.5840235133699 %

Number of terms: 4
Approx value: 0.5877699636164597
Approx errror: 0.1656101227945979 %

Number of terms: 5
Approx value: 0.5877854036591176
Approx errror: 0.002626816277120294 %

Number of terms: 6
Approx value: 0.5877852512720046
Approx errror: 2.592564420399015e-05 %

Number of terms: 7
Approx value: 0.5877852522974596
Approx errror: 1.7446081526780443e-07 %