Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program which calculates the integral of the function f(x)=B*exp(-x^2.5)

ID: 3632718 • Letter: W

Question

Write a program which calculates the integral of the function

f(x)=B*exp(-x^2.5)+(A*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, 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 B*exp(-x^2.5)+A*x^n if m=0, or B*exp(-x^2.5)+A*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.5 6

Enter the coefficients A and B: 2.5 -3.75

Enter the bounds for the integration interval, a < b : 1.05 4.05

Integrate f(x) on [a,b]
Enter the number of trapezoids: 10000

The length of the subinterval del_x = .......

The value of the integral is ...... .

*/
#include <stdio.h>
#include <math.h>
double func(double x, double m, int m, double A, double B);
double Integr(double n, int m, double A, double B, double a, double b);
double mfact(int m);
int main(void)
{
int m;
double A, B, a, b, n;
printf("Enter the exponent n and m: ");
scanf("%lf %d", &n, &m);
printf(" Enter coefficients A and B: ");
scanf("%lf %lf", &A, &B);
printf(" Enter the bounds for the integration interval, a < b: ");
scanf("%lf %lf", &a, &b);
Integral = Integr(n, m, A, B, a, b);
printf("The length of the subinterval del_x is %g", del_x);
printf("The value of the integral is %g", Integral);
}
Integr(double n, int m, double A, double B, double a, double b);
{
double f, x, del_x, sum, m, a, b;
int flag, k, n_trap;
printf(" Integrate f(x) on [a,b]");
do
{
printf("Enter the number of trapezoids: ");
scanf("%d", &n_trap);
del_x = (b-a)/n_trap;
printf("del_x= %g ", del_x);
x=a;
f = func(x, n, m, A, B);
sum = -0.5 * f;
for(k=0, k<=n_trap; k++)
{
x = a + k * del_x;
f = func(x, n, m, A, B);
sum += f;
}
sum -= 0.5 *f;
sum *= del_x;
return sum;
}
while(flag !=1);
}
func(double x, double m, int m, double A, double B);
{
if(m==0)
{
y = B*exp(-pow(x,2.5)) + A*pow(x,n);
}
else
{
y = B*exp(-pow(x,2.5)) + A*pow(x,n)/mfact(m)
}
mfact(intm)
{
int i, m;
int fact=1
for(i=1; i<=m; i++)
{
fact=fact*i;
return fact;
}
}

I have about 10 different errors and not sure where to start for them. Also feel free to suggest changes. I know I have some problems so any help would be great.


hw6.c:55: error: conflicting types for 'm'
hw6.c:55: error: previous definition of 'm' was here
hw6.c: In function 'main':
hw6.c:68: error: 'Integral' undeclared (first use in this function)
hw6.c:68: error: (Each undeclared identifier is reported only once
hw6.c:68: error: for each function it appears in.)
hw6.c:69: error: 'del_x' undeclared (first use in this function)
hw6.c: At top level:
hw6.c:72: warning: data definition has no type or storage class
hw6.c:72: error: conflicting types for 'Integr'
hw6.c:56: error: previous declaration of 'Integr' was here
hw6.c:73: error: expected identifier or '(' before '{' token
hw6.c:98: error: conflicting types for 'm'
hw6.c:98: error: previous definition of 'm' was here
hw6.c:98: warning: data definition has no type or storage class
hw6.c:98: error: conflicting types for 'func'
hw6.c:55: error: previous declaration of 'func' was here
hw6.c:99: error: expected identifier or '(' before '{' token

Explanation / Answer

To start, let's clean up the compile/syntax errors. Line 1 reads "#include

"

Line 11: Add the variables Integral and del_x to the list of doubles you are declaring.
Line 20: What is del_x? you declare it in Integr() but don't export it.
Line 24: Remove the semi-colon at the end of the line (many things will now change)
Line 24: Your Integr function says it returns an int (is that what you want?), but returns a double instead. change the return type to double. It should read:

double Integr(double n, int m, double A, double B, double a, double b)
Line 26: delete the declarations of b, m, and a (they're parameters)
Line 27: delete the declaration of flag
Line 29: delete the "do {" as you exit the loop before hitting the while()
Line 38: change the comma after k=0 to a semi-colon.
line 48: delete the while(...); and closing brace
Line 50: Again, you have a semi-colon and are missing a return type. Should be
Line 50: You have two parameters called "m". I assume the first one (the double) should be n;
double func(double x, double n, int m, double A, double B)

Line 52: declare a variable double y;
Line 58: Add a semi-colon
Line 60ish: reads mfact(intm) Lots to do here
Before this line add the following
return y;
}

Now the function mfact() is not "nested" inside func(), change the line to read
int mfact(int m)

Move the return fact to after the for-loop (currently it will always return 1).

Your code now looks like:

#include <stdio.h>
#include <math.h>

double func(double x, double m, int m, double A, double B);
double Integr(double n, int m, double A, double B, double a, double b);
double mfact(int m);

int main(void)
{
        int m;
        double A, B, a, b, n, Integral, del_x;
        printf("Enter the exponent n and m: ");
        scanf("%lf %d", &n, &m);
        printf(" Enter coefficients A and B: ");
        scanf("%lf %lf", &A, &B);
        printf(
                        " Enter the bounds for the integration interval, a < b: ");
        scanf("%lf %lf", &a, &b);
        Integral = Integr(n, m, A, B, a, b);
        printf("The length of the subinterval del_x is %g", del_x);
        printf("The value of the integral is %g", Integral);
}

double Integr(double n, int m, double A, double B, double a, double b)
{
        double f, x, del_x, sum;
        int k, n_trap;
        printf(" Integrate f(x) on [a,b]");
        printf("Enter the number of trapezoids: ");
        scanf("%d", &n_trap);
        del_x = (b-a)/n_trap;
        printf("del_x= %g ", del_x);
        x=a;
        f = func(x, n, m, A, B);
        sum = -0.5 * f;
        for(k=0; k<=n_trap; k++)
        {
                x = a + k * del_x;
                f = func(x, n, m, A, B);
                sum += f;
        }
        sum -= 0.5 *f;
        sum *= del_x;
        return sum;
}
double func(double x, double n, int m, double A, double B)
{         double y;
        if(m==0)
        {
                y = B*exp(-pow(x,2.5)) + A*pow(x,n);
        }
        else
        {
                y = B*exp(-pow(x,2.5)) + A*pow(x,n)/mfact(m);
        }
        return y;
}

int mfact(int m)
{
        int i;
        int fact=1;
        for(i=1; i<=m; i++)
        {
                fact=fact*i;
        }
        return fact;
}

It will compile, but I haven't debugged it ... that's down to you. Major syntax errors such as the spurious semi-colon on line 24 generate lots of errors which seem to make no sense - because the structure of your code doesn't make sense to the compiler.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote