Approximate e Using Its Series in C

CCBeginner
Practice Now

Introduction

In this lab, we will explore how to approximate the mathematical constant e using its series expansion in C. The series for e is defined as the sum of the reciprocals of factorials: e = 1 + 1/1! + 1/2! + 1/3! + ... We will first create a C program to calculate this series approximation, and then modify it to sum the terms until a desired level of accuracy is reached. This allows us to control the precision of our calculation and obtain a more accurate approximation of e.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/ControlFlowGroup -.-> c/while_loop("`While Loop`") c/ControlFlowGroup -.-> c/for_loop("`For Loop`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") c/FunctionsGroup -.-> c/recursion("`Recursion`") c/FunctionsGroup -.-> c/math_functions("`Math Functions`") subgraph Lab Skills c/while_loop -.-> lab-435336{{"`Approximate e Using Its Series in C`"}} c/for_loop -.-> lab-435336{{"`Approximate e Using Its Series in C`"}} c/function_declaration -.-> lab-435336{{"`Approximate e Using Its Series in C`"}} c/recursion -.-> lab-435336{{"`Approximate e Using Its Series in C`"}} c/math_functions -.-> lab-435336{{"`Approximate e Using Its Series in C`"}} end

Use e = ∑(1/n!)

In this step, we will explore how to approximate the mathematical constant e using its series expansion in C. The series for e is defined as the sum of the reciprocals of factorials: e = 1 + 1/1! + 1/2! + 1/3! + ...

First, let's create a C program to calculate this series approximation. Open a new file in the project directory:

cd ~/project
nano e_approximation.c

Now, enter the following C code:

#include <stdio.h>

double factorial(int n) {
    if (n == 0 || n == 1) return 1;
    double result = 1;
    for (int i = 2; i <= n; i++) {
        result *= i;
    }
    return result;
}

double approximate_e(int terms) {
    double e = 0;
    for (int n = 0; n < terms; n++) {
        e += 1.0 / factorial(n);
    }
    return e;
}

int main() {
    int num_terms = 10;
    double e_approximation = approximate_e(num_terms);
    printf("Approximation of e with %d terms: %f\n", num_terms, e_approximation);
    return 0;
}

Let's compile and run the program:

gcc e_approximation.c -o e_approximation
./e_approximation

Example output:

Approximation of e with 10 terms: 2.718282

Sum Terms Until Desired Accuracy

In this step, we will modify our previous program to calculate the approximation of e by summing terms until we reach a desired level of accuracy. This approach allows us to control the precision of our calculation.

Let's update the previous C file:

cd ~/project
nano e_approximation.c

Replace the previous content with the following improved code:

#include <stdio.h>
#include <math.h>

double factorial(int n) {
    if (n == 0 || n == 1) return 1;
    double result = 1;
    for (int i = 2; i <= n; i++) {
        result *= i;
    }
    return result;
}

double approximate_e_with_accuracy(double desired_accuracy) {
    double e = 0;
    double term = 1;
    int n = 0;

    while (fabs(term) > desired_accuracy) {
        e += term;
        n++;
        term = 1.0 / factorial(n);
    }

    return e;
}

int main() {
    double accuracy = 1e-6;  // Desired accuracy level
    double e_approximation = approximate_e_with_accuracy(accuracy);

    printf("Approximation of e with accuracy %e: %f\n", accuracy, e_approximation);
    printf("Standard math.h e value: %f\n", M_E);

    return 0;
}

Compile and run the updated program:

gcc e_approximation.c -o e_approximation -lm
./e_approximation

Example output:

Approximation of e with accuracy 1.000000e-06: 2.718282
Standard math.h e value: 2.718282

This version of the program continues adding terms to the series until the next term is smaller than the specified accuracy threshold. The fabs() function ensures we work with the absolute value of the term.

Key points to note:

  • We use 1e-6 as our desired accuracy level
  • The program dynamically determines how many terms are needed
  • We compare our approximation with the standard math library value of e

Print the Approximation

In this final step, we will enhance our program to print the approximation of e with different levels of accuracy and provide a more detailed output.

Update the C file once more:

cd ~/project
nano e_approximation.c

Replace the previous content with the following comprehensive code:

#include <stdio.h>
#include <math.h>

double factorial(int n) {
    if (n == 0 || n == 1) return 1;
    double result = 1;
    for (int i = 2; i <= n; i++) {
        result *= i;
    }
    return result;
}

void print_e_approximations() {
    double accuracies[] = {1e-1, 1e-3, 1e-6, 1e-9};
    int num_accuracies = sizeof(accuracies) / sizeof(accuracies[0]);

    printf("e Approximation with Different Accuracy Levels:\n");
    printf("---------------------------------------------\n");

    for (int i = 0; i < num_accuracies; i++) {
        double e = 0;
        double term = 1;
        int n = 0;
        int terms_used = 0;

        while (fabs(term) > accuracies[i]) {
            e += term;
            n++;
            term = 1.0 / factorial(n);
            terms_used++;
        }

        printf("Accuracy: %e\n", accuracies[i]);
        printf("Approximation: %.10f\n", e);
        printf("Terms Used: %d\n", terms_used);
        printf("Difference from math.h e: %.10f\n\n", fabs(e - M_E));
    }
}

int main() {
    print_e_approximations();
    return 0;
}

Compile and run the program:

gcc e_approximation.c -o e_approximation -lm
./e_approximation

Example output:

e Approximation with Different Accuracy Levels:
---------------------------------------------
Accuracy: 1.000000e-01
Approximation: 2.7000000000
Terms Used: 4
Difference from math.h e: 0.0182818284

Accuracy: 1.000000e-03
Approximation: 2.7182500000
Terms Used: 8
Difference from math.h e: 0.0000318284

Accuracy: 1.000000e-06
Approximation: 2.7182818000
Terms Used: 14
Difference from math.h e: 0.0000000284

Accuracy: 1.000000e-09
Approximation: 2.7182818285
Terms Used: 21
Difference from math.h e: 0.0000000000

Key improvements in this version:

  • Prints approximations at multiple accuracy levels
  • Shows number of terms used for each approximation
  • Calculates and displays the difference from the standard math library value of e

Summary

In this lab, we learned how to approximate the mathematical constant e using its series expansion in C. We first implemented a function to calculate the factorial of a number, and then used it to compute the approximation of e by summing the terms of the series until a desired number of terms. We then modified the program to continue summing the terms until a desired level of accuracy is reached, allowing us to control the precision of the calculation. The final program outputs the approximation of e to the desired accuracy.

Other C Tutorials you may like