Approximate a Derivative at a Point in C

CBeginner
Practice Now

Introduction

In this lab, we will learn how to approximate the derivative of a function at a specific point using the C programming language. We will start by defining a simple quadratic function, then use a small value h to compute the numerical derivative using the difference method. Finally, we will print the approximate derivative. This lab aims to provide a practical understanding of derivative approximation, which is a fundamental concept in calculus and analytical geometry.

Define the Function f(x)

In this step, we will define a mathematical function f(x) in C programming that will be used to demonstrate derivative approximation. We'll create a simple quadratic function to illustrate the concept.

First, let's create a new C file in the ~/project directory:

cd ~/project
nano derivative_approximation.c

Now, let's write the initial code for our function:

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

// Define a quadratic function f(x) = x^2 + 2x + 1
double f(double x) {
    return x * x + 2 * x + 1;
}

int main() {
    double x = 2.0;  // Point at which we'll approximate the derivative
    printf("Function f(x) = x^2 + 2x + 1\n");
    printf("Evaluated at x = %.2f: f(x) = %.2f\n", x, f(x));

    return 0;
}

Let's compile and run the code to verify our function:

gcc derivative_approximation.c -o derivative_approximation -lm
./derivative_approximation

Example output:

Function f(x) = x^2 + 2x + 1
Evaluated at x = 2.00: f(x) = 9.00
Code Explanation
  • We define a quadratic function f(x) = x^2 + 2x + 1
  • The function takes a double input x and returns a double result
  • In the main() function, we demonstrate evaluating the function at x = 2
  • We use printf() to display the function details and its value

Use a Small h and Compute (f(x+h)-f(x))/h

In this step, we will modify our previous code to approximate the derivative using the difference method. We'll introduce a small value h to calculate the numerical derivative.

Let's update the derivative_approximation.c file:

nano ~/project/derivative_approximation.c

Replace the previous main() function with the following code:

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

// Quadratic function f(x) = x^2 + 2x + 1
double f(double x) {
    return x * x + 2 * x + 1;
}

// Approximate derivative using difference method
double approximate_derivative(double x, double h) {
    return (f(x + h) - f(x)) / h;
}

int main() {
    double x = 2.0;  // Point of derivative approximation
    double h = 0.0001;  // Small value for difference method

    double approx_derivative = approximate_derivative(x, h);

    printf("Function: f(x) = x^2 + 2x + 1\n");
    printf("Approximating derivative at x = %.2f\n", x);
    printf("Step size h = %.6f\n", h);
    printf("Approximate derivative: %.4f\n", approx_derivative);

    return 0;
}

Compile and run the updated code:

gcc derivative_approximation.c -o derivative_approximation -lm
./derivative_approximation

Example output:

Function: f(x) = x^2 + 2x + 1
Approximating derivative at x = 2.00
Step size h = 0.000100
Approximate derivative: 5.0001
Code Explanation
  • We introduce a new function approximate_derivative() that calculates the derivative using the difference method
  • h is a small value (0.0001) that helps approximate the instantaneous rate of change
  • The formula (f(x+h) - f(x)) / h approximates the derivative at point x
  • We print the approximated derivative value

In this step, we will expand our derivative approximation program to compare the numerical approximation with the analytical derivative and print the results in a more informative way.

Update the derivative_approximation.c file:

nano ~/project/derivative_approximation.c

Replace the previous code with the following:

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

// Quadratic function f(x) = x^2 + 2x + 1
double f(double x) {
    return x * x + 2 * x + 1;
}

// Analytical derivative of f(x)
double analytical_derivative(double x) {
    return 2 * x + 2;
}

// Approximate derivative using difference method
double approximate_derivative(double x, double h) {
    return (f(x + h) - f(x)) / h;
}

int main() {
    double x = 2.0;  // Point of derivative approximation
    double h_values[] = {1e-1, 1e-2, 1e-3, 1e-4, 1e-5};
    int num_h = sizeof(h_values) / sizeof(h_values[0]);

    double true_derivative = analytical_derivative(x);

    printf("Function: f(x) = x^2 + 2x + 1\n");
    printf("Derivative Point: x = %.2f\n", x);
    printf("Analytical Derivative: %.4f\n\n", true_derivative);

    printf("Derivative Approximation Results:\n");
    printf("-----------------------------------\n");
    printf("Step Size (h)   Approximate Derivative   Error\n");
    printf("-----------------------------------\n");

    for (int i = 0; i < num_h; i++) {
        double h = h_values[i];
        double approx_derivative = approximate_derivative(x, h);
        double error = fabs(true_derivative - approx_derivative);

        printf("%.1e             %.4f               %.6f\n",
               h, approx_derivative, error);
    }

    return 0;
}

Compile and run the updated code:

gcc derivative_approximation.c -o derivative_approximation -lm
./derivative_approximation

Example output:

Function: f(x) = x^2 + 2x + 1
Derivative Point: x = 2.00
Analytical Derivative: 6.0000

Derivative Approximation Results:
-----------------------------------
Step Size (h)   Approximate Derivative   Error
-----------------------------------
1.0e-01             6.200000               0.200000
1.0e-02             6.020000               0.020000
1.0e-03             6.002000               0.002000
1.0e-04             6.000200               0.000200
1.0e-05             6.000020               0.000020
Code Explanation
  • Added analytical_derivative() function to calculate the true derivative
  • Created an array of different step sizes h to demonstrate convergence
  • Used a loop to print approximations with different step sizes
  • Calculated and displayed the error between analytical and numerical derivatives
  • Demonstrates how smaller h values lead to more accurate approximations

Summary

In this lab, we first defined a quadratic function f(x) = x^2 + 2x + 1 in C programming. We then introduced a small value h to approximate the derivative of the function using the difference method, (f(x+h)-f(x))/h. Finally, we printed the approximate derivative value.