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:
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:
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?
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);
}