I'm trying to call multiple functions to calculate an integral on the interval 0 < a < b.
My code compiles and it seems to call up the functions correctly but I get a weird number for an answer.
This is my code:
This is my output:Code:#include <stdio.h> #include <math.h> int main() { int n; double m, A, a, b; printf("\nEnter the exponents (double)m and (int)n in f(x)=A*x^m/n! : "); scanf("%lf %lf", &m,&n); printf("\nEnter the coefficient A in f(x)=A*x^m/n! : "); scanf("%lf",&A); printf("\nEnter bounds for integration interval, a < b : "); scanf("%lf %lf", &a,&b); Intgr(m,n,A,a,b); } double Intgr(double m, int n, double A, double a, double b) { int n_trap, k; double del_x, x, f, sum; printf("\nIntegrate f(x) on [a,b]\n"); printf("Enter the number of trapezoids: "); scanf("%d", &n_trap); del_x = (b-a)/(double)n_trap; x = a; f = func(x, m, n, A); sum = - 0.5 * f; for(k=0; k<=n_trap; k++) { x = a + k * del_x; f = func(x, m, n, A); sum += f; } sum -= 0.5 * f; sum *= del_x; printf("\nIntegral=%f\n", sum); } double func(double x, double m, int n, double A) { if (n == 0) return A * pow(x,m); else return (A* pow(x,m)) / nfact(n); } double nfact(int n){ double f=1; f*=n--; return f; }
The value of the integral should be -0.792905 .Code: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 bounds for integration interval, a < b : 1.5 3.75 Integrate f(x) on [a,b] Enter the number of trapezoids: 1000 Integral=2416590846.654750
I'm getting the following warnings when I compile:
hw7.c:19: warning: type mismatch with previous implicit declaration
hw7.c:15: warning: previous implicit declaration of `Intgr'
hw7.c:19: warning: `Intgr' was previously implicitly declared to return `int'
hw7.c:40: warning: type mismatch with previous implicit declaration
hw7.c:27: warning: previous implicit declaration of `func'
hw7.c:40: warning: `func' was previously implicitly declared to return `int'
hw7.c:47: warning: type mismatch with previous implicit declaration
hw7.c:44: warning: previous implicit declaration of `nfact'
hw7.c:47: warning: `nfact' was previously implicitly declared to return `int'
What do these errors mean and how do I make my Intgr function return the right value? Thank you very much.



LinkBack URL
About LinkBacks


