How to resolve the algorithm Numerical integration step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Numerical integration step by step in the C programming language

Table of Contents

Problem Statement

Write functions to calculate the definite integral of a function ƒ(x) using all five of the following methods: Your functions should take in the upper and lower bounds (a and b), and the number of approximations to make in that range (n). Assume that your example already has a function that gives values for ƒ(x) . Simpson's method is defined by the following pseudo-code:

Demonstrate your function by showing the results for:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Numerical integration step by step in the C programming language

This C code provides numerical integration functions and demonstrates their usage for various test functions. Here's a detailed explanation:

  1. Numerical Integration Functions:

    • int_leftrect (Left Rectangle Rule): Performs numerical integration using the left rectangle method.
    • int_rightrect (Right Rectangle Rule): Performs numerical integration using the right rectangle method.
    • int_midrect (Midpoint Rectangle Rule): Performs numerical integration using the midpoint rectangle method.
    • int_trapezium (Trapezoidal Rule): Performs numerical integration using the trapezoidal rule.
    • int_simpson (Simpson's Rule): Performs numerical integration using Simpson's rule.
  2. Test Functions:

    • f1: Represents a cubic function (x^3).
    • f1a: Represents the antiderivative of f1 (x^4/4).
    • f2: Represents a logarithmic function (1/x).
    • f2a: Represents the antiderivative of f2 (log(x)).
    • f3: Represents a linear function (x).
    • f3a: Represents the antiderivative of f3 (x^2/2).
  3. Integration Routine:

    • INTG(F, A, B): Numerically integrates F over the interval [A, B] using the antiderivative F provided.
  4. Main Function:

    • It defines an array f of function pointers to the numerical integration functions.
    • It defines an array names of strings representing the names of the numerical integration methods.
    • It defines arrays rf and If of function pointers to the test functions and their antiderivatives, respectively.
    • It defines arrays ivals and approx specifying the integration intervals and the approximate number of trapezoids for each interval.
    • It loops through the test functions and numerical integration methods, calculating the integral and displaying the results along with the antiderivative's value.
  5. Code Execution:

    • The code demonstrates the usage of the numerical integration functions by computing the integrals of the test functions over specified intervals and displaying the results. The expected value is the value of the antiderivative at the upper bound minus the value at the lower bound.

In summary, this code provides a variety of numerical integration methods and demonstrates their application to different functions to estimate the definite integral. It helps illustrate the impact of the integration method on accuracy.

Source code in the c programming language

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

double int_leftrect(double from, double to, double n, double (*func)())
{
   double h = (to-from)/n;
   double sum = 0.0, x;
   for(x=from; x <= (to-h); x += h)
      sum += func(x);
   return h*sum;
}

double int_rightrect(double from, double to, double n, double (*func)())
{
   double h = (to-from)/n;
   double sum = 0.0, x;
   for(x=from; x <= (to-h); x += h)
     sum += func(x+h);
   return h*sum;
}

double int_midrect(double from, double to, double n, double (*func)())
{
   double h = (to-from)/n;
   double sum = 0.0, x;
   for(x=from; x <= (to-h); x += h)
     sum += func(x+h/2.0);
   return h*sum;
}

double int_trapezium(double from, double to, double n, double (*func)())
{
   double h = (to - from) / n;
   double sum = func(from) + func(to);
   int i;
   for(i = 1;i < n;i++)
       sum += 2.0*func(from + i * h);
   return  h * sum / 2.0;
}

double int_simpson(double from, double to, double n, double (*func)())
{
   double h = (to - from) / n;
   double sum1 = 0.0;
   double sum2 = 0.0;
   int i;

   double x;
   
   for(i = 0;i < n;i++)
      sum1 += func(from + h * i + h / 2.0);

   for(i = 1;i < n;i++)
      sum2 += func(from + h * i);

   return h / 6.0 * (func(from) + func(to) + 4.0 * sum1 + 2.0 * sum2);
}


/* test */
double f3(double x)
{
  return x;
}

double f3a(double x)
{
  return x*x/2.0;
}

double f2(double x)
{
  return 1.0/x;
}

double f2a(double x)
{
  return log(x);
}

double f1(double x)
{
  return x*x*x;
}

double f1a(double x)
{
  return x*x*x*x/4.0;
}

typedef double (*pfunc)(double, double, double, double (*)());
typedef double (*rfunc)(double);

#define INTG(F,A,B) (F((B))-F((A)))

int main()
{
     int i, j;
     double ic;
     
     pfunc f[5] = { 
       int_leftrect, int_rightrect,
       int_midrect,  int_trapezium,
       int_simpson 
     };
     const char *names[5] = {
       "leftrect", "rightrect", "midrect",
       "trapezium", "simpson" 
     };
     rfunc rf[] = { f1, f2, f3, f3 };
     rfunc If[] = { f1a, f2a, f3a, f3a };
     double ivals[] = { 
       0.0, 1.0,
       1.0, 100.0,
       0.0, 5000.0,
       0.0, 6000.0
     };
     double approx[] = { 100.0, 1000.0, 5000000.0, 6000000.0 };
          
     for(j=0; j < (sizeof(rf) / sizeof(rfunc)); j++)
     {
       for(i=0; i < 5 ; i++)
       {
         ic = (*f[i])(ivals[2*j], ivals[2*j+1], approx[j], rf[j]);
         printf("%10s [ 0,1] num: %+lf, an: %lf\n",
                names[i], ic, INTG((*If[j]), ivals[2*j], ivals[2*j+1]));
       }
       printf("\n");
     }
}


  

You may also check:How to resolve the algorithm Top rank per group step by step in the C programming language
You may also check:How to resolve the algorithm Rate counter step by step in the C programming language
You may also check:How to resolve the algorithm Gaussian elimination step by step in the C programming language
You may also check:How to resolve the algorithm Smarandache prime-digital sequence step by step in the C programming language
You may also check:How to resolve the algorithm Dijkstra's algorithm step by step in the C programming language