Thread: Calculating Integral; Please help!

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    24

    Calculating Integral; Please help!

    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!
    Last edited by sam...; 02-22-2011 at 09:59 PM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... lets try this again... without the giant fonts and undreadable colors...

    Oh and btw, your source code has about 300 dots in it that do not belong.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    24
    Sorry about that! I tried to make the formatting clearer and did the opposite of what I intended to, I guess! The formatting should be okay now. Thanks.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Did you even compile this? It's rife with errors.
    Quote Originally Posted by gcc -Wall integral.c
    integral.c:4: warning: parameter names (without types) in function declaration
    integral.c:5: warning: parameter names (without types) in function declaration
    integral.c:6: warning: parameter names (without types) in function declaration
    integral.c:11: warning: return type defaults to ‘int’
    integral.c: In function ‘main’:
    integral.c:13: warning: format ‘%f’ expects type ‘float *’, but argument 3 has type ‘double *’
    integral.c:15: warning: format ‘%f’ expects type ‘float *’, but argument 2 has type ‘double *’
    integral.c:17: warning: format ‘%f’ expects type ‘float *’, but argument 2 has type ‘double *’
    integral.c:17: warning: format ‘%f’ expects type ‘float *’, but argument 3 has type ‘double *’
    integral.c: In function ‘Intgr’:
    integral.c:29: warning: implicit declaration of function ‘nfact’
    integral.c: In function ‘main’:
    integral.c:49: error: ‘B’ undeclared (first use in this function)
    integral.c:49: error: (Each undeclared identifier is reported only once
    integral.c:49: error: for each function it appears in.)
    integral.c:54: error: conflicting types for ‘n’
    integral.c:54: error: previous definition of ‘n’ was here
    integral.c: In function ‘Intgr’:
    integral.c:43: warning: control reaches end of non-void function
    integral.c: In function ‘main’:
    integral.c:65: warning: control reaches end of non-void function
    Some things to work on:
    • Fix all those icky compiler errors/warnings.
    • I hope your actual code has indentation. If not, indent it.
    • It's int main(void), and a return 0 at the end.
    • Global variables are evil. Declare them in main and pass them around.
    • Don't declare functions inside another function, move them outside.
    • You need parameter types in your function protytypes.
    • Intgr doesn't return anything, meaning it's result is garbage from the stack.
    • You print out the value of del_x/subinterval before you assign it anything in Intgr. Perfect case of global evilness.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by sam... View Post
    Sorry about that! I tried to make the formatting clearer and did the opposite of what I intended to, I guess! The formatting should be okay now. Thanks.
    Plain text is just fine... the only specials here are the use of code tags and quotations... When you start posting big fonts and boldface all over the place, you're perceived as shouting and overtly agressive and most people will simply go to the next message.

    As they say... "When in Rome, do as the Romans"...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculating an integral
    By kamme in forum C Programming
    Replies: 5
    Last Post: 12-04-2010, 04:00 PM
  2. Runtime problem
    By SantoshN in forum C Programming
    Replies: 2
    Last Post: 10-12-2010, 02:42 PM
  3. Calling functions, calculating integral
    By dakarn in forum C Programming
    Replies: 4
    Last Post: 11-18-2008, 01:14 AM
  4. Recursion
    By Lionmane in forum C Programming
    Replies: 11
    Last Post: 06-04-2005, 12:00 AM
  5. Calculating window sizes
    By Mox in forum Windows Programming
    Replies: 3
    Last Post: 11-08-2001, 09:17 PM