Write a C program (to not use the extensions of C++ ) that reads a number functi
ID: 3553022 • Letter: W
Question
Write a C program (to not use the extensions of C++ ) that reads a number function and two real terminals (a, b) representing the integration terminals of a definite integral . The goal of the program is to compare different methods of numerical integration, but this time with 10 different theoretical functions. So we need the function pointers. We only retain these two methods:
a) Midpoint method : (int_{x_{m}}^{x_{n}} f(x) dx pprox hsum_{i=0}^{n-1} f(x_{i+1/2}))
b) Simpson method : (int_{x_{m}}^{x_{n}} f(x)dx pprox h/3(f(x_{o}) + 2sum_{i=1}^{n/2-1} f(x_{2i})+ 4sum_{i=1}^{n/2} f(x_{2i-1}) + f(x_{n})))
Note: The terminal
Note: The quantity n is the number of intervals used by different methods, your program will vary n with the following values: 10, 20 and 30. Note that, since n must be even in the Simpson method, these values are compatible.
Here are the 10 functions that will be used with the number that allows the user to specify:
2. the sine function sin(x) with (int_{a}^{b} sin(x)dx = cos(a) - cos(b))
3. the cosine function cos(x) with (int_{a}^{b} cos(x)dx = sin(b) - sin(a))
4. the tangent function tan(x) with (int_{a}^{b} tan(x)dx = ln(cos(a)) - ln(cos(b)))
5. The arc sine function asin(x) with (int_{a}^{b} asin(x)dx = basin(b) + sqrt{1-b^{2}} - (aasin(a) + sqrt{1-a^{2}}))
6. The arc cosine function acos(x) with (int_{a}^{b} acos(x)dx = bacos(b) - sqrt{1-b^{2}} - (aacos(a) - sqrt{1-a^{2}}))
7. the arc tangent function atan(x) with (int_{a}^{b} atan(x)dx = batan(b) - 1/2sqrt{1+b^{2}} - (aatan(a) - 1/2sqrt{1+a^{2}}))
8. the hyperbolic sine function sinh(x) with (int_{a}^{b} sinh(x)dx = cosh(b) - cosh(a))
9. the hyperbolic cosine function cosh(x) with (int_{a}^{b} cosh(x)dx = sinh(b) - sinh(a))
10. the hyperbolic tangent function tanh(x) with (int_{a}^{b} tanh(x)dx = ln(cosh(b)) - ln(cosh(a)))
Your main function will be built like this:
printf (" Enter the number of functions and terminals :");
scanf ( "% d % f % f" , & nf , & binf , & Bsup ) ;
switch ( nf )
{
case 1: Integration ( binf , Bsup , IntegralExp , exp, " exp ");
break;
case 2: Integration ( binf , Bsup , IntegralSin , sin , "sin ");
break;
case 3: Integration ( binf , Bsup , IntegralCos , cos, " cos ");
break;
case 4: Integration ( binf , Bsup , IntegralTan , tan , "tan ");
break;
etc. .
Note: IntegralExp argument represents the function that calculates the theoretical value of the integral for the exponential, exp argument is the name of the C function to use and the last argument , the string " exp " is the function name to display. Same principle for other calls to the Integration function.
Note : The names of C functions to be used are the same as those mentioned in the list of functions above, except to the function ln (x) is called log ( x ) in C.
These are expected by your program outputs:
Enter the number of function and terminals: 1 0 1
Exact value for exp: 1.71828182845905
Methode intervals approximated value error
Midpoint 10 1.71756608646113 0.041655%
Simpson 10 1.71828278192482 0.000055%
Midpoint 20 1.71810285381891 0.010416%
Simpson 20 1.71828188810386 0.000003%
Midpoint 30 1.71820228095233 0.004629%
Simpson 30 1.71828184024268 0.000001%
Secret 9 1.71828182845967 0.000000%
Note: You will notice that there a line for the Secret Method: This method uses only 9 intervals and is always more effective than Simpson's method when using 20 intervals, in some cases even if it uses 30 intervals.
Note: There is no data validation to perform (bad function number, incorrect terminals, 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 data that you must use to produce the results of the program:
1 0 1, 2 0 3
Explanation / Answer
Code:
#include "stdio.h"
#include "math.h"
void Integration(double a, double b, double (*fp1)(double, double), double (*fp2)(double), char* type){
double exact = fp1(a,b);
printf("Exact value for %s: %0.14f ",type, exact);
printf("Method intervals approximated value error ");
double error;
int mult,n;
for(mult=1;mult<=3;mult++){
n = mult*10;
double h = (b-a)/n;
int i=0;
// Midpoint method
double index, ans=0,x;
for(i=0; i<n; i++){
index = i + 0.5;
x = a + index*h;
ans += fp2(x);
}
ans = h*ans;
error = ((ans - exact)/exact)*100;
if(error < 0) error = 0-error;
printf("Midpoint %8d %19.14f %10.6lf%% ", n, ans, error);
// Simpson method
double sec_term=0, third_term=0;
for(i=1; i<=(n/2); i++){
index = 2*i;
x = a + index*h;
if(i!=(n/2)) sec_term += fp2(x);
index --;
x = a + index*h;
third_term += fp2(x);
}
ans = h/3*(fp2(a) + 2*sec_term + 4*third_term + fp2(b));
error = ((ans - exact)/exact)*100;
if(error < 0) error = 0-error;
printf("Simpson %8d %19.14f %10.6lf%% ", n, ans, error);
}
}
double IntegralExp(double a, double b){
return exp(b) - exp(a);
}
double IntegralSin(double a, double b){
return cos(a) - cos(b);
}
double IntegralCos(double a, double b){
return sin(b) - sin(a);
}
double IntegralTan(double a, double b){
return log(cos(a)) - log(cos(b));
}
double IntegralAsin(double a, double b){
return b*asin(b) + sqrt(1-b*b) - (a*asin(a) + sqrt(1-a*a));
}
double IntegralACos(double a, double b){
return b*acos(b) - sqrt(1-b*b) - (a*acos(a) - sqrt(1-a*a));
}
double IntegralAtan(double a, double b){
return b*atan(b) - 0.5*log(1+b*b) - (a*atan(a) - 0.5*log(1+a*a));
}
double IntegralSinh(double a, double b){
return cosh(b) - cosh(a);
}
double IntegralCosh(double a, double b){
return sinh(b) - sinh(a);
}
double IntegralTanh(double a, double b){
return log(cosh(b)) - log(cosh(a));
}
int main(){
float a, b;
int nf;
printf (" Enter the number of functions and terminals :");
scanf ( "%d %f %f" , & nf , & a , & b ) ;
switch ( nf )
{
case 1:
Integration ( a , b , IntegralExp , exp, "exp");
break;
case 2:
Integration ( a , b , IntegralSin , sin , "sin");
break;
case 3:
Integration ( a , b , IntegralCos , cos, "cos");
break;
case 4:
Integration ( a , b , IntegralTan , tan , "tan");
break;
case 5:
Integration ( a , b , IntegralAsin , asin, "asin");
break;
case 6:
Integration ( a , b , IntegralACos , acos , "acos");
break;
case 7:
Integration ( a , b , IntegralAtan , atan, "atan");
break;
case 8:
Integration ( a , b , IntegralSinh , sinh , "sinh");
break;
case 9:
Integration ( a , b , IntegralCosh , cosh, "cosh");
break;
case 10:
Integration ( a , b , IntegralTanh , tanh , "tanh");
break;
}
return 0;
}
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Input: 1 0 1
Output:
Exact value for exp: 1.71828182845905
Method intervals approximated value error
Midpoint 10 1.71756608646113 0.041655%
Simpson 10 1.71828278192482 0.000055%
Midpoint 20 1.71810285381891 0.010416%
Simpson 20 1.71828188810386 0.000003%
Midpoint 30 1.71820228095233 0.004629%
Simpson 30 1.71828184024268 0.000001%
Input: 2 0 3
Output:
Exact value for sin: 1.98999249660045
Method intervals approximated value error
Midpoint 10 1.99747460403255 0.375987%
Simpson 10 1.99008301487454 0.004549%
Midpoint 20 1.99185933960437 0.093812%
Simpson 20 1.98999810848141 0.000282%
Midpoint 30 1.99082190204383 0.041679%
Simpson 30 1.98999360346935 0.000056%
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.