My program takes in a polynomial of any degree, and outputs it back the user. I want to be able to then pass this polynomial to a subfunction where it will be evaluated using the rectangular rule process. Currently i'm writing the function and I have run into a few problems, mainly having the correct coefficient be multiplied by the polynomial. I highlighted the parts of my program that deal with the rectangular rule.
Now for the rectangular rule, for instance if I had a function that was just x^2 to be integrated on the bounds of [0,1] with 2 subintervals, the change in x, or delta x, would be (1-0)/2, or right bound minus left bound divided by subintervals. So that means the total approximate area would be f(1/2)*deltax + f(1)*deltax, which can be simplified to [f(1/2) + f(1)] * deltax
Code:
#include <stdio.h>
#include <stdlib.h>
int menu(void);
void simpsonsrule(void);
double rectanglerule(char *, double, double, double, int);
main()
{
int j=1; //Loop for the menu
int technique; //Integration technique
int order; //Order for the polynomial
int totterms; //Total number of terms in the equation
double a; //left endpoint
double b; //right endpoint
double boundtotal; //endpoint math
double subint; //subintervals
int loopvar; //for loop variable
double *ptr;
int count;
double rectarea; //Total area for the rectangle rule
double simparea; //Total area for the simpson rule
while(j)
{
puts("Welcome to the integration program.");
puts("You can compute an integral using either Simpson's or the Rectangular rule.");
puts("\nEnter the order of the polynomial you wish to integrate.");
scanf("%d",&order);
totterms= order+1;
ptr=malloc(totterms*sizeof(double));
for(count=totterms-1;count >=0; count--)
{
if(count != 0)
{
printf("\nEnter the coefficient for the %d power: ", count);
scanf("%lf", &ptr[count]);
}
else
{
printf("\nEnter the constant: ");
scanf("%lf", &ptr[count]);
}
}
puts("\nEnter the left endpoint of the integral.");
scanf("%lf",&a);
puts("\nEnter the right endpoint of the integral.");
scanf("%lf",&b);
puts("\nEnter the number of sub-intervals you want to use.");
scanf("%lf",&subint);
boundtotal= (b-a)/subint;
printf("\nThe polynomial you entered was.\n");
for(loopvar=totterms-1;loopvar >=0;loopvar--)
{
if(loopvar != 0)
{
printf("%lf^%d + ",ptr[loopvar],loopvar);
}
else
{
printf("%lf.",ptr[loopvar],loopvar);
}
}
printf("\nIt is going to be integrated from %lf to %lf.",a,b);
printf("\nWith %lf subintervals.",subint);
puts("\nTo integrate using Simpson's Rule enter 1.");
puts("To integrate using the Rectangular Rule enter 2.");
puts("To integrate using both techniques enter 3.");
scanf("%d",&technique);
if(technique == 1)
{
}
else if(technique == 2)
{
rectarea=rectanglerule(ptr,a,b,subint,order);
printf("The total area under the curve is %lf.", rectarea);
}
else if(technique == 3)
{
}
else
{
puts("You did not enter a valid menu option, goodbye.");
}
j=menu();
}
}
int menu(void)
{
int j;
puts("To restart the program enter 1.");
puts("To quit the program enter 0.");
scanf("%d",&j);
return j;
}
void simpsonsrule(void)
{
}
double rectanglerule(char *ptr, double a, double b, double subint, int order)
{
double approxarea=0; //Approximated total area
int i;
double deltax = (b-a)/subint; //Change in x throughout the entire x-axis
double deltaxadd = (b-a)/subint; //Addition of the intervals to plug into functions
for(i=subint;i>0;i--)
{
//approxarea+=(function expression);
deltaxadd+=deltaxadd;
}
}
I'm not sure if I need to pass the order through this function as well and decrease it as the for loops continues or not.
So far I have come up with...
Code:
double rectanglerule(char *ptr, double a, double b, double subint, int order)
{
double approxarea=0; //Approximated total area
int i;
double deltax = (b-a)/subint; //Change in x throughout the entire x-axis
double deltaxadd = (b-a)/subint; //Addition of the intervals to plug into functions
double funcval=0;
printf("Order of pol is %d", order);
for(i=subint;i>0;i--)
{
for(order=order; order >= 0; order--) //order-1 because the constant can be added at the end
{
if(order !=0)
{
funcval+=(pow(&ptr[order], order));
}
else
{
funcval+=(&ptr[order]);
}
}
approxarea=funcval*deltax;
approxarea+=approxarea;
deltaxadd+=deltaxadd;
}
}
This is giving me some errors though, saying chars cant be converted to doubles...