Code:
/*Write a program which calculates the integral of the function
f(x)=(A*x^m)/m!
on the interval from a to b (a<b). In the main program, scanf
a non-negative integer value of m and double values of A, a and b.
Call the function Intgr() to evaluate the integral.
Your main program should be followed by three functions:
double Intgr(int m, double A, double a, double b)
double func(double x, int m, double A)
double mfact(int m)
When writing the function Intgr(), use the program w5-10.c
or the program from part B of your hw5, appropriately modified
to integrate an arbitrary function f(x) from a to be (a is not
necessarily equal to 0.0; also, let the program ask
you for n_trap only once). Within Intgr() call another
function func() to specify f(x). The return value of func() should
be equal to A if m=0, or A*x^m/m! if m>0. To evaluate m! call the
function mfact() 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 exponent m in f(x)=A*x^m/m! : 5
Enter the coefficient A in f(x)=A*x^m/m! : 2.5
Enter the bounds for the integration interval, a < b : 1.25 3.75
Integrate f(x) on [a,b]
Enter the number of trapezoids: 1000
The value of the integral is 9.64272 .
*/
#include <stdio.h>
#include <math.h>
double Intgr(int m, double A, double a, double b);
double func(double x, int m, double A);
double mfact(int m);
main()
{
int n_trap, m;
double del_x, f, x, sum, A, a, b;
printf("Enter the exponent m in f(x)=A*x^m/m! :");
scanf("%d",&m);
printf("Enter the coefficient A in f(x)=A*x^m/m! :");
scanf("%lf",&A);
printf("Enter the bounds for the integration interval, a < b :");
scanf("%lf%lf",&a,&b);
printf("Integrate f(x) on [a,b]\n");
printf("Enter the number of trapezoids:");
scanf("%d",&n_trap);
printf("\nThe value of the integral is %.5f .\n",Intgr(m, A, a, b));
}
double mfact(int m)
{
int k,abs;
printf("(mfact function) m is %d",m);
abs=1;
if(m!=0)
{
for(k=m;k>1;k--)
abs*=k;
printf("(mfact function) abs is %d",abs);
}
else if(m=0)
{
abs=1;
printf("(mfact function) k is %d",k);
}
else if(m<0)
{
printf("Error: Cannot take factorial of negative integer\n");
return;
}
return abs;
}
double func(double x, int m, double A)
{
if(m>=0)
{
printf("(in func) A is %f\n",A);
printf("(in func) x is %f\n",x);
printf("(in func) m is %d\n",m);
printf("(in func) mfact(m) is %f\n",mfact(m));
return A*pow(x,m)/mfact(m);
printf("(in func) return is %f\n",A*pow(x,m)/mfact(m));
}
else
{
printf("Error: Cannot take factorial of negative number\n");
return;
}
}
double Intgr(int m, double A, double a, double b)
{
int n_trap,k; double del_x,x,f,sum;
del_x=(b-a)/(double)n_trap;
x=0.;
f=func(x,m,A);
sum=0.;
for(k=0;k<=n_trap;k++) // Sum of x0 + x1 + x2 +...+x(n-1)+x(n)
{
x=((double)k*del_x)+a;
sum+=f;
}
for(k=1;k<n_trap;k++) // Sum of x1 + x2 +...+ x(n-2)+x(n-1)
{
x=((double)k*del_x)+a;
sum+=f;
}
sum*=0.5;
sum*=del_x;
return sum;
}
My problem (as of now) is that my 'x' is undefined. This isnt really a programming question, but what is x ( func() function)