How to Write a Program to Solve an Integral with Subintervals?

  • Thread starter Thread starter bethi309
  • Start date Start date
  • Tags Tags
    Integral Program
bethi309
Messages
2
Reaction score
0
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
 
Physics news on Phys.org
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.
 
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.
 
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.
 
What is para comparison if that is not a rude question ?
 

Similar threads

Back
Top