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


Sign In
Create Account


Back to top









