-2

I need to make a C program that would calculate the integral of a third degree polynomial using the midpoint method.

Basically integrating this: f(x) = mx^3 + nx^2 + px + q using the block/midpoint method.

So far I have this:

#include <stdio.h>
#include <math.h>
int main(){
    char option;
    float m, n, p, q, a, b, N, S=0,T,h;//S for midpoint, T for Trap, h for interval length
    do{
        printf("\n Select one of following options:\n");
        printf("a)Enter new coefficients (m,n,p, and q)\n");
        printf("b)Enter the value for the range from a to b, and N, the number of intervals\n");
        printf("c)Calculate the integral from a to b\n");
        printf("d)Exit\n");

        option=getc(stdin);
        printf("You have selected option: \"%c\" \n", option);

        switch (option){
        case 'a':
            printf("Enter m,n,p,q (in this order):\n");
            scanf("%f%f%f%f", &m,&n,&p,&q);
            break;
        case 'b':
            printf("Enter the interval from a to b and the number of intervals N:");
            scanf("%f%f%f", &a, &b, &N);
            break;
        case 'c':
            h = (b - a) / N;
            for (float i = a; i < b - h; i + h){
                S =+(m*pow(i, 3)) + n*pow(i, 2) + p*i + q;
            }
            S = S*h;
            printf("The integral using midpoint rule: %f", S);
            break;
        case 'd':
            printf("Exit.\n");
            break;
        default:
            printf("Invalid entry, try again... \n");
            break;
        }
    } while (option != 'd');
    return 0;
}

this is the main algorithm I'm trying to use:

h = (b - a) / N;
            for (float i = a; i < b - h; i + h){
                S =+(m*pow(i, 3)) + n*pow(i, 2) + p*i + q;
            }
            S = S*h;
            printf("The integral using midpoint rule: %f", S);
            break;

but it's not working. It gets stuck in the loop part. Because after I input all the other variables and choose the c option, it just does nothing.

The algorithm starts by finding the length of interval. Then the loop adds the function per the midpoint formula, and that's where I think I did something wrong.

halfer
  • 19,824
  • 17
  • 99
  • 186
dsa22
  • 11
  • 2
  • 4
  • 4
    The appropriate response to "its not working" is "then fix it". If you can provide some details as to *how* and/or *where* it is not working, you're more likely to get a helpful response. That, and commenting your code. – Scott Hunter Sep 27 '14 at 01:06
  • Not solving it but the line `for (float i = a; i < b - h; i + h)` doesn't do what you think it does, use `for (float i = a; i < b - h; i += h)` instead – chouaib Sep 27 '14 at 01:16
  • thanks, I had a stupid moment when I did the last part of the for loop. – dsa22 Sep 27 '14 at 01:21
  • 1
    Note: `(((m*i + n)*i + p)*i + q` is numerically more stable and is faster than `(m*pow(i, 3)) + n*pow(i, 2) + p*i + q`. – chux - Reinstate Monica Sep 27 '14 at 05:16

2 Answers2

2

Generally, using a floating point variable for a loop counter is considered a bad practice because of the difficulties in comparing floating point values for equality. A work around is to use an int as the counter and multiply it by a floating-point delta:

double delta = (b - a) / N;
double start = a + delta / 2.0;

for (int i = 0; i < N; i++) {
    double x = start + delta * i;

    // do calculations with x
}
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

Fixed the loop:

h = (b - a) / N /2;
            for (float i = a+h; i < b; i=i + 2*h){
                S = S+(m*pow(i, 3)) + n*pow(i, 2) + p*i + q;
            }
            S = S*h*2;

Works perfectly now.

dsa22
  • 11
  • 2
  • 4
  • from where you got `i < 5` ? – chouaib Sep 27 '14 at 01:37
  • Fixed it. It was supposed to be b. 5 was the value I was testing with so I put it directly into the loop because I wanted to check if the b value was messing the loop up (it wasn't). – dsa22 Sep 27 '14 at 01:49
  • 1
    alright, I have one more remark: in the algorithm it says that `i` being increased by `h` per iteration but in this code I see that you increasing by `2h`!! don't you think that it's better if `h = (b-a)/N` increase by `h` then `S *= h` ?? – chouaib Sep 27 '14 at 02:08