# A program to solve an integral

1. Dec 3, 2008

### bethi309

I'm wondering if anyone know how to write a program to solve a simple integral when input a number of subintervals. this program can be written with any language, C++, java, fortran, C, etc...

this is an obvious integral to solve, but i have no idea how to write a program for it. the answer is pi, but how to write a program to get this answer when entered a subinterval of 400,000,000?

$$\int^{1}_{0}$$ $$\frac{4}{1 + x^{2}}$$dx

2. Dec 3, 2008

### variety

Just use the (approximate) definition of the integral. Partition the set [0,1] into however many intervals of width dx. Then take a value of f(x) within each interval (it can be in middle or endpoints or wherever in the interval), and multiple it to dx. Then sum them all up for each interval. It should look something like this:

a=0; b=1; dx=(b-a)/n; sum=0;
for(i=a, i<b, i+=dx)
{
sum=sum+f(i)*dx;
}

In this case I am taking the values at the left of the interval and n is the number of subintervals. I hope I didn't make a mistake but it should look something like that.

3. Dec 4, 2008

### HallsofIvy

Staff Emeritus
Are you asking for a program to do a numerical integration (very easy) or a program to do an "algebraic" integral (very hard)?

Variety gave an example of a simple example of a numerical integration, though using Simpson's rule is not really harder to program and is faster for a given accuracy.

4. Dec 4, 2008

### bethi309

I was just asking for a program to do a numerical integration. variety gave me an idea of what to do, I got it figured out on visual basics. I'm wondering if there's a way to do this using para comparison.

5. Dec 5, 2008

### CylonMath

What is para comparison if that is not a rude question ?

Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?