Jump to content

Trapezoidal rule problem

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Yuriy M

Yuriy M

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
Hi guys! :)

I've been working on a program that implements the trapezoidal rule to help approximate the area under a curve. My program makes use of function parameters for the purposes of calling the functions that represent each curve.

Here is the code:

double trap(double, double, int, double(double));       /* Trapezoid calculation */
double g(double);                                       /* Function G */
double h(double);                                       /* Function H */

int main(void)
{
    /* Declare variables */
    
    FILE   *inp;                                                     /* The input file */
    double area,                                                     /* The area under a graphical curve */
           g_left,                                                   /* The leftmost line of function g */
           g_right,                                                  /* The rightmost line of function g */
           h_left,                                                   /* The leftmost line of function h */
           h_right;                                                  /* The rightmost line of function h */
    int    length_subinterval,                                       /* The subinterval of the length */
           input_status;                                             /* The current status value */
           
    /* Initialization */
    
    g_left = 0;
    g_right = 3.14159;
    h_left = -2;
    h_right = 2;
    
    /* Open file */
    
    inp = fopen("trap_values.dat", "r");
    
    /* Collect subinterval data */
    input_status = fscanf(inp, "%d", &length_subinterval);
    
    /* While subinterval data can be collected, call function to calculate the area of the trapezoid and display the results */
   
    while (input_status != EOF)
    {
     area = trap(g_left, g_right, length_subinterval, g);
     printf("\nThe area for function g with subinterval value %d is %.2f", length_subinterval, area);
     area = trap(h_left, h_right, length_subinterval, h);
     printf("\nThe area for function h with subinterval value %d is %.2f\n", length_subinterval, area);
     
     /* Collect subinterval data */
     input_status = fscanf(inp, "%d", &length_subinterval);
    }

    /* Close file */
    
    fclose(inp);
    
    return 0;   
}

double trap(double a, double b, int n, double f(double farg))
{
    /* Declare variables */
    
    int    counter;                                                  /* Counts the values in the summation */
    double h_length,                                                 /* The length between two line segments */
           sum = 0.0,                                                /* The sum result */
           area_result;                                              /* The approximate area of the trapezoid */
    
    /* Calculate length */
    h_length = (b - a) / n;
    
    /* Perform summation operation */
    
    for (counter = 1; counter <= n + 1; counter++)
    {
     if (counter == 1 || counter == n + 1)
      sum += f(counter);
     else
      sum += 2 * f(counter);  
    }
      
    /* Calculate approximate area of the trapezoid */
    area_result = (h_length * sum) / 2;
    
    return area_result;
}

double g(double x)
{
    return fabs(pow(x, 2) * sin(x));      
}

double h(double x)
{
    return (sqrt(fabs(4 - (pow(x, 2)))));   
}


The program displays what appears to be decent output but the issue is that I'm working under a different formula for calculating the trapezoidal rule than what is given in my book.

For the record, I have n values of 2, 4, 8, 16, 32, 64 and 128. The formula given in the book to calculate the trapezoidal rule is h/2 (f(a) + f(b) + 2 * the summation notation with 1 as the starting index, n-1 as the stopping point and xi as the typical element.

Function g is represented as x^2 * sin x and function h is represented as the square root of 4 - x^2.

The alternate example I am using to calculate the trapezoidal rule is from this YouTube tutorial: The Trapezoid Rule for Approximating Integrals - YouTube

I also tried creating an extra function for 1 + x^2 as a reference point to determine the accuracy of my coding calculations. I have since deleted that which is why you don't see it in the source code that I provided.

Output I get is the following:

The area for function g with subinterval value 2 is 7.37
The area for function h with subinterval value 2 is 3.97

The area for function g with subinterval value 4 is 23.11
The area for function h with subinterval value 4 is 8.86

The area for function g with subinterval value 8 is 64.27
The area for function h with subinterval value 8 is 17.82

The area for function g with subinterval value 16 is 207.57
The area for function h with subinterval value 16 is 34.59

The area for function g with subinterval value 32 is 751.40
The area for function h with subinterval value 32 is 67.13

The area for function g with subinterval value 64 is 2904.09
The area for function h with subinterval value 64 is 131.48

The area for function g with subinterval value 128 is 11214.06
The area for function h with subinterval value 128 is 259.70

Just want to verify if the output matches the approximate area as represented in the trapezoidal rule. Sorry if that was a mouthful. :P
For $1000: Something that is a miserable pile of secrets.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
you need to call f(a+counter*h_length), not f(counter).
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Yuriy M

Yuriy M

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
Okay. Here is the revised code:

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

double trap(double, double, int, double(double));       /* Trapezoid calculation */
double g(double);                                       /* Function G */
double h(double);                                       /* Function H */

int main(void)
{
    /* Declare variables */
    
    FILE   *inp;                                                     /* The input file */
    double area,                                                     /* The area under a graphical curve */
           g_left,                                                   /* The leftmost line of function g */
           g_right,                                                  /* The rightmost line of function g */
           h_left,                                                   /* The leftmost line of function h */
           h_right;                                                  /* The rightmost line of function h */
    int    length_subinterval,                                       /* The subinterval of the length */
           input_status;                                             /* The current status value */
           
    /* Initialization */
    
    g_left = 0;
    g_right = 3.14159;
    h_left = -2;
    h_right = 2;
    
    /* Open file */
    
    inp = fopen("trap_values.dat", "r");
    
    /* Collect subinterval data */
    input_status = fscanf(inp, "%d", &length_subinterval);
    
    /* While subinterval data can be collected, call function to calculate the area of the trapezoid and display the results */
   
    while (input_status != EOF)
    {
     area = trap(g_left, g_right, length_subinterval, g);
     printf("\nThe area for function g with subinterval value %d is %.2f", length_subinterval, area);
     area = trap(h_left, h_right, length_subinterval, h);
     printf("\nThe area for function h with subinterval value %d is %.2f\n", length_subinterval, area);
     
     /* Collect subinterval data */
     input_status = fscanf(inp, "%d", &length_subinterval);
    }

    /* Close file */
    
    fclose(inp);
    
    return 0;   
}

double trap(double a, double b, int n, double f(double farg))
{
    /* Declare variables */
    
    int    counter;                                                  /* Counts the values in the summation */
    double h_length,                                                 /* The length between two line segments */
           sum = 0.0,                                                /* The sum result */
           area_result;                                              /* The approximate area of the trapezoid */
    
    /* Calculate length */
    h_length = (b - a) / n;
    
    /* Perform summation operation */
    
    for (counter = 1; counter <= n + 1; counter++)
    {
     if (counter == 1 || counter == n + 1)
      sum += f(a + counter * h_length);
     else
      sum += 2 * f(a + counter * h_length);  
    }
      
    /* Calculate approximate area of the trapezoid */
    area_result = (h_length * sum) / 2;
    
    return area_result;
}

double g(double x)
{
    return fabs(pow(x, 2) * sin(x));      
}

double h(double x)
{
    return (sqrt(fabs(4 - (pow(x, 2)))));   
}


And the output...


The area for function g with subinterval value 2 is 19.38
The area for function h with subinterval value 2 is 5.46

The area for function g with subinterval value 4 is 9.47
The area for function h with subinterval value 4 is 5.72

The area for function g with subinterval value 8 is 6.67
The area for function h with subinterval value 8 is 6.04

The area for function g with subinterval value 16 is 6.05
The area for function h with subinterval value 16 is 6.19

The area for function g with subinterval value 32 is 5.91
The area for function h with subinterval value 32 is 6.25

The area for function g with subinterval value 64 is 5.88
The area for function h with subinterval value 64 is 6.27

The area for function g with subinterval value 128 is 5.87
The area for function h with subinterval value 128 is 6.28
Looking good! :w00t:
For $1000: Something that is a miserable pile of secrets.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users