Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program is to be done in C. I apologize, it did not upload pictures. For th

ID: 3865683 • Letter: T

Question

This program is to be done in C. I apologize, it did not upload pictures.

For this program, let’s explore some of the ways we can use C to perform numerical integration. We’ll restrict our attention to definite integrals on smooth curves only and look at two methods, called the rectangle rule and the trapezoidal rule respectively (http://en.wikipedia.org/wiki/Numerical_integration).

With the rectangle rule, if we wish to calculate the area under a curve between two end points, a and b, we can evaluate the function at the midpoint between a and b and use that value to create a rectangle whose area will approximate the area under the curve between a and b. A diagram follows:

Here, the area of the red rectangle is consider an approximation for the definite integral between a and b. This area can be expressed by the following equation:

When this technique is applied to a larger region of the curve, we can divide the whole large region into smaller sub-regions of equal size, and apply the rectangle rule to each one. We may get something like the following:

An approximation for the definite integral between, say, -2 and 2, in this case, will then be the sum of the areas of the rectangles. That sum will produce an approximation for the integral over the entire region.

With the trapezoid rule, we can do something similar. The difference is that trapezoids instead of rectangles are used to create the underlying approximation. Here is a diagram:

The area of the trapezoid (and therefore an approximation of the area under the curve) is given by the following equation:

When applied to a larger region that is divided into many sub-regions, we get something like:

Here the technique is very similar: We divide the total region to be integrated into smaller sub- regions of equal size, calculate area of the trapezoid for each region, and then sum them all up to produce an approximation for the whole integral.

Note that this approach works when f(x)>0. Remembering your calculus, how do you define “area under the curve” if f(x)<0? How would you change your numerical integration program to deal with that situation? You do not have to concern yourself with that situation for this instance, but the change is minor if you are trying to “find the area of the region bounded by f(x), the x- axis, and the bounds a and b.”

Assignment:

Let’s write a C program that will calculate an approximate value for the definite integral of a curve using each of the two methods, the rectangle rule and the trapezoidal rule. Hopefully we’ll be able to observe how the approximate value improves as we increase the number of sub- intervals (decrease the size of the sub-intervals). In addition, let’s design our main function (i.e., the function that calculates the integral approximation) so that it can accept a function pointer that will determine which method to use. In that way, our integral approximation function is generalized and can be used with any method that depends on the summing of sub-intervals to approximate a definite integral.

Let’s do the following:  

1) To simplify matters, I recommend putting the following two typedef’s in global space:

                                                       a) typedef double (*mainFunction) (double);

                                                       b) typedef double (*calcArea) (mainFunction, double, double);

Notes about these typedefs:

       i) The function of type mainFunction , which is indicated below as curve1(), will calculate a single value based on the underlying function that is being integrated. It basically returns f(x) for some input value x.

     ii) Functions of type calcArea, on the other hand, will calculate the area between two endpoints using either the rectangle or the trapezoidal methods. The examples given below of these types of functions are calcAreaRect() and calcAreaTrap().

2) Then create the following functions in your program:

      a) double curve1( double x );

              i) This function will simply return the value of the underlying function at the point x.

            ii) Note that this function is of type mainFunction.

      b) double calcAreaRect (mainFunction main, double a1, double b1);

            i) This function will return the approximation value between two end points, a1 and b1, based on the rectangle method

            ii) Note that this function is of type calcArea.

      c) double calcAreaTrap (mainFunction main, double a1, double b1);

             i) This function will return the approximation value between two end points, a1 and b1, based on the trapezoid method.

            ii) Note that this function is of type calcArea.

      d) double calcIntegral (mainFunction main, calcArea getArea, double a, double b);

             i) This function will return the approximation value between two end points, a and b, based on the method specified by calcArea.

    ii) calcIntegral will:

   (1) Divide the region between a and b into NUM_INTERVAL subregions.

   (2) Calculate the approximate value for each sub-region according to the function calcArea.

   (3) Sum them all up to get the final result and return it.

Initial Settings and Specifications:

For our initial values, let’s let NUM_INTERVALS = 100, and f(x) = x2 + 3x + 2. A diagram of that curve is depicted below. Have the computer ask the user which method he would like to use to do the integration, i.e., rectangle or trapezoid, and then to specify the endpoints. Your program will then return the result of the integral approximation based on the indicated method. Print out your answer to two significant decimal places.

Test Cases

Integrate, using both rectangle and trapezoid rules, the following three intervals:

t1: [0, 1.0]

t2: [1.0, 3.0]

t3: [0, 3.0]

You may find it interesting to try different numbers of intervals when integrating to see the impact on the accuracy of your answer.

For this simple function, you may find it interesting to analytically solve the integration problem and compare the “correct” answer to the approximate answers you have found using the rectangle and trapezoid rules.

Please include a screen shot of the TEST CASES printed out to the screen and tell me the System/Compiler that you used.

THANK YOU so much for anybody who can help!!!

           

f(1)() ((a +6)/2) d: T b - a

Explanation / Answer

Given below is the code for the question and also output. Please do rate the answer if it helped .Thank you.


#include <stdio.h>

int NUM_INTERVALS = 100;

typedef double (*mainFunction) (double);
typedef double (*calcArea) (mainFunction, double, double);


double curve1( double x ); //function f(x) for the curve
double calcAreaRect (mainFunction f, double a, double b); //function to calculate area using rectangle rule
double calcAreaTrap (mainFunction f, double a, double b); //function to calculate area using trapezoidal rule
double calcIntegral (mainFunction f, calcArea getArea, double a, double b); //calculte definite integral in interval a,b

int main()
{
int choice;
double a, b, integral = 0;
printf("Program to calculate the definite integral of curve f(x) = x^2 + 3x + 2 in the interval a , b ");
printf("1. Use Rectangle rule ");
printf("2. Use Trapezoidal rule ");
printf("Which method do you want to use (1/2) ? ");
scanf("%d", &choice);
  
printf("Enter the interval - ");
printf(" a = ");
scanf("%lf", &a);
printf(" b = ");
scanf("%lf", &b);
  
if(choice == 1)
{
integral = calcIntegral(curve1, calcAreaRect, a, b); //pass the function names as parameter
printf("The definite integral (using Rectangle Rule) for f(x) = x^2 + 3x + 2 in the interval [%.2f, %.2f] is %.2f ", a, b, integral);
}
else if(choice == 2)
{
integral = calcIntegral(curve1, calcAreaTrap, a, b); //pass the function names as parameter
printf("The definite integral (using Trapezoidal Rule) for f(x) = x^2 + 3x + 2 in the interval [%.2f, %.2f] is %.2f ", a, b, integral);

}
else
{
printf("Invalid choice ! ");
}
}

double curve1( double x )
{
//f(x) = x^2 + 3x + 2
return x * x + 3 * x + 2;
}

double calcAreaRect (mainFunction f, double a, double b)
{
return (b - a) * f((a+b)/2);
}
double calcAreaTrap (mainFunction f, double a, double b)
{
return (b - a) * ((f(a) + f(b))/2);
}
double calcIntegral (mainFunction f, calcArea getArea, double a, double b)
{
  
double a1, b1;
double area = 0;
double delta = (b - a) / NUM_INTERVALS;
  
a1 = a;
b1 = a + delta;
  
while(b1 <= b)
{
area = area + getArea(f, a1, b1);
a1 = b1;
b1 += delta;
}   
return area;
  
}

output for different runs of the program

Program to calculate the definite integral of curve f(x) = x^2 + 3x + 2 in the interval a , b
1. Use Rectangle rule
2. Use Trapezoidal rule
Which method do you want to use (1/2) ? 1
Enter the interval -
   a = 0
   b = 1.0
The definite integral (using Rectangle Rule) for f(x) = x^2 + 3x + 2 in the interval [0.00, 1.00] is 3.77
======================
Program to calculate the definite integral of curve f(x) = x^2 + 3x + 2 in the interval a , b
1. Use Rectangle rule
2. Use Trapezoidal rule
Which method do you want to use (1/2) ? 2
Enter the interval -
   a = 0
   b = 1.0
The definite integral (using Trapezoidal Rule) for f(x) = x^2 + 3x + 2 in the interval [0.00, 1.00] is 3.77
======================
Program to calculate the definite integral of curve f(x) = x^2 + 3x + 2 in the interval a , b
1. Use Rectangle rule
2. Use Trapezoidal rule
Which method do you want to use (1/2) ? 1
Enter the interval -
   a = 1
   b = 3
The definite integral (using Rectangle Rule) for f(x) = x^2 + 3x + 2 in the interval [1.00, 3.00] is 24.27
======================
Program to calculate the definite integral of curve f(x) = x^2 + 3x + 2 in the interval a , b
1. Use Rectangle rule
2. Use Trapezoidal rule
Which method do you want to use (1/2) ? 2
Enter the interval -
   a = 1
   b = 3
The definite integral (using Trapezoidal Rule) for f(x) = x^2 + 3x + 2 in the interval [1.00, 3.00] is 24.27
======================
Program to calculate the definite integral of curve f(x) = x^2 + 3x + 2 in the interval a , b
1. Use Rectangle rule
2. Use Trapezoidal rule
Which method do you want to use (1/2) ? 1
Enter the interval -
   a = 0
   b = 3.0
The definite integral (using Rectangle Rule) for f(x) = x^2 + 3x + 2 in the interval [0.00, 3.00] is 28.50
======================
Program to calculate the definite integral of curve f(x) = x^2 + 3x + 2 in the interval a , b
1. Use Rectangle rule
2. Use Trapezoidal rule
Which method do you want to use (1/2) ? 2
Enter the interval -
   a = 0
   b = 3.0
The definite integral (using Trapezoidal Rule) for f(x) = x^2 + 3x + 2 in the interval [0.00, 3.00] is 28.50

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote