Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

A program to solve an integral

  1. Dec 3, 2008 #1
    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?

    [tex]\int^{1}_{0}[/tex] [tex]\frac{4}{1 + x^{2}}[/tex]dx
  2. jcsd
  3. Dec 3, 2008 #2
    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)

    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.
  4. Dec 4, 2008 #3


    User Avatar
    Science Advisor

    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.
  5. Dec 4, 2008 #4
    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.
  6. Dec 5, 2008 #5
    What is para comparison if that is not a rude question ?
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook