f(x)=(A*x^m)/n!
on the interval from a to b (0<a<b). In the main program, scanf
a double value for m and a non-negative integer value for n,
a double value of A, and positive double values for a and b>a.
Call the function Intgr() to evaluate the integral.
Your main program should be followed by three functions:
double Intgr(double m, int n, double A, double a, double b)
double func(double x, double m, int n, double A)
double nfact(int n)
When writing the function Intgr(), use the program w4-10.c
or the program from part B of your hw5, appropriately modified
to integrate an arbitrary function f(x) from a to b
(let the program ask you for n_trap only once, and scan
sufficiently large value for n_trap). Within Intgr() call another
function func() to specify f(x). The return value of func() should
be equal to A*x^m if n=0, or A*x^m/n! if n>0. To evaluate n! call the
function nfact() that you will also write. To evaluate x^m call
the function pow() already embedded in the math.h library.
.................................................. ..............
Your output should look like this:
Enter the exponents (double)m and (int)n in f(x)=A*x^m/n! : 3.25 5
Enter the coefficient A in f(x)=A*x^m/n! : -1.5
Enter the bounds for the integration interval, a < b : 1.5 3.75
Integrate f(x) on [a,b]
Enter the number of trapezoids: 1000
The value of the integral is -0.792905 .
*/
Expand|Select|Wrap|Line Numbers
- #include <stdio.h>
- #include <math.h>
- double Intgr(double m, int n, double A, double a, double b);
- double func(double x, double m, int n, double A);
- double nfact(int n);
- main()
- {
- double m, A, a, b, x, del_x, sum, f;
- int n, k, n_trap;
- printf("Enter the exponents (double)m and (int)n in f(x) = A*x^m/n! : ");
- scanf("%f %d", &m, &n);
- printf("\nEnter the coefficient A in f(x) = A*x^m/n! : ");
- scanf("%f", &A);
- printf("\nEnter the bounds for the integration interval, a < b : ");
- scanf("%f %f", &a, &b);
- printf("Integrate f(x) on [a,b]\n");
- printf("Enter the number of trapezoids : ");
- scanf("%d", &n_trap);
- printf("\nn! is : %g\n", nfact(n));
- printf("\nThe value of the integral is %g\n", Intgr(m,n,A,a,b));
- }
- double Intgr(double m, int n, double A, double a, double b)
- {
- double m, A, a, b;
- int n;
- del_x = (b-a)/n_trap;
- x = a;
- f = A*pow(x,m)/nfact(n);
- sum = -0.5 * f;
- double m, A, a, b;
- int n;
- del_x = (b-a)/n_trap;
- x = a;
- f = A*pow(x,m)/nfact(n);
- sum = -0.5 * f;
- for(k=0; k<=n_trap; k++)
- {
- x = k * del_x;
- f = A*pow(x,m)/nfact(n);
- sum +=f;
- }
- sum -= 0.5 * f;
- sum *= del_x;
- }
- double func(double x, double m, int n double A)
- {
- double x, m, A;
- int n;
- if(n==0) return A*pow(x,m);
- else return (A*pow(x,m))/nfact(n);
- }
- double nfact(int n)
- {
- if(n==1)return 1;
- else return n*nfact(n-1);
- }
iacs5.ucsd.edu% !g
gcc hw7.c -lm
hw7.c: In function `Intgr':
hw7.c:77: warning: declaration of `m' shadows a parameter
hw7.c:77: warning: declaration of `A' shadows a parameter
hw7.c:77: warning: declaration of `a' shadows a parameter
hw7.c:77: warning: declaration of `b' shadows a parameter
hw7.c:78: warning: declaration of `n' shadows a parameter
hw7.c:79: error: `del_x' undeclared (first use in this function)
hw7.c:79: error: (Each undeclared identifier is reported only once
hw7.c:79: error: for each function it appears in.)
hw7.c:79: error: `n_trap' undeclared (first use in this function)
hw7.c:80: error: `x' undeclared (first use in this function)
hw7.c:81: error: `f' undeclared (first use in this function)
hw7.c:82: error: `sum' undeclared (first use in this function)
hw7.c:83: error: `k' undeclared (first use in this function)
hw7.c: At top level:
hw7.c:93: error: parse error before "double"
hw7.c: In function `func':
hw7.c:94: error: number of arguments doesn't match prototype
hw7.c:54: error: prototype declaration
iacs5.ucsd.edu% vi hw7.c
Version SVR4.0, Solaris 2.5.0
"hw7.c" 105 lines, 2783 characters