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.