Question

I am implementing an algorithm for school and am having problems understanding how a definite integral is represented in programming. For example I know that the Summation equation can be implemented as the following example:
enter image description here

assuming y=f(x)

if(x==0){
    y=x+1;
}else{
    for(int i = 0; i < n; i++){
        y = y + (x - 1);
    }
}

How would I then represent a numerical integral, example:
enter image description here
The equations planted here may not make mathematical sense but my objective is to implement similar equations in c# for a school programming project that I have to do in which I have to implement an algorithm which contains integrals. I have been reading that there are numerical methods to solve definite integrals such as simpson rule; would I have to use such methods to implement the equation or can an integral be represented in programming such as a loop or something of that sort?

Was it helpful?

Solution

It depends on what you are trying to do. If this was a specific implementation you could simply integrate the formula x-1 becomes (x^2)/2 - x and then return the max value minus the minimum value.

Alternatively it can be implemented as an estimation choosing an appropriate step size for dx.

decimal dx=0.1;

if(x==0){  
    y=x+1;  // could just return y=1
}else{  
    decimal tempY=0;
    for(decimal i = 3; i <= 20; i+=dx){  
        tempY += (i - 1);  
    }  
    // Either return tempY as decimal or
    y= Convert.ToInt32(tempY);
}  

OTHER TIPS

Maybe I do not understand but do you want to know how to compute an integral numerically ?

If it is that, there is plenty of methods. For a short introduction take look at : http://en.wikipedia.org/wiki/Numerical_integration

From you example you can simply do :

int f(int x) {
    if(x == 0) {
       y = x + 1;
    }else{ 
      y = computeIntegral()
    }
    return y
}

where computeIntegral() is a function you have to write in order to compute an integral.

But I think there is a problem with your function f(x) because if x is not zero f(x) as only one value. Maybe the integral boundary should depend on x ?

A last comment. If x is a floating number x == 0 is not recommended because a floating number does not have a unique binary representation because of rounding truncation.

The topic of numerical integration is large and can get extremely complex so I won't cover it all in this answer.

Simpson's rule is a method for numerical integration towards the simple and approximate (rather than either complex, accurate or both) end of the spectrum. It's not a bad choice for you to start your investigations of the topic, as it is very simple to understand and to program. I have no doubt that you could directly implement the formula as presented on Wikipedia in your favourite programming language.

Now, to tie loops and Simpson's rule together: the accuracy of the approximation that Simpson's rule makes to the true value of an integral is improved as the limits of integration (3 and 20 in your example) get closer together. So one approach that you could take would be to write a loop which calculates the integral from 3 to 4, from 4 to 5, ..., and adds them all up at the end. Integration produces areas, adding areas together produces (usually) another, bigger, area.

And to all the other experts in numerical integration, yes I know that there are other approaches, and that many of those other approaches are 'better' (in many different senses), and I know that Simpson's rule will have trouble with some (many) functions, but it's not a bad place to start.

You can't represent an integral simply with a loop, because an integral is an infinite sum. You have to either use an approximation method or modify the equation to remove the integral sign. The integral in your second equation should be easy to remove.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top