Write a C program ( Care should be taken not to use extensions of C + + language
ID: 3552141 • Letter: W
Question
Write a C program (Care should be taken not to use extensions of C + + language in your work) that reads two real terminals (a, b) representing the integration of a definite integral terminals. The program's goal is to compare four different methods of numerical integration. Each method will be programmed by a different function. Here is each method:
Method of rectangles : (int_{x_{m}}^{x_{n}} f(x)dx pprox hsum_{i=0}^{n-1} f(x_{i}))
The midpoint method : (int_{x_{m}}^{x_{n}} f(x)dx pprox hsum_{i=0}^{n-1} f(x_{i+1/2}))
Method of trapezoids : (int_{x_{m}}^{x_{n}} f(x)dx pprox h((( f(x_{m}) +f(x_{n}))/2) + sum_{i=1}^{n-1} f(x_{i})))
SImpson method : (int_{x_{m}}^{x_{n}} f(x)dx pprox h/3( f(x_{0}) + 2 sum_{i=1}^{n/2-1} f(x_{2i}) +4 sum_{i=1}^{n/2} f(x_{2i-1}) + f(x_{n})))
Here is an example of the results of the program with terminals 0 and 1:
Enter terminals : 0 1
Exact value : 1.71828182845905
Method intervals approximated value error
Rectangle 20 1.67568274321374 2.479168 %
Midpoint 20 1.71810285381891 0.010416 %
Trapeze 20 1.71863978892522 0.020832 %
Simpson 20 1.71828188810386 0.000003 %
Rectangle 40 1.69689279851633 1.244792 %
Midpoint 40 1.71823708235212 0.002604 %
Trapeze 40 1.71837132137206 0.005208 %
Simpson 40 1.71828183218768 0.000000 %
Rectangle 60 1.70400258808007 0.831019 %
Midpoint 60 1.71826194109901 0.001157 %
Trapeze 60 1.71832160331723 0.002315 %
Simpson 60 1.71828182919560 0.000000 %etc. .
Note: Use the double type in your calculations.
Note: Use the following conversion specifications : % 8d for the 2nd column %19.14f for the 3rd column and %10.6lf for the 4th column.
Data for submission : Here are four pairs of real that you must use to produce the results of the program:0 1, -1 0, -1 1, -1.5 1.5
Explanation / Answer
#include #include double f(double x) { return exp(x); }double integral_by_rectangle(double (*f)(double), double a, double b, int n);double integral_by_midpoint(double (*f)(double), double a, double b, int n);double integral_by_trapeze(double (*f)(double), double a, double b, int n);double integral_by_simpson(double (*f)(double), double a, double b, int n);int main(){ const char* labels[] = { "Rectangle", "Midpoint", "Trapeze", "Simpson" }; double (*integrals[4])(double(*)(double), double, double, int) = { integral_by_rectangle, integral_by_midpoint, integral_by_trapeze, integral_by_simpson }; double a, b, Ie, In, err; int n, i; printf("Enter intervals: "); scanf("%lf%lf", &a, &b); Ie = f(b) - f(a); printf("Exact value: %.15f ", Ie); puts("Method intervals approximated value error"); for (n = 20; n < 101; n += 20) { for (i = 0; i < 4; ++i) { In = integrals[i](f, a, b, n); err = 100.0 * fabs(In - Ie) / Ie; printf("%-9s %8d %19.14f %10.6lf%% ", labels[i], n, In, err); } } return 0;}double integral_by_rectangle(double (*f)(double), double a, double b, int n){ double h = (b-a) / n; double x = a, sum = 0; int i; for (i = 0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.