I think I have a good starting point on this, but I still need some help :)
First I need to ask the user for the limits of integration and the error tolerance, (that's already done).
So when I divide the trapezoid (when the end points are connected) in 2, I get a better approximation, so based on the error tolerance I need to keep dividing the 2 trapezoids on each side of the midpoint. For example if the entered tolerance is 0.5, the error tolerance on each side of the midpoint is 0.25. Now I made a separate function for calculating the function, so I just need to figure out how to write this function recursively. The area of the 1st trapezoid, before dividing it is
(b-a)(F(b)+F(a)/2. Since it's recursion I know it has to have a starting point, check if it is the answer I need and continue dividing, but I don't know how to write the stopping point based on the tolerance. And at the end it will return itself. Hopefully someone will have the patience to read and help me! Thank you!

Recommended Answers

All 4 Replies

If you are using recursion you have to make a function that will take input int or float or double, and provide same type output.

What is condition that will stop the recursive call, how do you know when you are done calculating?

the part that will stop the recursion will have to do with the tolerance given by the user, but I really don't know how to implement it. So what I think it should do is something like, if the tolerance is 0.5, the whole trapezoid should be divided by 4 (divide each side of the midpoint by 2), sum the result and return it.

There are two logical endpoints to stop calculations. One is based on the width of the boxes. The other is on the variation in the area under the curve; that is, decrease the width of the boxes as much as you need to until the change in the area under curve (call it delta) from one iteration to the other is under some arbitrary value.

If we use delta to stop calculations, then each new iteration needs to know the the current value of w and the previous area under the curve based on the previous size of w.

void func(double w, double priorAreaUnderCurve)
{
   double currentAreaUnderCurve = 0.0;

   //now calculate current area under curve between a and b if the boxes used to calculate the area have width w 
     
  if the absolute value of the currentAreaUnderCurve minus priorAreaUnderCurve) is above delta
     then call func passing w/2 and currentAreaUnderCurve as arguments
  else
     output result
}

Dimitar: I am afraid you are moving in a wrong direction. In a numerical analysis a "recursive integration" usually refers to a Romberg algorithm. Check it with your teacher before it is too late.

if the absolute value of the currentAreaUnderCurve minus priorAreaUnderCurve) is above delta
     then call func passing w/2 and currentAreaUnderCurve as arguments

I would grade this solution F. This approach suffers from early termination thus producing incorrect results, and the recursion is, let's put it mildly, artificial.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.