Evaluate Finite Difference Approximations in C

CCBeginner
Practice Now

Introduction

In this lab, you will learn how to evaluate finite difference approximations of derivatives in C. The lab covers the following steps:

  1. Define a mathematical function f(x) and set up the step size h for finite difference approximations.
  2. Compute forward and backward finite difference approximations of the derivative of f(x) at a given evaluation point x.
  3. Print the computed approximations and compare them to the true derivative.

By the end of this lab, you will have a solid understanding of how to implement finite difference methods in C and assess the accuracy of the approximations.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/operators("`Operators`") c/FunctionsGroup -.-> c/math_functions("`Math Functions`") subgraph Lab Skills c/variables -.-> lab-435175{{"`Evaluate Finite Difference Approximations in C`"}} c/operators -.-> lab-435175{{"`Evaluate Finite Difference Approximations in C`"}} c/math_functions -.-> lab-435175{{"`Evaluate Finite Difference Approximations in C`"}} end

Define f(x) and Step Size h

In this step, you will learn how to define a mathematical function and set up the step size for finite difference approximations in C.

First, create a new C file for your finite difference approximation program:

cd ~/project
nano finite_difference.c

Now, add the following code to define the function and step size:

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

// Define the function f(x)
double f(double x) {
    return x * x;  // Example function: f(x) = x^2
}

int main() {
    // Define the step size h
    double h = 0.0001;  // Small step size for accurate approximation

    // Point at which to evaluate the derivative
    double x = 2.0;

    printf("Function: f(x) = x^2\n");
    printf("Step size h: %f\n", h);
    printf("Evaluation point x: %f\n", x);

    return 0;
}

Example output:

Function: f(x) = x^2
Step size h: 0.000100
Evaluation point x: 2.000000

Let's break down the key components:

  1. f(x) function: We've defined a simple quadratic function f(x) = x^2. You can modify this function to represent any mathematical function you want to approximate.

  2. Step size h: This is a small value used in finite difference approximations. A smaller h typically provides more accurate results, but extremely small values can lead to numerical precision issues.

  3. Evaluation point x: This is the point at which we'll calculate the derivative approximation.

Compile the program to ensure it works:

gcc -o finite_difference finite_difference.c -lm
./finite_difference

Example output:

Function: f(x) = x^2
Step size h: 0.000100
Evaluation point x: 2.000000

Compute Forward/Backward Differences

In this step, you will learn how to compute forward and backward finite difference approximations of derivatives.

Open the previous file and modify the code to include derivative approximation methods:

cd ~/project
nano finite_difference.c

Update the code with forward and backward difference calculations:

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

// Define the function f(x)
double f(double x) {
    return x * x;  // Example function: f(x) = x^2
}

int main() {
    // Define the step size h
    double h = 0.0001;  // Small step size for accurate approximation

    // Point at which to evaluate the derivative
    double x = 2.0;

    // Forward Difference Approximation
    double forward_diff = (f(x + h) - f(x)) / h;

    // Backward Difference Approximation
    double backward_diff = (f(x) - f(x - h)) / h;

    printf("Forward Difference Approximation: %f\n", forward_diff);
    printf("Backward Difference Approximation: %f\n", backward_diff);

    return 0;
}

Compile and run the program:

gcc -o finite_difference finite_difference.c -lm
./finite_difference

Example output:

Forward Difference Approximation: 4.000100
Backward Difference Approximation: 3.999900

Let's break down the finite difference approximations:

  1. Forward Difference: Calculates the derivative using the point ahead of x

    • Formula: (f(x + h) - f(x)) / h
    • Approximates the rate of change moving forward
  2. Backward Difference: Calculates the derivative using the point behind x

    • Formula: (f(x) - f(x - h)) / h
    • Approximates the rate of change moving backward

Note that for the function f(x) = x^2, the true derivative is 2x (which is 4 at x = 2).
The approximations are very close to the actual derivative value.

Print the Approximations

In this step, you will enhance the program to print detailed approximation results and compare them with the actual derivative.

Open the previous file and modify the code to include comprehensive output:

cd ~/project
nano finite_difference.c

Update the code with detailed approximation printing:

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

// Define the function f(x)
double f(double x) {
    return x * x;  // Example function: f(x) = x^2
}

// Actual derivative function
double actual_derivative(double x) {
    return 2 * x;  // Derivative of x^2 is 2x
}

int main() {
    // Define multiple step sizes for comparison
    double step_sizes[] = {0.1, 0.01, 0.001, 0.0001};
    int num_steps = sizeof(step_sizes) / sizeof(step_sizes[0]);

    // Point at which to evaluate the derivative
    double x = 2.0;

    // Actual derivative value
    double true_derivative = actual_derivative(x);

    printf("Derivative Approximation Analysis\n");
    printf("--------------------------------\n");
    printf("Function: f(x) = x^2\n");
    printf("Evaluation Point: x = %f\n", x);
    printf("True Derivative: %f\n\n", true_derivative);

    printf("Step Size | Forward Diff | Backward Diff | Forward Error | Backward Error\n");
    printf("-----------------------------------------------------------------------\n");

    // Compute and print approximations for different step sizes
    for (int i = 0; i < num_steps; i++) {
        double h = step_sizes[i];

        // Forward Difference Approximation
        double forward_diff = (f(x + h) - f(x)) / h;

        // Backward Difference Approximation
        double backward_diff = (f(x) - f(x - h)) / h;

        // Calculate absolute errors
        double forward_error = fabs(forward_diff - true_derivative);
        double backward_error = fabs(backward_diff - true_derivative);

        printf("%9f | %11f | %12f | %11f | %12f\n",
               h, forward_diff, backward_diff, forward_error, backward_error);
    }

    return 0;
}

Compile and run the program:

gcc -o finite_difference finite_difference.c -lm
./finite_difference

Example output:

Derivative Approximation Analysis
--------------------------------
Function: f(x) = x^2
Evaluation Point: x = 2.000000
True Derivative: 4.000000

Step Size | Forward Diff | Backward Diff | Forward Error | Backward Error
-----------------------------------------------------------------------
    0.100000 |     4.100000 |      3.900000 |      0.100000 |       0.100000
    0.010000 |     4.010000 |      3.990000 |      0.010000 |       0.010000
    0.001000 |     4.001000 |      3.999000 |      0.001000 |       0.001000
    0.000100 |     4.000100 |      3.999900 |      0.000100 |       0.000100

Key observations:

  1. As step size h decreases, the approximation becomes more accurate
  2. Forward and backward differences converge to the true derivative
  3. Error reduces with smaller step sizes

Summary

In this lab, you learned how to define a mathematical function and set up the step size for finite difference approximations in C. You also learned how to compute forward and backward finite difference approximations of derivatives. The key steps covered include defining the function f(x) and the step size h, as well as implementing the forward and backward difference methods to approximate the derivative at a given evaluation point x. This provides a foundation for understanding and implementing finite difference techniques in numerical analysis and scientific computing.

Other C Tutorials you may like