Hi! These are the instructions:
Write a program which calculates the integral of the function
f(x)=A*exp(-x^2)+(B*x^n)/m!
on the interval from a to b (0<a<b). In the main program, scanf
a double value for n and a non-negative integer value for m,
nonzero double values for A and B, and positive double values for a and b>a.
Call the function Integr() to evaluate the integral.
Your main program should be followed by three functions:
double Integr(double n, int m, double A, double B, double a, double b)
double func(double x, double n, int m, double A, double B)
double mfact(int m)
When writing the function Integr(), use the program w4-10.c
or the program from 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; also print the length
of the corresponding sub-interval del_x). Within Integr() call another
function func() to evaluate f(x). The return value of func() should
be equal to A*exp(-x^2)+B*x^n if m=0, or A*exp(-x^2)+B*x^n/m! if m>0.
To evaluate m! call the function mfac() that you will also create.
To evaluate x^n call the function pow(), which is embedded in
the math.h library.
.................................................. ..............
Your output should look like this:
Enter the exponents (double)n and (int)m: 1.25 5
Enter the coefficients A and B: 2.1 -3.15
Enter the bounds for the integration interval, a < b : 1.2 4.05
Integrate f(x) on [a,b]
Enter the number of trapezoids: 10000
The length of the subinterval del_x = 0.000285
The value of the integral is -0.0869732 .
And here is what I have:
Code:#include <stdio.h> #include <math.h> double Intgr(n, m, A, B, a, b); double func(x, n, m, A, B); double mfact(m); double m, A, a, b, x, del_x, sum, f, integral, subinterval; int n, k, n_trap; main() { printf("\n\nEnter the exponents (double)n and (int)m:\n\n"); scanf("%d %f", &n, &m); printf("Enter the coefficients A and B:\n\n"); scanf("%f", &A); printf("Enter the bounds for the integration interval, a < b :\n"); scanf("%f %f", &a, &b); printf("Integrate f(x) on [a,b]\n"); printf("Enter the number of trapezoids:"); scanf("%d", &n_trap); double Intgr(double n, int m, double A, double B, double a, double b) { del_x = (b-a)/n_trap; x = a; f = A*pow(x,m)/nfact(n); sum = -0.5 * f; 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; } subinterval=del_x; printf("The length of the subinterval del_x = %.5f\n", subinterval); printf("\n"); integral=Intgr(n,m,A,B,a,b); printf("The value of the integral is %.5f\n", integral); double func(double x, double n, int n, double A, double B) { if(m==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); } }
I basically tried to modify a program that was provided but I'm afraid I had to make QUITE a few additions. Really confused and probably completely wrong! Please help. Thanks!