MAE9: Homework #7, Winter 2011 Due on Tuesday, February 22, Turnin by 9:00pm 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

&a. A. } .Integrate f(x) on [a.h> double Integr(double x. m. integral = -. printf("Enter the bounds for the integration interval. printf("Integrate f(x) on [a. del_x. n_trap. m. */ //Shreyas Rao //HW7 #include <stdio. &B). i<=n_trap. a. integral += y. n. int m. &n_trap). double B). B). scanf("%lf %d".000285 The value of the integral is -0. printf("Enter the coefficients A and B: "). x.b]\n"). i++){ x = a + i*del_x. B. b. del_x= (b-a)/(double)n_trap. A. double B). n. printf("\n"). &A. y = func(x. double mfact(int m). printf("Enter the exponents (double)n and (int)m: "). a < b : "). B). scanf("%lf %lf". printf("\n"). &n. integral. printf("\n"). y = func(x. printf("\n"). double func( double x.5*y. double A. m.0869732 .m for (i=0. int i. double A. double n.h> #include <math. double b. int m. y. printf("Enter the number of trapezoids: "). scanf("%d". scanf("%lf %lf". n. &b). double a. double n. x = a. main (void) { double A. &m).b] Enter the number of trapezoids: 10000 The length of the subinterval del_x = 0.

2))+B*pow(x. double n. printf("\n"). integral). A. double B){ if(m==0) return A*exp(-pow(x. int m.integral -= 0. } double func(double x. n. double b. del_x). } .n)/mfact(m).2))+B*pow(x. double n. double A. printf("The length of the subinterval del_x = %f". double a. double B){ return func(x. printf("The value of the integral is %. B). printf("\n"). printf("\n\n"). else return m*mfact(m-1). m.7f\n". } double mfact(int m){ if(m==1) return 1. int m. } double Integr(double x.5 * y.n). double A. integral *= del_x. else if(m>0) return A*exp(-pow(x.

Sign up to vote on this title
UsefulNot useful