Thread: Find Area using trapozeoidal Rule

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    41

    Find Area using trapozeoidal Rule

    Find Area using trapozeoidal Rule-capture-jpg

    Above is the equation necessary for this program , now I will only post the part of the code that is required for this question , the rest is too long ad will cause confusion, it is wrong and I am totally stuck on this.

    Code:
    voidcalculateAreaTrapazoid(SHAPE *sPtr)
    {
        double time;  // time value
        int i;        // increment number
        double i_ti;   // for computing i(ti)
        double fti, ftiM1; // compute f(ti), f(ti-1)
        // Initialise values at time 0
        time = sPtr->aStart;
        sPtr->a[0] = time;
        i_ti = 0.0;
        sPtr->areaTrap[0] = 0;
        for(i = 1; i < sPtr->n; i = i +1)
        {
            sPtr->a[i] = time;
            ftiM1 = computeFa(sPtr->a[i-1], sPtr->num_steps);
            fti = computeFa(sPtr->a[i] , sPtr->num_steps);
            i_ti = i_ti + (sPtr->num_steps/2)*(ftiM1 + fti);  // note that in the expression, i_ti is I(ti-1)
            sPtr->areaTrap[i] = i_ti;
            time = time + sPtr->inc;
        }
    
    
    
    
    
    
    }
    
    
    /*----------------------------------------------------------
    Function: computeFa
    Parameters:
        a - Dimension a of the scrapper
        num_steps - number of steps for determining h
    Description: Computes the value of fA(a) for the dimension a
                 by applying the Trapezoidal rule for integrating
                 f(x) from -a to +a.
    -------------------------------------------------------------*/
    double computeFa(double a, int num_steps)
    {
      int i;
      double fx = 0;
    
    
        for(i=-a ; i < num_steps ; i = i+a)
        {
            fx = fx + (a*a - i*i)*exp(-i/a);
    
    
        }
    
    
    return(fx);
    
    
    
    
    
    
    
    
    
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    How many times does the loop in computeFa actually run?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2017
    Posts
    41
    num_steps times , num_steps is an input from the user , here is the full code I hope this will help you get a better idea of the problem
    Code:
    /*-------------------------------------------------
     File: TrapezoidShapeArea.c
     Description: Calculates the area of a shape
                  using Trapazoidal rule for integration.
    -------------------------------------------------*/
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include "gng1106plplot.h"
    #include "plplot.h"
    // Define symbolic constant
    #define MAX_SIZE 500  // maximum size of arrays
    // Structure definitions
    typedef struct
    {
        // INPUT from the user
        double aStart;  // Initial value of dimension a for plotting
        double aEnd;    // Final value of dimension a for plotting
        double inc;     // incrementation for value of a
        int num_steps;  //  number of steps to determine h (Trapezoidal Rule)
        // Calculated values
        int n;       // number of points a/fA(a) to compute must
        // be less than MAX_SIZE
        double a[MAX_SIZE];  // values  of dimension a
        double area[MAX_SIZE];  // area using analytical equation
        double areaTrap[MAX_SIZE]; // area using Trapezoidal Rule
    }  SHAPE;
    
    
    // function prototypes
    void getUserInput(SHAPE *);
    double getPositiveValue(char *);
    void calculateAreaAnalytical(SHAPE *);
    void calculateAreaTrapazoid(SHAPE *);
    double computeFa(double, int );
    void plot(SHAPE *);
    double getMin(double *, int);
    double getMax(double *, int);
    
    
    /*--------------------------------------------
    Function: main
    Description:  Overall control of the program.
    Gets the input from the user, calculates areas
    using analytical solution and using Trapazoidal rule,
    and plot the 2 curves.
    ----------------------------------------------*/
    void main()
    {
        SHAPE shape;  // Input and output data
    
    
        // Get the user input
        getUserInput(&shape);
        // Calculations
        calculateAreaAnalytical(&shape);
        calculateAreaTrapazoid(&shape);
        // Plotting
        plot(&shape);
        printf("All done.");
    }
    
    
    /*----------------------------------------------------------
    Function: getUserInput
    Parameters:
        pPtr - reference to SHAPE structure variable.  Members used
                num_steps - number of steps for determining h
                aStart - starting value for a
                aEnd - end value for a
                inc - incrementation value for dimension a
                n - number of elements used in time/area arrays
    Description: Gets from the user values for range of values for
                 a, an incremetation value for and the number of steps
                 to be used with the Trapezoidal rune (determines h)
                 and stores in appropriate variables.
                 Ensures that aStart is less than aEnd. Computes n
                 and ensures that it is less than MAX_SIZE.
    -------------------------------------------------------------*/
    void getUserInput(SHAPE *pPtr)
    {
        // Get input
        pPtr->num_steps = getPositiveValue("number of steps for defining h");
        do
        {
            pPtr->aStart = getPositiveValue("the start value for a");
            pPtr->aEnd = getPositiveValue("the end value for a");
            pPtr->inc = getPositiveValue("incrementation of a");
            pPtr->n = 1+(pPtr->aEnd - pPtr->aStart)/pPtr->inc;
            if(pPtr->n > MAX_SIZE)
                printf("Incrementation too small for range of dimension a (%d)\n", pPtr->n);
            if(pPtr->n < 0)
                printf("The end value for a must be larger than its start value\n");
        }
        while(pPtr->n > MAX_SIZE || pPtr->n <= 0);
    }
    
    
    /*----------------------------------------------------------
    Function: getPositiveValue
    Parameters:
        prompt - reference to string to include in the user prompt
    Returns
        value: positive value obtained from the user.
    Description: Prompt the user for a value (using the prompt string)
        and checks that the value is positive.
    -------------------------------------------------------------*/
    double getPositiveValue(char *prompt)
    {
        double value; // Value entered by the user.
        do
        {
            printf("Please enter a value for %s: ", prompt);
            scanf("%lf",&value);
            if(value <= 0.0)
                printf("The value must be greater than zero.\n");
        }
        while(value <= 0.0);
        return(value);
    }
    
    
    /*----------------------------------------------------------
    Function: calculateAreaAnalytical
    Parameters:
        sPtr - reference to SHAPE structure variable.  Members used
                aStart - starting value for a
                inc - incrementation value for dimension a
                n - number of elements used in time/area arrays
                a - array for saving values of a
                area - array for saving area values
    Description: Fills in the arrays with n points of
        a/fA(a) values using the analytical solution for the
        area fA(a).
    -------------------------------------------------------------*/
    void calculateAreaAnalytical(SHAPE *sPtr)
    {
        double aValue;
        int ix;
        aValue = sPtr->aStart;
        for(ix = 0; ix < sPtr->n; ix = ix + 1)
        {
    
    
            sPtr->a[ix] = aValue;
            sPtr->area[ix] = 4.0*pow(aValue,3)/exp(1);
            aValue = aValue + sPtr->inc;
        }
    }
    
    
    /*----------------------------------------------------------
    Function: calculateAreaTrapazoid
    Parameters:
        sPtr - reference to SHAPE structure variable.  Members used
                num_steps - number of steps for determining h
                aStart - starting value for a
                aEnd 0 ending value of a
                inc - incrementation value for dimension a
                n - number of elements used in time/area arrays
                a - array for saving values of a
                areaTrap - array for saving area values
    Description: Fills in the arrays with n points of
        a/area values using the Trapezoidal rule for the
        distance.
    -------------------------------------------------------------*/
    void calculateAreaTrapazoid(SHAPE *sPtr)
    {
        double time;  // time value
        int i;        // increment number
        double i_ti;   // for computing i(ti)
        double fti, ftiM1; // compute f(ti), f(ti-1)
        // Initialise values at time 0
        time = sPtr->aStart;
        sPtr->a[0] = time;
        i_ti = 0.0;
        sPtr->areaTrap[0] = 0;
        for(i = 1; i < sPtr->n; i = i +1)
        {
            sPtr->a[i] = time;
            time = time + sPtr->inc ;
            ftiM1 = computeFa(sPtr->a[i-1], sPtr->num_steps);
            fti = computeFa(sPtr->a[i] , sPtr->num_steps);
            i_ti = i_ti + (sPtr->num_steps/2)*(ftiM1 + fti);  // note that in the expression, i_ti is I(ti-1)
            sPtr->areaTrap[i] = i_ti/12.5;
    
    
        }
    
    
    
    
    
    
    }
    
    
    /*----------------------------------------------------------
    Function: computeFa
    Parameters:
        a - Dimension a of the scrapper
        num_steps - number of steps for determining h
    Description: Computes the value of fA(a) for the dimension a
                 by applying the Trapezoidal rule for integrating
                 f(x) from -a to +a.
    -------------------------------------------------------------*/
    double computeFa(double a, int num_steps)
    {
      int i;
      double fx = 0;
    
    
        for(i=-a ; i < num_steps ; i = i+a)
        {
            fx = fx + (a*a - i*i)*exp(-i/a);
    
    
        }
    
    
    return(fx);
    
    
    
    
    
    
    
    
    
    
    }
    
    
    /*----------------------------------------------------------
    Function: plot
    Parameters:
        sPtr - reference to SHAPE structure variable.  Members used
                aStart - starting value for dimension a
                aEnd - end value for dimension a
                n - number of elements used in time/area arrays
                a - array for saving values of a
                areaTrap - array for saving area values
    Description: Initilialises the plot device, pen width,
                 and plots both cuvers for the analytical and the
                 Euler method.
    ----------------------------------------------------------------*/
    void plot(SHAPE *sPtr)
    {
        double maxArea;   // maximum area
        double temp;
        char plotLabel[100];
    
    
        // Find the maximum distance to scale the distance axis
        maxArea = getMax(sPtr->area, sPtr->n);
        temp = getMax(sPtr->areaTrap, sPtr->n);
        if(temp > maxArea) maxArea = temp;
        maxArea = 1.1*maxArea;
        // Initiliaise the PLplot page
        plsdev("wingcc");  // Sets device to wingcc - CodeBlocks compiler
        // Initialise the plot
        plinit();
        plwidth(2);    // pen width
        plenv(sPtr->aStart, sPtr->aEnd, 0, maxArea, 0, 0);
        plcol0(GREEN);           // Select color for labels
        sprintf(plotLabel, "Part area (num steps = %d)",
                sPtr->num_steps);
        pllab("Dimension a", "Area fA(a)", plotLabel);
        // Plot the analytical curve
        plcol0(RED);
        pllsty(SOLID);
        plline(sPtr->n, sPtr->a, sPtr->area);
        plptex(0.1*(sPtr->aEnd - sPtr->aStart) + sPtr->aStart, 0.9*maxArea,
               0, 0, 0, "Analytical");
        // Plot the Trapezoidal curve
        plcol0(BLUE);
        pllsty(LNGDASH_LNGGAP);
        plline(sPtr->n, sPtr->a, sPtr->areaTrap);
        plptex(0.3*(sPtr->aEnd - sPtr->aStart) + sPtr->aStart, 0.9*maxArea,
               0, 0, 0, "Trapazoid");
        plend();
    }
    
    
    
    
    /*----------------------------------------------------------
    Function: getMin
    Parameters:
        array - reference to an array with double values
        n - number of elements in the array
    Returns
        min:  the minimum value found in the array
    Description: Traverses the array to find its minimum value.
    ----------------------------------------------------------------*/
    double getMin(double *array, int n)
    {
        int ix;
        double min = array[0];
        for(ix = 1; ix < n; ix = ix +1)
            if(min > array[ix]) min = array[ix];
        return(min);
    }
    
    
    /*----------------------------------------------------------
    Function: getMax
    Parameters:
        array - reference to an array with double values
        n - number of elements in the array
    Returns
        max:  the maximum value found in the array
    Description: Traverses the array to find its maximum value.
    ----------------------------------------------------------------*/
    double getMax(double *array, int n)
    {
        int ix;
        double max = array[0];
        for(ix = 1; ix < n; ix = ix +1)
            if(max < array[ix]) max = array[ix];
        return(max);
    }

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    The code doesn't appear to be calculating the area of each trapezoid. Using double deltax = 2.*a/num_steps, the area of the first trapezoid is delta_area = ((f(-a) + f(-a+deltax))/2.)*deltax;.

    The total area using integration is 4.*pow(a,3)/e, where e = exp(1.) == 2.7182818284590452353602874713527 .
    Last edited by rcgldr; 11-18-2017 at 06:52 PM.

  5. #5
    Registered User
    Join Date
    Sep 2017
    Posts
    41
    can you please edit the code because i dont understand what you mean by
    "deltax = 2.*a/num_steps, the area of the first trapezoid is delta_area = ((f(-a) + f(-a+deltax))/2.)*deltax;"

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If a is 2, and numsteps is 100, how many times does your loop execute?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Sep 2017
    Posts
    41
    99 times , it's fine tho i figured it out thanks for the "help"

  8. #8
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    What is the purpose of "sPtr->areaTrap[i] = i_ti/12.5;" ? The trapezoidal part looks different than what I've usually seen before.

  9. #9
    Registered User
    Join Date
    Sep 2017
    Posts
    41
    that part was wrong i edited it out there shouldn't be /12.5 , the code above is wrong

  10. #10
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by Kamal Joub View Post
    that part was wrong i edited it out there shouldn't be /12.5 , the code above is wrong
    I made an example program, that outputs the results instead of plotting them, using a conventional and non-optimized trapezoidal calculation. I'm curious what the differences are with your version.

    Code:
    // trp.c trapezoidal test program
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    // Define symbolic constant
    #define MAX_SIZE 500  // maximum size of arrays
    // Structure definitions
    typedef struct
    {
        // INPUT from the user
        double aStart;  // Initial value of dimension a for plotting
        double aEnd;    // Final value of dimension a for plotting
        double inc;     // incrementation for value of a
        int num_steps;  //  number of steps to determine h (Trapezoidal Rule)
        // Calculated values
        int n;       // number of points to compute
        double a[MAX_SIZE];  // values  of dimension a
        double area[MAX_SIZE];  // area using analytical equation
        double areaTrap[MAX_SIZE]; // area using Trapezoidal Rule
    }  SHAPE;
    
    // function prototypes
    void getUserInput(SHAPE *);
    double getPositiveValue(char *);
    void calculateAreaAnalytical(SHAPE *);
    void calculateAreaTrapezoid(SHAPE *);
    double computeFa(double, double );
    
    /*--------------------------------------------
    Function: main
    Description:  Overall control of the program.
    Gets the input from the user, calculates areas
    using analytical solution and using Trapezoidal rule,
    and plot the 2 curves.
    ----------------------------------------------*/
    int main()
    {
    int i;
        SHAPE shape;  // Input and output data
        // Get the user input
        getUserInput(&shape);
        // Calculations
        calculateAreaAnalytical(&shape);
        calculateAreaTrapezoid(&shape);
        // output 
        for(i = 0; i < shape.n; i++)
            printf("%20.12lf %20.12lf\n", shape.area[i], shape.areaTrap[i]);
        return 0;
    }
    
    /*----------------------------------------------------------
    Function: getUserInput
    Parameters:
        pPtr - reference to SHAPE structure variable.  Members used
                num_steps - number of steps for determining h
                aStart - starting value for a
                aEnd - end value for a
                inc - incrementation value for dimension a
                n - number of elements used in time/area arrays
    Description: Gets from the user values for range of values for
                 a, an incremetation value for and the number of steps
                 to be used with the Trapezoidal rune (determines h)
                 and stores in appropriate variables.
                 Ensures that aStart is less than aEnd. Computes n
                 and ensures that it is less than MAX_SIZE.
    -------------------------------------------------------------*/
    void getUserInput(SHAPE *pPtr)
    {
        // Get input
        pPtr->num_steps = (int)getPositiveValue("number of steps for defining h");
        do
        {
            pPtr->aStart = getPositiveValue("the start value for a");
            pPtr->aEnd = getPositiveValue("the end value for a");
            pPtr->inc = getPositiveValue("incrementation of a");
            pPtr->n = (int)(1+(pPtr->aEnd - pPtr->aStart)/pPtr->inc);
            if(pPtr->n > MAX_SIZE)
                printf("Incrementation too small for range of dimension a (%d)\n", pPtr->n);
            if(pPtr->n < 0)
                printf("The end value for a must be larger than its start value\n");
        }
        while(pPtr->n > MAX_SIZE || pPtr->n <= 0);
        printf("\n");
    }
    
    /*----------------------------------------------------------
    Function: getPositiveValue
    Parameters:
        prompt - reference to string to include in the user prompt
    Returns
        value: positive value obtained from the user.
    Description: Prompt the user for a value (using the prompt string)
        and checks that the value is positive.
    -------------------------------------------------------------*/
    double getPositiveValue(char *prompt)
    {
        double value; // Value entered by the user.
        do
        {
            printf("Please enter a value for %s: ", prompt);
            scanf("%lf",&value);
            if(value <= 0.0)
                printf("The value must be greater than zero.\n");
        }
        while(value <= 0.0);
        return(value);
    }
    
    /*----------------------------------------------------------
    Function: calculateAreaAnalytical
    Parameters:
        sPtr - reference to SHAPE structure variable.  Members used
                aStart - starting value for a
                inc - incrementation value for dimension a
                n - number of elements used in time/area arrays
                a - array for saving values of a
                area - array for saving area values
    Description: Fills in the arrays with n points of
        a/fA(a) values using the analytical solution for the
        area fA(a).
    -------------------------------------------------------------*/
    void calculateAreaAnalytical(SHAPE *sPtr)
    {
        double aValue;
        int ix;
        for(ix = 0; ix < sPtr->n; ix = ix + 1)
        {
            aValue = sPtr->aStart + ix * sPtr->inc;
            sPtr->a[ix] = aValue;
            sPtr->area[ix] = 4.0*pow(aValue,3)/exp(1);
            aValue = aValue + sPtr->inc;
        }
    }
    
    /*----------------------------------------------------------
    Function: calculateAreaTrapezoid
    Parameters:
        sPtr - reference to SHAPE structure variable.  Members used
                num_steps - number of steps for determining h
                aStart - starting value for a
                aEnd 0 ending value of a
                inc - incrementation value for dimension a
                n - number of elements used in time/area arrays
                a - array for saving values of a
                areaTrap - array for saving area values
    Description: Fills in the arrays with n points of
        a/area values using the Trapezoidal rule for the
        distance.
    -------------------------------------------------------------*/
    void calculateAreaTrapezoid(SHAPE *sPtr)
    {
    int i, j;
    double a;
    double x0, x1;   // start, end x values for each trapezoid
        for(j = 0; j < sPtr->n; j++){
            a = sPtr->aStart + j * sPtr->inc;
            sPtr->areaTrap[j] = 0.;
            // non-optimized trapezoidal calculation
            for(i = 1; i < sPtr->num_steps; i++){
                x0 = -a + 2. * a *(i-1) / sPtr->num_steps;
                x1 = -a + 2. * a *(i  ) / sPtr->num_steps;
                sPtr->areaTrap[j] += (computeFa(a,x0) + computeFa(a,x1)) / 2. * (x1-x0);
            }
        }
    }
    
    /*----------------------------------------------------------
    Function: computeFa
    -------------------------------------------------------------*/
    double computeFa(double a, double x)
    {
        return((a*a - x*x)*exp(-x/a));
    }
    Last edited by rcgldr; 11-20-2017 at 12:56 AM.

  11. #11
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by rcgldr View Post
    Code:
    // ...
    void calculateAreaTrapezoid(SHAPE *sPtr)
    // ...
            for(i = 1; i < sPtr->num_steps; i++){
    // ...
    That should have been
    Code:
    // ...
            for(i = 1; i <= sPtr->num_steps; i++){
    // ...
    A bit more cleanup:

    Code:
    void calculateAreaTrapezoid(SHAPE *sPtr)
    {
    int i, j;
    double a;
    double x0, x1;   // start, end x values for each trapezoid
    double f0, f1;
        for(j = 0; j < sPtr->n; j++){
            a = sPtr->aStart + j * sPtr->inc;
            sPtr->areaTrap[j] = 0.;
            x1 = -a;
            f1 = computeFa(a,x1);
            for(i = 1; i <= sPtr->num_steps; i++){
                x0 = x1;
                f0 = f1;
                x1 = -a + 2. * a * i / sPtr->num_steps;
                f1 = computeFa(a,x1);
                sPtr->areaTrap[j] += (f0 + f1) / 2. * (x1-x0);
            }
        }
    }

  12. #12
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    I needed a bit more clean up, and it's too late to edit prior post. So here's another example. I added Simpson rule to the code for comparison.

    Code:
    // cpsmptrp.c - simpson / trapezoid test program
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    // Define symbolic constant
    #define MAX_SIZE 500  // maximum size of arrays
    // Structure definitions
    typedef struct
    {
        // INPUT from the user
        double aStart;  // Initial value of dimension a for plotting
        double aEnd;    // Final value of dimension a for plotting
        double aInc;    // incrementation for value of a
        int n;          // number of points to compute
        int num_steps;  // number of steps to integrate
        // Calculated values
        double a[MAX_SIZE];        // values of a
        double area[MAX_SIZE];     // area using analytical equation
        double areaSimp[MAX_SIZE]; // area using Simpson Rule
        double areaTrap[MAX_SIZE]; // area using Trapezoidal Rule
    }SHAPE;
    
    // function prototypes
    void getUserInput(SHAPE *);
    double getPositiveValue(char *);
    void calculateAreaAnalytical(SHAPE *);
    void calculateAreaSimpson(SHAPE *);
    void calculateAreaTrapezoid(SHAPE *);
    double computeF(double, double );
    
    /*--------------------------------------------
    Function: main
    Description:  Overall control of the program.
    Gets the input from the user, calculates areas
    ----------------------------------------------*/
    int main()
    {
    int i;
        SHAPE shape;  // Input and output data
        // Get the user input
        getUserInput(&shape);
        // Calculations
        calculateAreaAnalytical(&shape);
        calculateAreaSimpson(&shape);
        calculateAreaTrapezoid(&shape);
        // output 
        for(i = 0; i < shape.n; i++)
            printf("%12.4lf %20.12lf %20.12lf %20.12lf\n",
                   shape.a[i], shape.area[i], shape.areaSimp[i], shape.areaTrap[i]);
        return 0;
    }
    
    /*----------------------------------------------------------
    Function: getUserInput
    Parameters:
        pPtr - reference to SHAPE structure variable.  Members used
                num_steps - number of steps for integration
                aStart - starting value for a
                aEnd - end value for a
                aInc - increment for a
                n - number of elements of a[] used 
    Description: Gets from the user values for range of values for
                 a, an incremetation value for and the number of steps
                 to be used with the Trapezoidal rune (determines h)
                 and stores in appropriate variables.
                 Ensures that aStart is less than aEnd. Computes n
                 and ensures that it is less than MAX_SIZE.
    -------------------------------------------------------------*/
    void getUserInput(SHAPE *pPtr)
    {
        // Get input
        pPtr->num_steps = (int)getPositiveValue("number of steps for defining h");
        do
        {
            pPtr->aStart = getPositiveValue("the start value for a");
            pPtr->aEnd = getPositiveValue("the end value for a");
            pPtr->aInc = getPositiveValue("incrementation of a");
            pPtr->n = (int)(1+(pPtr->aEnd - pPtr->aStart)/pPtr->aInc);
            if(pPtr->n > MAX_SIZE)
                printf("Incrementation too small for range of dimension a (%d)\n", pPtr->n);
            if(pPtr->n < 0)
                printf("The end value for a must be larger than its start value\n");
        }
        while(pPtr->n > MAX_SIZE || pPtr->n <= 0);
        printf("\n");
    }
    
    /*----------------------------------------------------------
    Function: getPositiveValue
    Parameters:
        prompt - reference to string to include in the user prompt
    Returns
        value: positive value obtained from the user.
    Description: Prompt the user for a value (using the prompt string)
        and checks that the value is positive.
    -------------------------------------------------------------*/
    double getPositiveValue(char *prompt)
    {
        double value; // Value entered by the user.
        do
        {
            printf("Please enter a value for %s: ", prompt);
            scanf("%lf",&value);
            if(value <= 0.0)
                printf("The value must be greater than zero.\n");
        }
        while(value <= 0.0);
        return(value);
    }
    
    /*----------------------------------------------------------
    Function: calculateAreaAnalytical
    Parameters:
        sPtr - reference to SHAPE structure variable.  Members used
                aStart - starting value for a
                aInc - incrementation value for dimension a
                n - number of elements used in time/area arrays
                a - array for saving values of a
                area - array for saving area values
    Description: Fills in area[] using analytical solution
    -------------------------------------------------------------*/
    void calculateAreaAnalytical(SHAPE *sPtr)
    {
        double a;
        int i;
        for(i = 0; i < sPtr->n; i = i + 1)
        {
            a = sPtr->aStart + i * sPtr->aInc;
            sPtr->a[i] = a;
            sPtr->area[i] = 4.0 * pow(a,3) / exp(1);
        }
    }
    
    /*----------------------------------------------------------
    Function: calculateAreaSimpson
    Parameters:
        sPtr - reference to SHAPE structure variable.  Members used
                num_steps - number of steps for determining h
                aStart - starting value for a
                aEnd - ending value of a
                aInc - incrementation value for dimension a
                n - number of elements used in time/area arrays
                a - array for saving values of a
                areaSimp - array for saving area values
    Description: Fills in areaSimp[] using Simpson rule
    -------------------------------------------------------------*/
    void calculateAreaSimpson(SHAPE *sPtr)
    {
    int i, j;
    double a;
    double x0, x1, xm;  // start, end, mid x values for each trapezoid
    double f0, f1, fm;
        for(j = 0; j < sPtr->n; j++){
            a = sPtr->aStart + j * sPtr->aInc;
            sPtr->areaSimp[j] = 0.;
            x1 = -a;
            f1 = computeF(a,x1);
            for(i = 1; i <= sPtr->num_steps; i++){
                x0 = x1;
                f0 = f1;
                x1 = -a + 2. * a * i / sPtr->num_steps;
                xm = (x0 + x1) / 2.;
                f1 = computeF(a,x1);
                fm = computeF(a,xm);
                sPtr->areaSimp[j] += (f0 + 4. * fm + f1) / 6. * (x1-x0);
            }
        }
    }
    
    /*----------------------------------------------------------
    Function: calculateAreaTrapezoid
    Parameters:
        sPtr - reference to SHAPE structure variable.  Members used
                num_steps - number of steps for determining h
                aStart - starting value for a
                aEnd - ending value of a
                aInc - incrementation value for dimension a
                n - number of elements used in time/area arrays
                a - array for saving values of a
                areaTrap - array for saving area values
    Description: Fills in the areaTrap[] using Trapezoidal rule
    -------------------------------------------------------------*/
    void calculateAreaTrapezoid(SHAPE *sPtr)
    {
    int i, j;
    double a;
    double x0, x1;   // start, end x values for each trapezoid
    double f0, f1;
        for(j = 0; j < sPtr->n; j++){
            a = sPtr->aStart + j * sPtr->aInc;
            sPtr->areaTrap[j] = 0.;
            x1 = -a;
            f1 = computeF(a,x1);
            for(i = 1; i <= sPtr->num_steps; i++){
                x0 = x1;
                f0 = f1;
                x1 = -a + 2. * a * i / sPtr->num_steps;
                f1 = computeF(a,x1);
                sPtr->areaTrap[j] += (f0 + f1) / 2. * (x1-x0);
            }
        }
    }
    
    /*----------------------------------------------------------
    Function: computeF
    -------------------------------------------------------------*/
    double computeF(double a, double x)
    {
        return((a*a - x*x)*exp(-x/a));
    }
    Last edited by rcgldr; 11-20-2017 at 11:35 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-09-2015, 08:03 AM
  2. Replies: 15
    Last Post: 02-18-2013, 05:08 PM
  3. Replies: 1
    Last Post: 02-21-2010, 01:09 PM
  4. help: function, find area of triangle
    By Niz in forum C Programming
    Replies: 20
    Last Post: 01-17-2007, 02:36 PM
  5. Whats wrong with my find Circle Area program?[compiles fine]
    By Golden Bunny in forum C++ Programming
    Replies: 22
    Last Post: 06-16-2002, 02:49 PM

Tags for this Thread