S Lazy-H
  • Home
  • About
  • Posts
  • Contact
  • Slide Rules
  • A Biker’s Tale

Double Integrals Part 2

mathematics
software
C++
Author

Sam Hutchins

Published

October 9, 2025

This is a follow-up to the post Double Integrals. This expands a bit on that post by adding some small changes to allow entry of x and y ranges, and enter different formula elements. The first post had as an example:

\[\sideset{}{_{-100}^{100}}\int{\sideset{}{_{-20}^{20}}\int{40 - 0.03 x^{2} + y}} \: dx \: dy\]

where the program had the \(a - b + c\) hardcoded. The changes to allow entry is straightforward and requires minimal modifications. We do have to add some more STLs:

Code
#include <sstream>
#include <vector>
#include <iomanip>

for the interactions. So, the new main function now looks like this:

Code
int main() {
    dblIntro(); // Eye candy
    double xMin{}, xMax{}, yMin{}, yMax{}, a{}, b{}, c{};
    string d{};
    int n = 1000; // Number of slices, determines accuracy
    cout << "xMin: "; cin >> xMin;
    cout << "xMax: "; cin >> xMax;
    cout << "yMin: "; cin >> yMin;
    cout << "yMax: "; cin >> yMax;
    cout << "If a,b,c not used, enter 0." << endl;
    cout << "a: "; cin >> a;
    cout << "b: "; cin >> b;
    cout << "c: "; cin >> c;

    double result = doubleIntegral(xMin, xMax, yMin, yMax, n, a, b, c); // Function
    if(c == 0) { d = "y"; } else { d = "y\u00b2"; }
    cout << "\nFormula: " << std::fixed << std::setprecision(3) << a << " + " << b << " x\u00b2 + " << c << d << endl;
    cout << "The double integral result is: " << std::fixed << std::setprecision(1) << result << std::endl; // Display result
    return 0; // Function type defines return value
}

The cout and cin statements get the data. Changes required to the f() and doubleIntegral() functions are to include passing the new parameters, a, b, c. Both functions need the additional parameters included, as the doubleIntegral() reuses the f() function. So:

Code
double f(double x, double y, double a, double b, double c) {
    //return 40 - 0.03 * x * x + y; // result  = 288,000
    //return 40 - 0.006 * x * x + 0.003 * y * y; //result = 900,000
    return a + b * x * x + c * y * y;
}

Note there is a difference in data entry between the original f() and this one is, for example: \(40 - .05\), which are the a and b parameters are entered as: 40, and -.05, because the last line formula is changed to a + b + c. And for doubleIntegral(), the same parameters are also included:

Code
double doubleIntegral(double xMin, double xMax, double yMin, double yMax, int n, double a, double b, double c) {
    double dx = (xMax - xMin) / n;
    double dy = (yMax - yMin) / n;
    double integral = 0.0;

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            double x = xMin + (i + 0.5) * dx; // use center of each slice
            double y = yMin + (j + 0.5) * dy; // use center of each slice
            integral += f(x, y, a, b, c) * dx * dy; // add each slice's area to total
        }
    }
    return integral; // Function type defines return value
}

The remainder of the program is mostly just tweaking for better looks and is mostly the same. One thing different from C++ and R Core Team (2025) is the ease of doing the same double integral function. Of course C++ allows more definition at the expense of more programming. So, for integrals in R, we would load the pracma package, then do something like \(fun <- function(x,y) { a + b * x^2 + c * y^2 }\), then use this function as: \(integral2(fun, xmin, xmax, ymin, ymax)\).

But as I am attempting to learn a bit about C++, that is what I am using, and it’s fun to decipher things to accomplish the task!

Anyway, have a great day and God Bless you and yours. I pray you are right with God and accepted His Son Jesus as your Lord and Savior, as time is getting short! Don’t wait too long! Remember it’s your soul on the line, and Satan wants to devour it!

References

R Core Team. 2025. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.
© S Lazy-H 2019 -