Originally Posted by
Salem
Perhaps post your main() as well, so we can see what parameters you pass to this function.
We could also run the same test as you as well.
Code:
double simpsons ( double a, int n, double b, double ans);
double Anal_function (double a, double b);
int main ()
{
double a, b, h, ans, Anal_ans; ////variables to the function
int n; ///variable to the function (n should be an integer)
printf ("Please enter Start value (a):");
scanf ("%lf", &a);
printf ("Please enter End value (b);");
scanf ("%lf", &b);
printf("Please enter number of segments (n);");
scanf ("lf", &n);
h= (b-a)/2.0; ///defining segment size
ans= simpsons (a, b, h, n); //// ans is equal to the simpsons function, that is defined later)
Anal_ans= Anal_function (a, b); //// here we linked the two variables that are defined differently.
printf ("The value of Integral according to Simpson's Rule= %0.8f\n",ans);
printf ("The value from the Analytical method = %0.8f\n", Anal_ans);
system ("pause");
return 0;
}
double simpsons ( double a, int n, double b, double h)
{
int i;
double ans[10000], x=a, final_ans, ans1, holding=0;
for ( i = 0; i <= n-1; i++ )
{
ans[i] = exp (2*x); ///function to be integrated e^2x ans[] is an array of all x values.
x = x + h;
}
for ( i = 1; i <= n-2; i+=2 )
{
ans1 = 4*ans[i];
holding = holding + ans1;
}
h= (b+a)/2.0;
final_ans = (h/3)*(ans[0] + holding + ans[n-1]);
return final_ans;
}
double Anal_function (double a, double b)
{
double Anal_ans;
Anal_ans = (0.5*(exp(2*b)))-(0.5*(exp(2*a)));
return Anal_ans;
}
what im trying here is to do the simpson's rule with 3 points only, the start value, end value and the midpoint.
where f(x) dx= ((b-a)/6)* [f(a) + 4f*(a+b/2) + f(b)].