# Fortran Riemann Sum

1. Nov 16, 2009

### tactical

Hello, for my FORTRAN class it wants me to use the method of Riemann's sums to find the area under the curve for the function f(x) = -(x-3)**2 +9, and stop when successive iterations yield a change of less than 0.0000001. I know im going to have to used double precision. Im just confused on how to set it up. This is what I am thinking...

Im going to need a loop from 1 to count, then my deltaX will be upperbound-lowerbound/count.
Then Inside that do loop im going to need another one that does from 1 to 6 by iterations of deltaX. Then I am going to need to compute the Area(where I am stumped). After that I am going to want to say if the area after minus the area before is greater then 0.0000001 then count=count + 1. So far this is what my do loops look like. Im just wondering if i am headed in the correct direction of not? Can anyone help me please?!?!

DO I=1,COUNT,1
deltaX = (upperBOUND-lowerBOUND)/COUNT
DO J = 0,6,deltaX
F_X=-(J-3)**2 + 9
ENDDO
ENDDO

2. Nov 16, 2009

### Staff: Mentor

You have a long way to go.

You don't have anything that actually computes the area (which you acknowledged in your post), but there are several ways this could be done, including the following three.

1. Use the function value at the left endpoint of the subinterval.
2. Use the function value at the right endpoint of the subinterval.
3. Use the function value at the midpoint of the subinterval.
For each subinterval [x_i, x_i + deltaX], the area is f(x)*deltaX, where x can be the left endpoint, the right endpoint, or the midpoint. You'll want to have a loop that adds all of these areas together to get the total area. And, you'll want another loop outside of that one that keeps track of how much the area has changed between the two runs. When the difference between the two runs is smaller than the specified tolerance, you're done.

3. Dec 1, 2009

### DueceDuece

I also have a problem very similar to this. I have my program set up to calculate the riemann sum to whatever number of sections i want, but i need some help to write the outside loop calculating the negligible error (iterationNew-iterationOld < .0000001) and exiting. Any help would be awesome
this is what i have so far:

Program wtf
IMPLICIT NONE
REAL(kind=8)s,ans
INTEGER(kind=4) I,J,G
I=0
S=0

WRITE(*,*) 'Enter the number of sub intervals you wish to use'

DO
IF(I.LE.J)THEN
S=S+(-(3-(I*6.0/J))**2+9)
I=I+1

ELSE IF(I.GT.J) THEN
EXIT
ENDIF
ENDDO

ans=S*(6.0/J)

WRITE(*,*)'The reimann sum is',ans

END program

Last edited: Dec 1, 2009