The Wayback Machine - https://web.archive.org/web/20241122073402/https://www.geeksforgeeks.org/program-to-calculate-double-integration/
Open In App

Program to calculate Double Integration

Last Updated : 18 Nov, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Write a program to calculate double integral numerically.

Example:

Input: Given the following integral.
\int _{3.7}^{4.3}\int _{2.3}^{2.5}\sqrt{x^4+y^5}\:dxdywheref(x, y)=\sqrt{x^4+y^5}\Output: 3.915905

Explanation and Approach:

  1. We need to decide what method we are going to use to solve the integral. In this example, we are going to use Simpson 1/3 method for both x and y integration. To do so, first, we need to decide the step size. Let h be the step size for integration with respect to x and k be the step size for integration with respect to y. We are taking h=0.1 and k=0.15 in this example. Refer for Simpson 1/3 rule
  2. We need to create a table which consists of the value of function f(x, y) for all possible combination of all x and y points.
x\yy0y1y2….ym
x0f(x0, y0)f(x0, y1)f(x0, y2)….f(x0, ym)
x1f(x1, y0)f(x1, y1)f(x1, y2)….f(x1, ym)
x2f(x2, y0)f(x2, y1)f(x2, y2)….f(x2, ym)
x3f(x3, y0)f(x3, y1)f(x3, y2)….f(x3, ym)
….….….….….….
….….….….….….
xnf(xn, y0)f(xn, y1)f(xn, y2)….f(xn, ym)
  1. In the given problem, 
x0=2.3
x2=2.4
x3=3.5

y0=3.7
y1=3.85
y2=4
y3=4.15
y4=4.3
  1. After generating the table, we apply Simpson 1/3 rule (or whatever rule is asked in the problem) on each row of the table to find integral wrt y at each x and store the values in an array ax[].
  2. We again apply Simpson 1/3 rule(or whatever rule asked) on the values of array ax[] to calculate the integral wrt x.

Below is the implementation of the above code: 

C++

// C++ program to calculate
// double integral value
 
#include <bits/stdc++.h>
using namespace std;
 
// Change the function according to your need
float givenFunction(float x, float y)
{
    return pow(pow(x, 4) + pow(y, 5), 0.5);
}
 
// Function to find the double integral value
float doubleIntegral(float h, float k,
                     float lx, float ux,
                     float ly, float uy)
{
    int nx, ny;
 
    // z stores the table
    // ax[] stores the integral wrt y
    // for all x points considered
    float z[50][50], ax[50], answer;
 
    // Calculating the number of points
    // in x and y integral
    nx = (ux - lx) / h + 1;
    ny = (uy - ly) / k + 1;
 
    // Calculating the values of the table
    for (int i = 0; i < nx; ++i) {
        for (int j = 0; j < ny; ++j) {
            z[i][j] = givenFunction(lx + i * h,
                                    ly + j * k);
        }
    }
 
    // Calculating the integral value
    // wrt y at each point for x
    for (int i = 0; i < nx; ++i) {
        ax[i] = 0;
        for (int j = 0; j < ny; ++j) {
            if (j == 0 || j == ny - 1)
                ax[i] += z[i][j];
            else if (j % 2 == 0)
                ax[i] += 2 * z[i][j];
            else
                ax[i] += 4 * z[i][j];
        }
        ax[i] *= (k / 3);
    }
 
    answer = 0;
 
    // Calculating the final integral value
    // using the integral obtained in the above step
    for (int i = 0; i < nx; ++i) {
        if (i == 0 || i == nx - 1)
            answer += ax[i];
        else if (i % 2 == 0)
            answer += 2 * ax[i];
        else
            answer += 4 * ax[i];
    }
    answer *= (h / 3);
 
    return answer;
}
 
// Driver Code
int main()
{
    // lx and ux are upper and lower limit of x integral
    // ly and uy are upper and lower limit of y integral
    // h is the step size for integration wrt x
    // k is the step size for integration wrt y
    float h, k, lx, ux, ly, uy;
 
    lx = 2.3, ux = 2.5, ly = 3.7,
    uy = 4.3, h = 0.1, k = 0.15;
 
    printf("%f", doubleIntegral(h, k, lx, ux, ly, uy));
    return 0;
}

                    

Java

// Java program to calculate
// double integral value
class GFG{
 
 
// Change the function according to your need
static float givenFunction(float x, float y)
{
    return (float) Math.pow(Math.pow(x, 4) +
                        Math.pow(y, 5), 0.5);
}
 
// Function to find the double integral value
static float doubleIntegral(float h, float k,
                    float lx, float ux,
                    float ly, float uy)
{
    int nx, ny;
 
    // z stores the table
    // ax[] stores the integral wrt y
    // for all x points considered
    float z[][] = new float[50][50], ax[] = new float[50], answer;
 
    // Calculating the number of points
    // in x and y integral
    nx = (int) ((ux - lx) / h + 1);
    ny = (int) ((uy - ly) / k + 1);
 
    // Calculating the values of the table
    for (int i = 0; i < nx; ++i)
    {
        for (int j = 0; j < ny; ++j)
        {
            z[i][j] = givenFunction(lx + i * h,
                                    ly + j * k);
        }
    }
 
    // Calculating the integral value
    // wrt y at each point for x
    for (int i = 0; i < nx; ++i)
    {
        ax[i] = 0;
        for (int j = 0; j < ny; ++j)
        {
            if (j == 0 || j == ny - 1)
                ax[i] += z[i][j];
            else if (j % 2 == 0)
                ax[i] += 2 * z[i][j];
            else
                ax[i] += 4 * z[i][j];
        }
        ax[i] *= (k / 3);
    }
 
    answer = 0;
 
    // Calculating the final integral value
    // using the integral obtained in the above step
    for (int i = 0; i < nx; ++i)
    {
        if (i == 0 || i == nx - 1)
            answer += ax[i];
        else if (i % 2 == 0)
            answer += 2 * ax[i];
        else
            answer += 4 * ax[i];
    }
    answer *= (h / 3);
 
    return answer;
}
 
// Driver Code
public static void main(String[] args)
{
    // lx and ux are upper and lower limit of x integral
    // ly and uy are upper and lower limit of y integral
    // h is the step size for integration wrt x
    // k is the step size for integration wrt y
    float h, k, lx, ux, ly, uy;
 
    lx = (float) 2.3; ux = (float) 2.5; ly = (float) 3.7;
    uy = (float) 4.3; h = (float) 0.1; k = (float) 0.15;
 
    System.out.printf("%f", doubleIntegral(h, k, lx, ux, ly, uy));
}
}
 
/* This code contributed by PrinciRaj1992 */

                    

Python3

# Python3 program to calculate
# double integral value
 
# Change the function according
# to your need
def givenFunction(x, y):
 
    return pow(pow(x, 4) + pow(y, 5), 0.5)
 
# Function to find the double integral value
def doubleIntegral(h, k, lx, ux, ly, uy):
 
    # z stores the table
    # ax[] stores the integral wrt y
    # for all x points considered
    z = [[None for i in range(50)]
               for j in range(50)]
    ax = [None] * 50
 
    # Calculating the number of points
    # in x and y integral
    nx = round((ux - lx) / h + 1)
    ny = round((uy - ly) / k + 1)
 
    # Calculating the values of the table
    for i in range(0, nx):
        for j in range(0, ny):
            z[i][j] = givenFunction(lx + i * h,
                                    ly + j * k)
         
    # Calculating the integral value
    # wrt y at each point for x
    for i in range(0, nx):
        ax[i] = 0
        for j in range(0, ny):
             
            if j == 0 or j == ny - 1:
                ax[i] += z[i][j]
            elif j % 2 == 0:
                ax[i] += 2 * z[i][j]
            else:
                ax[i] += 4 * z[i][j]
         
        ax[i] *= (k / 3)
     
    answer = 0
 
    # Calculating the final integral
    # value using the integral
    # obtained in the above step
    for i in range(0, nx):
        if i == 0 or i == nx - 1:
            answer += ax[i]
        elif i % 2 == 0:
            answer += 2 * ax[i]
        else:
            answer += 4 * ax[i]
     
    answer *= (h / 3)
 
    return answer
 
# Driver Code
if __name__ == "__main__":
 
    # lx and ux are upper and lower limit of x integral
    # ly and uy are upper and lower limit of y integral
    # h is the step size for integration wrt x
    # k is the step size for integration wrt y
    lx, ux, ly = 2.3, 2.5, 3.7
    uy, h, k = 4.3, 0.1, 0.15
 
    print(round(doubleIntegral(h, k, lx, ux, ly, uy), 6))
     
# This code is contributed
# by Rituraj Jain

                    

C#

// C# program to calculate
// double integral value
using System;
 
class GFG
{
 
// Change the function according to your need
static float givenFunction(float x, float y)
{
        return (float) Math.Pow(Math.Pow(x, 4) +
                            Math.Pow(y, 5), 0.5);
    }
     
    // Function to find the double integral value
    static float doubleIntegral(float h, float k,
                        float lx, float ux,
                        float ly, float uy)
    {
        int nx, ny;
     
        // z stores the table
        // ax[] stores the integral wrt y
        // for all x points considered
        float [, ] z = new float[50, 50];
        float [] ax = new float[50];
        float answer;
     
        // Calculating the number of points
        // in x and y integral
        nx = (int) ((ux - lx) / h + 1);
        ny = (int) ((uy - ly) / k + 1);
     
        // Calculating the values of the table
        for (int i = 0; i < nx; ++i)
        {
            for (int j = 0; j < ny; ++j)
            {
                z[i, j] = givenFunction(lx + i * h,
                                        ly + j * k);
            }
        }
     
        // Calculating the integral value
        // wrt y at each point for x
        for (int i = 0; i < nx; ++i)
        {
            ax[i] = 0;
            for (int j = 0; j < ny; ++j)
            {
                if (j == 0 || j == ny - 1)
                    ax[i] += z[i, j];
                else if (j % 2 == 0)
                    ax[i] += 2 * z[i, j];
                else
                    ax[i] += 4 * z[i, j];
            }
            ax[i] *= (k / 3);
        }
     
        answer = 0;
     
        // Calculating the final integral value
        // using the integral obtained in the above step
        for (int i = 0; i < nx; ++i)
        {
            if (i == 0 || i == nx - 1)
                answer += ax[i];
            else if (i % 2 == 0)
                answer += 2 * ax[i];
            else
                answer += 4 * ax[i];
        }
        answer *= (h / 3);
     
        return answer;
    }
     
    // Driver Code
    public static void Main()
    {
        // lx and ux are upper and lower limit of x integral
        // ly and uy are upper and lower limit of y integral
        // h is the step size for integration wrt x
        // k is the step size for integration wrt y
        float h, k, lx, ux, ly, uy;
     
        lx = (float) 2.3; ux = (float) 2.5; ly = (float) 3.7;
        uy = (float) 4.3; h = (float) 0.1; k = (float) 0.15;
     
        Console.WriteLine(doubleIntegral(h, k, lx, ux, ly, uy));
    }
}
 
// This code contributed by ihritik

                    

Javascript

// JavaScript program to calculate
// double integral value
 
// Change the function according to your need
function givenFunction(x, y){
    return Math.pow(Math.pow(x, 4) + Math.pow(y, 5), 0.5);
}
 
// Function to find the double integral value
function doubleIntegral(h, k, lx, ux, ly, uy){
     
    // z stores the table
    // ax[] stores the integral wrt y
    // for all x points considered
    var z = new Array(50);
    for(var i = 0; i < z.length; i++){
        z[i] = new Array(50);
    }
    var ax = new Array(50);
    let answer;
     
    // Calculating the number of points
    // in x and y integral
    let nx = Math.round((ux - lx) / h + 1);
    let ny = Math.round((uy - ly) / k + 1);
     
    // Calculating the values of the table
    for(let i = 0; i < nx; i++){
        for(let j = 0; j < ny; ++j){
            z[i][j] = givenFunction(lx + i * h, ly + j * k);
        }
    }
     
    // Calculating the integral value
    // wrt y at each point for x
    for (let i = 0; i < nx; ++i)
    {
        ax[i] = 0;
        for (let j = 0; j < ny; ++j)
        {
            if (j == 0 || j == ny - 1)
                ax[i] += z[i][j];
            else if (j % 2 == 0)
                ax[i] += 2 * z[i][j];
            else
                ax[i] += 4 * z[i][j];
        }
        ax[i] *= (k / 3);
    }
     
    answer = 0;
 
    // Calculating the final integral value
    // using the integral obtained in the above step
    for (let i = 0; i < nx; ++i)
    {
        if (i == 0 || i == nx - 1)
            answer += ax[i];
        else if (i % 2 == 0)
            answer += 2 * ax[i];
        else
            answer += 4 * ax[i];
    }
    answer *= (h / 3);
 
    return answer;
}
 
// lx and ux are upper and lower limit of x integral
// ly and uy are upper and lower limit of y integral
// h is the step size for integration wrt x
// k is the step size for integration wrt y
let lx = 2.3, ux = 2.5, ly = 3.7;
let uy = 4.3, h = 0.1, k = 0.15;
 
console.log(doubleIntegral(h, k, lx, ux, ly, uy).toFixed(6));
 
// This code is contributed by lokeshmvs21.

                    
Output:
3.915905


Previous Article
Next Article

Similar Reads

C program to print a string without any quote (single or double) in the program
Print a string without using quotes anywhere in the program using C or C++. Note : should not read input from the console. The idea is to use macro processor in C (Refer point 6 of this article). A token passed to macro can be converted to a string literal by using # before it. C/C++ Code // C program to print a string without // quote in the progr
1 min read
Program to find the indefinite Integration of the given Polynomial
Given a polynomial string str, the task is to integrate the given string and print the string after integrating it. Note: The input format is such that there is a whitespace between a term and the ‘+’ symbol. Examples: Input: str = "4X3 + 3X1 + 2X2" Output: X4 + (3/2)X2 + (2/3)X3 + C Input: str = "5X3 + 7X1 + 2X2 + 1X0" Output: (5/4)X4 + (7/2)X2 +
8 min read
Program to print double headed arrow pattern
Given an integer N which is the number of rows, the task is to draw the number pattern in the shape of a double headed arrow.Prerequisite: The pattern is a grow and shrink type pattern and hence basic knowledge to execute loops is required to understand the topic and the code in any language. The geometric shape can be visualized as- Examples: Inpu
9 min read
C++ Program to Find the Size of int, float, double and char
In this article, we will learn to write a C++ program to find the size of int, float, double, and char. It is important to know the size of different data types especially when working with large datasets to optimize memory usage. The size of a variable can be determined using sizeof() operator in C++. The syntax of size of operator is: sizeof(data
2 min read
C++ Program For Double to String Conversion
Here, we will build a C++ program for double to string conversion using various methods i.e. Using to_stringUsing stringstreamUsing sprintfUsing lexical_cast We will keep the same input in all the mentioned approaches and get an output accordingly. Input: n = 456321.7651234 Output: string: 456321.7651234 1. Using to_string In C++, use std::to strin
2 min read
C++ Program For String to Double Conversion
There are situations, where we need to convert textual data into numerical values for various calculations. In this article, we will learn how to convert strings to double in C++. Methods to Convert String to Double We can convert String to Double in C++ using the following methods: Using stod() FunctionUsing stold() FunctionUsing atof() Function L
3 min read
C Program For Double to String Conversion
To convert double to string in C language, we will use the sprintf function as follows: Input: n = 456321.7651234 Output: string: 456321.7651234 Method: Using sprintf By specifying the precision in sprintf, we can convert double to string or character array with custom precision. We can use sprintf to add extra text (as required) to the string at t
1 min read
Program to print double sided Stair-Case Pattern
Creating a double-sided staircase pattern is a fun and educational exercise that helps practice programming skills, especially loops and conditional statements. In this article, we will walk through the process of writing a program to print a double-sided stair-case pattern Examples: Input: 10Output: Approach: To solve the problem follow the below
4 min read
C Program to Find the Size of int, float, double and char
Write a C program to find the size of the data types: int, float, double, and char in bytes and print it on the output screen. Examples Input: charOutput: Size of char: 1 byte Input: intOutput:Size of int: 4 bytes Different Methods to Find the Size of int, float, double and char in CWe can find the size of the int, float, double and char data types
4 min read
Integration in a Polynomial for a given value
Given string str which represents a polynomial and a number N, the task is to integrate this polynomial with respect to X at the given value N. Examples: Input: str = "90x4 + 24x3 + 18x2 + 18x", N = 1. Output: 39 Explanation: Given, dy/dx = 90*(X4) + 24* (X3) + 18* (X2) + 18*(X). On integrating this equation, we get 18*(X5) + 6*(X4) + 6*(X3) + 9*(X
7 min read
Power Rule of Integration
Power Rule of Integration is a fundamental law for finding the integrals of algebraic functions. It is used to find the integral of a variable raised to some constant power which may be positive, negative or zero except being -1. In this article, we will discuss the power rule of integration, its mathematical representation, its applications, defin
7 min read
Application of Integration
Integration is a fundamental concept in calculus that plays a crucial role in various scientific and engineering disciplines. It involves finding the integral of a function, which can represent areas, volumes, central points, and other physical and abstract concepts. The applications of integration are vast and diverse, reflecting its importance in
2 min read
Integration Formulas
Integration Formulas are the basic formulas that are used to solve various integral problems. They are used to find the integration of algebraic expressions, trigonometric ratios, inverse trigonometric functions, and logarithmic and exponential functions. These integration formulas are very useful for finding the integration of various functions.
12 min read
Integration of Trigonometric Functions
Integration is the process of summing up small values of a function in the region of limits. It is just the opposite to differentiation. Integration is also known as anti-derivative. We have explained the Integration of Trigonometric Functions in this article below. Below is an example of the Integration of a given function. e.g., Consider a functi
9 min read
Methods of Integration
Methods of Integration in Calculus refer to the various techniques that are used to integrate function easily. Many times it is not possible to directly integrate a function, so we need to use a specific technique to reduce the integral and then perform integration. Any method of integration involves identifying the type of integral and then decidi
10 min read
Limit of Integration
Limits of integration are the numbers that set the boundaries for calculating the definite integral of a function. The definite integral, ∫f(x)dx, involves finding the antiderivative F(x) and then evaluating it at the upper and lower limits, [a, b]. In this article, we will cover the basic concept of integration, formulas for limits of integration,
10 min read
Integration by Parts
Integration by Parts: Integration by parts is a technique used in calculus to find the integral of the product of two functions. It's essentially a reversal of the product rule for differentiation. Integrating a function is not always easy sometimes we have to integrate a function that is the multiple of two or more functions in this case if we hav
14 min read
Integration by Substitution Method
Integration by substitution is one of the important methods for finding the integration of the function where direct integration can not be easily found. This method is very useful in finding the integration of complex functions. We use integration by substitution to reduce the given function into the simplest form such that its integration is easi
9 min read
Integration by Partial Fractions
Integration by Partial Fractions is one of the methods of integration, which is used to find the integral of the rational functions. In Partial Fraction decomposition, an improper-looking rational function is decomposed into the sum of various proper rational functions. If f(x) and g(x) are polynomial functions such functions. that g(x) ≠ 0 then f(
9 min read
Integration by Substitution Practice Problems
While integration by substitution, commonly referred to as u-substitution is a common and vital method for solving integrals in calculus. It makes the integration easier because it can convert a complicated integration into a more manageable one. This method is important for students as it builds up their knowledge to progress to other integration
7 min read
Integration
Integration is an important part of calculus. It involves finding the anti-derivative of a function and is used to solve integrals. Integration has numerous applications in various fields, such as mathematics, physics, and engineering. This article serves as a comprehensive guide to integration, covering everything from integration formulas to meth
13 min read
Integration of 1/x dx with limits
To integrate 1/x​ from a to b, we can use the following formula: [Tex]\int_{a}^{b} \frac{1}{x} \, dx = \ln \left| \frac{b}{a} \right|[/Tex] Now, evaluate the natural logarithm of the absolute value of the ratio of the limits. Steps to Integrate 1/x with limitsTo integrate the function 1/x with respect to x, we can use the following integral formula
2 min read
Integration by U-substitution
Finding integrals is basically a reverse differentiation process. That is why integrals are also called anti-derivatives. Often the functions are straightforward and standard functions that can be integrated easily. It is easier to solve the combination of these functions using the properties of indefinite integrals. Sometimes functions are made up
8 min read
Integration by Parts
Integration is the calculation of an integral which is used in maths for finding many useful quantities such as areas, volumes, displacement, etc., that occur due to a collection of small data which cannot be measured singularly. Integrals are denoted using " ∫ ". The concept of integration is used to solve two types of problems given below : When
7 min read
Integration Formulas
Indefinite Integrals: The derivatives have been really useful in almost every aspect of life. They allow for finding the rate of change of a function. Sometimes there are situations where the derivative of a function is available, and the goal is to calculate the actual function whose derivative is given. In this article, we will discuss Indefinite
9 min read
Integration by Substitution Formula
The process of finding the anti-derivative of a function is the inverse process of differentiation i.e. finding integral is the inverse process of differentiation. Integration can be used to find the area or volume of a function with or without certain limits or boundaries  It is shown as ∫g(x)dx = G(x) + C  Explanation:It means integral of functio
6 min read
Integration of Gaussian Functions
Gaussian function is named after Carl Friedrich Gauss and is often referred to as a "bell curve" due to its characteristic shape. It's widely used in various fields such as probability theory, statistics, signal processing, and physics due to its properties. In this article, we will discuss how we will Integrate Gaussian functions. Table of Content
6 min read
Complex Integration
Complex integration is a simple extension of the ideas we develop in calculus to the complex world. In real calculus, differentiation and integration are, roughly speaking, inverse operations (save for the additional interpretation of derivative as the slope of a function and integral as the area under the curve). We will see that the same inverse
12 min read
Differentiation and Integration Formula
Differentiation and Integration are two mathematical operations used to find change in a function or a quantity with respect to another quantity instantaneously and over a period, respectively. Differentiation is an instantaneous rate of change and it breaks down the function for that instant with respect to a particular quantity while Integration
13 min read
What is dx in Integration?
In integration, dx signifies an infinitesimal change in x, representing integration with respect to x. In this article will explain what "dx" represents in integration and provide a practical example to help to understand its role. What is dx in Integration?The notation "dx" is used in the integral sign of calculus to denote the variable of integra
2 min read
three90RightbarBannerImg