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

USING C CODE PLEASE HELP Program-Numerical Integration Introduction For this pro

ID: 3711430 • Letter: U

Question

USING C CODE PLEASE HELP

Program-Numerical Integration Introduction 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/wikiNumerical 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:

Explanation / Answer

#include <stdio.h>

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 = 0;
double a, b, value = 0;
printf("Program to calculate the definite integral of curve f(x) = x^2 + 3x + 2 in the interval [a,b] ");
  
while(choice != 3)
{
printf("1. Rectangle method ");
printf("2. Trapezoidal method ");
printf("3. Exit ");
printf("Enter your choice: ");
scanf("%d", &choice);
  
if(choice != 3)
{
printf("Enter the interval [a,b] - ");
printf("a = ");
scanf("%lf", &a);
printf("b = ");
scanf("%lf", &b);
}
  
switch(choice)
{
case 1:
value = calcIntegral(curve1, calcAreaRect, a, b); //pass the function names as parameter
printf("Using Rectangle method, the definite integral for f(x) in the interval [%.2f, %.2f] is %.2f ", a, b, value);
break;
case 2:
value = calcIntegral(curve1, calcAreaTrap, a, b); //pass the function names as parameter
printf("Using Trapezoidal method, the definite integral for f(x) in the interval [%.2f, %.2f] is %.2f ", a, b, value);
case 3:
break;
default:
printf("Invalid choice!");
  
}
}
}
//function f(x) for the curve
double curve1( double x )
{
//f(x) = x^2 + 3x + 2
return x * x + 3 * x + 2;
}


//function to calculate area using rectangle rule
double calcAreaRect (mainFunction f, double a, double b)
{
return (b - a) * f((a+b)/2);
}
//function to calculate area using trapezoidal rule
double calcAreaTrap (mainFunction f, double a, double b)
{
return (b - a) * ((f(a) + f(b))/2);
}


//calculte definite integral in interval a,b
double calcIntegral (mainFunction f, calcArea getArea, double a, double b)
{
  
double a1, b1;
double area = 0;
int NUM_INTERVALS = 100;
double increment = (b - a) / NUM_INTERVALS;
  
a1 = a;
b1 = a + increment;
  
while(b1 <= b)
{
area = area + getArea(f, a1, b1);
a1 = b1;
b1 += increment;
}
  
return area;
  
}

output

Program to calculate the definite integral of curve f(x) = x^2 + 3x + 2 in the interval [a,b]
1. Rectangle method
2. Trapezoidal method
3. Exit
Enter your choice: 1
Enter the interval [a,b] -
a = 0
b = 1.0
Using Rectangle method, the definite integral for f(x) in the interval [0.00, 1.00] is 3.77
1. Rectangle method
2. Trapezoidal method
3. Exit
Enter your choice: 2
Enter the interval [a,b] -
a = 0
b = 1
Using Trapezoidal method, the definite integral for f(x) in the interval [0.00, 1.00] is 3.77
1. Rectangle method
2. Trapezoidal method
3. Exit
Enter your choice: 1
Enter the interval [a,b] -
a = 1
b = 3.0
Using Rectangle method, the definite integral for f(x) in the interval [1.00, 3.00] is 24.27
1. Rectangle method
2. Trapezoidal method
3. Exit
Enter your choice: 2
Enter the interval [a,b] -
a = 1.0
b = 3.0
Using Trapezoidal method, the definite integral for f(x) in the interval [1.00, 3.00] is 24.27
1. Rectangle method
2. Trapezoidal method
3. Exit
Enter your choice: 1
Enter the interval [a,b] -
a = 0.0
b = 3.0
Using Rectangle method, the definite integral for f(x) in the interval [0.00, 3.00] is 28.50
1. Rectangle method
2. Trapezoidal method
3. Exit
Enter your choice: 2
Enter the interval [a,b] -
a = 0.0
b = 3.0
Using Trapezoidal method, the definite integral for f(x) in the interval [0.00, 3.00] is 28.50
1. Rectangle method
2. Trapezoidal method
3. Exit
Enter your choice: 3