This code is using Simpson's rule to calculate the integral of x*sin(x) with the bounds of (1,2). The problem I am having is, while its getting very close to the actual value. Even with 999 iterations, it still doesn’t hit the point. While I have a separate program that uses trapezoidal rule for the same thing, and it DOES hit the point exactly after 1000 iterations. The point it should hit is "1.440422"
Is that what should be happening for Simpson's rule? Or is there something wrong with my code?
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
double f(double x);
int main()
{
double x,result,y,z,h,s1,s2;
s1 = 0;
s2 = 0;
int i,n;
printf("\nHow many points to you want it evaluated at (odd number)? And what are the bounds? lower bound,upper bound >\n");
scanf("%d %lf,%lf",&n,&y,&z);
h = (z-y)/n;
result = 0;
if(n%2!=0)
{
for(i=0;i<n;i++)
{
if(i%2==0)
{
s1 = s1+f(y+i*h);
}
else
{
s2 = s2+f(y+i*h);
}
}
result = (h/3)*(f(y)+f(z)+4*s2+2*s1);
printf("\nThe value is %lf with %d interations\n",result,i);
}
else
{
printf("\n The number of points has to be odd, try again\n");
}
}
double f(double x)
{
return(x*sin(x));
}