Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6651255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:59:21+00:00 2026-05-26T00:59:21+00:00

I am attempting to approximate integrals using an adaptive Trapezoidal Rule. I have a

  • 0

I am attempting to approximate integrals using an adaptive Trapezoidal Rule.

I have a coarse integral approximation:

//Approximates the integral of f across the interval [a,b]
double coarse_app(double(*f)(double x), double a, double b) {
    return (b - a) * (f(a) + f(b)) / 2.0;
}

I have a fine integral approximation:

//Approximates the integral of f across the interval [a,b]
double fine_app(double(*f)(double x), double a, double b) {
    double m = (a + b) / 2.0;
    return (b - a) / 4.0 * (f(a) + 2.0 * f(m) + f(b));
}

This is made adaptive by summing the approximation across decreasing portions of the given interval until either the recursion level is too high or the coarse and fine approximation are very close to one another:

//Adaptively approximates the integral of f across the interval [a,b] with
//    tolerance tol.
double trap(double(*f)(double x), double a, double b, double tol) {
    double q = fine_app(f, a, b);
    double r = coarse_app(f, a, b);
    if ((currentLevel >= minLevel) && (abs(q - r) <= 3.0 * tol)) {
        return q;
    } else if (currentLevel >= maxLevel) {
        return q;
    } else {
        ++currentLevel;
        return (trap(f, a, b / 2.0, tol / 2.0) + trap(f, a + (b / 2.0), b, tol / 2.0));
    }
}

If I manually calculate an integral by breaking it up into sections and using fine_app on it, I get a very good approximation. However, when I use the trap function, which should do this for me, all of my results are far too small.

For example, trap(square, 0, 2.0, 1.0e-2) gives the output 0.0424107, where the square function is defined as x^2. However, the output should be around 2.667. This is far worse than doing a single run of fine_app on the entire interval, which gives a value of 3.

Conceptually, I believe I have it implemented correctly, but there is something about C++ recursion which is not doing what I expect it to.

First time programming in C++, so all improvements are welcome.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:59:22+00:00Added an answer on May 26, 2026 at 12:59 am

    I’m assuming you have currentLevel defined somewhere else. You don’t want to do that. You also calculate your midpoints incorrectly.

    Take a = 3, b = 5:

    [a, b / 2.0] = [3, 2.5]
    [a + b / 2.0, b] = 2.5, 3]
    

    The correct points should be [3, 4] and [4, 5]

    The code should look like this:

    double trap(double(*f)(double x), double a, double b, double tol, int currentLevel) {
        double q = fine_app(f, a, b);
        double r = coarse_app(f, a, b);
        if ((currentLevel >= minLevel) && (abs(q - r) <= 3.0 * tol)) {
            return q;
        } else if (currentLevel >= maxLevel) {
            return q;
        } else {
            ++currentLevel;
            return (trap(f, a, (a + b) / 2.0, tol / 2, currentLevel) + trap(f, (a + b) / 2.0, b, tol / 2, currentLevel));
        }
    }
    

    You can add a helper function so you don’t have to specify currentLevel:

     double integrate(double (*f)(double x), double a, double b, double tol)
     {
         return trap(f, a, b, tol, 1);
     }
    

    If I call this as integrate(square, 0, 2, 0.01) I get the answer of 2.6875, which means you need an even lower tolerance to converge to the correct result of 8/3 = 2.6666...7. You can check the exact error bound on this by using the error terms for Simpson’s method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to style some html that I do not have control over.
I'm attempting to compile a working copy of the MagickNet class library (DLL) using
I have two large data sets and I am attempting to reformat the older
Attempting to profile the difference in runtime between 1+1 and 1x2 using Ruby and
Attempting to build .exe using py2exe: python setup.py py2exe , receiving error: ImportError: No
Attempting the following exponential integral: Integrate[ Exp[-2 A Sqrt[x^2 + a^2] + I (
I have a java application on Websphere that is using Apache Commons FTPClient to
When attempting a file upload to an Alfresco Server using ssl the server logs
Attempting to insert an escape character into a table results in a warning. For
Attempting to print out a list of values from 2 different variables that are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.