www.main.lv
Don't think just code it

2010-11-09 Numerical integration. Simpson method

Simpsons method not to compilcated to calculate numerical value of integral.Main point of this Simpson rules is insert values in to parabole
\[ y(x) = Ax^2+Bx+C \]
this parabole going trought points x_0, x_0+h, x_0+2*h we doubling number of points where we will calculate by 2 and this gives us enought points for this formula.
\[ \int_{x_0}^{x_0+2h} y(x)= \frac{h}{3}(y_0+4y_1+y_2) \]

#include <stdio.h>
#include <math.h>

#define TYPE float
#define NUMBER 10

TYPE integ_simpson( TYPE f(TYPE) , TYPE, TYPE, int);
TYPE fun( TYPE );

int main()
{
	printf("Result: %f\n",integ_simpson( &fun , 0.0 , 1.0 , NUMBER ));
	return 0;
}

TYPE integ_simpson( TYPE f(TYPE) , TYPE a, TYPE b, int n)
{
	int i;
	n=2*n;
	TYPE sum=f(a),h=(b-a)/n;
	for (i=1;i<=n-1;i+=2) sum += 4*f(a+h*i);
	for (i=2;i<=n-2;i+=2) sum += 2*f(a+h*i);
	sum += f(b);
	return h * sum / 3;
}

TYPE fun( TYPE x )
{
	return 1/(1+pow(x,2.0));
}