
- MATLAB - Home
- MATLAB - Overview
- MATLAB - Features
- MATLAB - Environment Setup
- MATLAB - Editors
- MATLAB - Online
- MATLAB - Workspace
- MATLAB - Syntax
- MATLAB - Variables
- MATLAB - Commands
- MATLAB - Data Types
- MATLAB - Operators
- MATLAB - Dates and Time
- MATLAB - Numbers
- MATLAB - Random Numbers
- MATLAB - Strings and Characters
- MATLAB - Text Formatting
- MATLAB - Timetables
- MATLAB - M-Files
- MATLAB - Colon Notation
- MATLAB - Data Import
- MATLAB - Data Output
- MATLAB - Normalize Data
- MATLAB - Predefined Variables
- MATLAB - Decision Making
- MATLAB - Decisions
- MATLAB - If End Statement
- MATLAB - If Else Statement
- MATLAB - If…Elseif Else Statement
- MATLAB - Nest If Statememt
- MATLAB - Switch Statement
- MATLAB - Nested Switch
- MATLAB - Loops
- MATLAB - Loops
- MATLAB - For Loop
- MATLAB - While Loop
- MATLAB - Nested Loops
- MATLAB - Break Statement
- MATLAB - Continue Statement
- MATLAB - End Statement
- MATLAB - Arrays
- MATLAB - Arrays
- MATLAB - Vectors
- MATLAB - Transpose Operator
- MATLAB - Array Indexing
- MATLAB - Multi-Dimensional Array
- MATLAB - Compatible Arrays
- MATLAB - Categorical Arrays
- MATLAB - Cell Arrays
- MATLAB - Matrix
- MATLAB - Sparse Matrix
- MATLAB - Tables
- MATLAB - Structures
- MATLAB - Array Multiplication
- MATLAB - Array Division
- MATLAB - Array Functions
- MATLAB - Functions
- MATLAB - Functions
- MATLAB - Function Arguments
- MATLAB - Anonymous Functions
- MATLAB - Nested Functions
- MATLAB - Return Statement
- MATLAB - Void Function
- MATLAB - Local Functions
- MATLAB - Global Variables
- MATLAB - Function Handles
- MATLAB - Filter Function
- MATLAB - Factorial
- MATLAB - Private Functions
- MATLAB - Sub-functions
- MATLAB - Recursive Functions
- MATLAB - Function Precedence Order
- MATLAB - Map Function
- MATLAB - Mean Function
- MATLAB - End Function
- MATLAB - Error Handling
- MATLAB - Error Handling
- MATLAB - Try...Catch statement
- MATLAB - Debugging
- MATLAB - Plotting
- MATLAB - Plotting
- MATLAB - Plot Arrays
- MATLAB - Plot Vectors
- MATLAB - Bar Graph
- MATLAB - Histograms
- MATLAB - Graphics
- MATLAB - 2D Line Plot
- MATLAB - 3D Plots
- MATLAB - Formatting a Plot
- MATLAB - Logarithmic Axes Plots
- MATLAB - Plotting Error Bars
- MATLAB - Plot a 3D Contour
- MATLAB - Polar Plots
- MATLAB - Scatter Plots
- MATLAB - Plot Expression or Function
- MATLAB - Draw Rectangle
- MATLAB - Plot Spectrogram
- MATLAB - Plot Mesh Surface
- MATLAB - Plot Sine Wave
- MATLAB - Interpolation
- MATLAB - Interpolation
- MATLAB - Linear Interpolation
- MATLAB - 2D Array Interpolation
- MATLAB - 3D Array Interpolation
- MATLAB - Polynomials
- MATLAB - Polynomials
- MATLAB - Polynomial Addition
- MATLAB - Polynomial Multiplication
- MATLAB - Polynomial Division
- MATLAB - Derivatives of Polynomials
- MATLAB - Transformation
- MATLAB - Transforms
- MATLAB - Laplace Transform
- MATLAB - Laplacian Filter
- MATLAB - Laplacian of Gaussian Filter
- MATLAB - Inverse Fourier transform
- MATLAB - Fourier Transform
- MATLAB - Fast Fourier Transform
- MATLAB - 2-D Inverse Cosine Transform
- MATLAB - Add Legend to Axes
- MATLAB - Object Oriented
- MATLAB - Object Oriented Programming
- MATLAB - Classes and Object
- MATLAB - Functions Overloading
- MATLAB - Operator Overloading
- MATLAB - User-Defined Classes
- MATLAB - Copy Objects
- MATLAB - Algebra
- MATLAB - Linear Algebra
- MATLAB - Gauss Elimination
- MATLAB - Gauss-Jordan Elimination
- MATLAB - Reduced Row Echelon Form
- MATLAB - Eigenvalues and Eigenvectors
- MATLAB - Integration
- MATLAB - Integration
- MATLAB - Double Integral
- MATLAB - Trapezoidal Rule
- MATLAB - Simpson's Rule
- MATLAB - Miscellenous
- MATLAB - Calculus
- MATLAB - Differential
- MATLAB - Inverse of Matrix
- MATLAB - GNU Octave
- MATLAB - Simulink
MATLAB - Double Integral
An integral in mathematics is a concept that represents the area under a curve. It is a fundamental concept in calculus and can be thought of as the accumulation of quantities. Integrals are used to find areas, volumes, central points, and many useful things.
There are two main types of integrals
Definite Integrals − Calculate the area under a curve between two specific points.
Indefinite Integrals − Represent a family of functions and include a constant of integration.
Definite Integral Example
If you have a function f(x), the definite integral from a to b is written as −
$$\mathrm{\displaystyle\int_{b}^{a} f(x) \: dx}$$
This gives the area under the curve of f(x) from x = a to x = b.
What is a Double Integral?
A double integral extends the concept of a single integral to functions of two variables. It is used to calculate the volume under a surface in three-dimensional space. The double integral of a function f(x,y) over a region R in the xy-plane is written as.
$$\mathrm{\iint_{R} \: f(x,y) \: dA}$$
where dA represents a tiny area element in the region R.
Double Integral Example:
If you have a function f(x,y) and you want to find the volume under the surface over a rectangular region R = [a,b] x [c,d], the double integral is.
$$\mathrm{\iint_{R} \: f(x,y) \: dA \: = \: \int_{a}^{b}\int_{c}^{d}f(x,y)dy \: dx}$$
MATLAB and Double Integrals
MATLAB is a powerful tool for numerical computations, including evaluating integrals and double integrals.
Single Integral in MATLAB
To compute a single integral in MATLAB, you use the integral function. For example, to find the integral of f(x) = x2 from 0 to 1 −
f = @(x) x.^2; result = integral(f, 0, 1); disp(result);
Double Integral in MATLAB
To compute a double integral in MATLAB, you use the integral2 function. Here's how you can calculate the double integral of f(x,y) = x2 + y2 over the region.
$$\mathrm{0 \: \leq \: x \: \leq \: 1 \: and \: 0 \: \leq \: y \: \leq \: 1}$$
f = @(x, y) x.^2 + y.^2; result = integral2(f, 0, 1, 0, 1); disp(result);
In this example
f is an anonymous function representing f(x,y) = x2 + y2.
integral2 computes the double integral over the specified limits for x and y.
Let us now understand integral2() method in more detail.
Matlab Double Integral using integral2() function
integral2() function numerically evaluate double integral.
Syntax
q = integral2(fun,xmin,xmax,ymin,ymax) q = integral2(fun,xmin,xmax,ymin,ymax,Name,Value)
Syntax Explanation
q = integral2(fun,xmin,xmax,ymin,ymax): in this syntax
- fun is the function you want to integrate.It is written in terms of x and y.
- xmin and xmax are the limits for the x-variable.The function will be integrated from x=xmin to x=xmax.
- ymin and ymax these are the limits for the y-variable.The integration will be done from y=ymin to y=ymax.These are constants or functions of x.
q = integral2(fun,xmin,xmax,ymin,ymax,Name,Value): in this syntax
- fun − The function to integrate.
- xmin and xmax − The limits for the x-variable.
- ymin and ymax − The limits for the y-variable.
- "Name,Value" pairs allow you to customize how MATLAB performs the integration.
Common "Name,Value" Pairs
- 'RelTol' − Sets the relative tolerance for accuracy.
- 'AbsTol' − Sets the absolute tolerance for accuracy.
- 'Method' − Specifies the integration method (e.g., 'auto', 'iterated', 'tiled').
- 'Waypoints' − Specifies intermediate points that the integrator should include.
Using integral2() function to solve double integrals
Example 1
using integral2() to Integrate Triangular Region with Singularity at the Boundary.
We want to compute the integral of a function f(x,y) over a triangular region in the xy-plane. A singularity at the boundary means that the function might become infinite or undefined at the boundary of the region. This can make the integral more challenging to compute, but MATLAB's integral2 can handle such cases with proper limits and function definitions.
Defining the Triangular Region
Consider a triangular region with vertices at points (0,0),(1,0), and (0,1). The region can be described with the limits −
0 x 1 0 y 1x
Let's consider the function
$$\mathrm{f(x, y) \: = \: \frac{1}{\sqrt{x^{2} \: + \: y^{2}}}}$$
This function has a singularity at the origin (0,0), where it becomes infinite.
% Define the function with a singularity at the boundary fun = @(x, y) 1 ./ sqrt(x.^2 + y.^2); % Define the limits for x xmin = 0; xmax = 1; % Define the limits for y as functions of x ymin = 0; ymax = @(x) 1 - x; % Compute the double integral over the triangular region q = integral2(fun, xmin, xmax, ymin, ymax); % Display the result disp(q);
Explanation of Code
We define the function shown below using an anonymous function handle in MATLAB.
$$\mathrm{f(x, y) \: = \: \frac{1}{\sqrt{x^{2} \: + \: y^{2}}}}$$
Set Integration Limits:For x, the limits are from 0 to 1.
For y, the lower limit is 0 and the upper limit is 1x. Note that ymin is set to 0 (a constant), while ymax is a function of x.
Compute the Integral: We use the integral2 function to compute the double integral. By ensuring ymin is a constant and ymax is a function of x, we meet the requirements of the integral2 function.
The result of the integral is displayed using disp(q).
When you execute the code in matlab the output we get is -
% Define the function with a singularity at the boundary fun = @(x, y) 1 ./ sqrt(x.^2 + y.^2); % Define the limits for x xmin = 0; xmax = 1; % Define the limits for y as functions of x ymin = 0; ymax = @(x) 1 - x; % Compute the double integral over the triangular region q = integral2(fun, xmin, xmax, ymin, ymax); % Display the result disp(q); 1.2465
Example 2
Using integral2() to evaluate the Double Integral of a Parameterized Function with Specific Method and Error Tolerance.
To compute the double integral of a parameterized function with additional options such as the method of integration and error tolerance, you can use the integral2 function with "Name,Value" pair arguments.
Example Function and Region
Let's consider the function over the triangular region with vertices at points (0,0), (1,0) and (0,1).
$$\mathrm{f(x, y) \: = \: \frac{1}{\sqrt{x^{2} \: + \: y^{2}}}}$$
Here's how to set up and compute this integral using MATLAB with additional options for the method and error tolerance:
% Define the function with a singularity at the boundary fun = @(x, y) 1 ./ sqrt(x.^2 + y.^2); % Define the limits for x xmin = 0; xmax = 1; % Define the limits for y as functions of x ymin = 0; ymax = @(x) 1 - x; % Define additional options: Relative Tolerance, Absolute Tolerance, and Method options = {'RelTol', 1e-6, 'AbsTol', 1e-8, 'Method', 'auto'}; % Compute the double integral over the triangular region with additional options q = integral2(fun, xmin, xmax, ymin, ymax, options{:}); % Display the result disp(q);
Explanation of Code
The function is defined using an anonymous function handle.
$$\mathrm{f(x, y) \: = \: \frac{1}{\sqrt{x^{2} \: + \: y^{2}}}}$$
Set Integration Limits
For x, the limits are from 0 to 1.
For y, the lower limit is 0 (a constant), and the upper limit is 1x (a function of x).
The additional options we defined are −
'RelTol', 1e-6 specifies a relative tolerance of 10-6
'AbsTol', 1e-8 specifies an absolute tolerance of 10-8
'Method', 'auto' lets MATLAB choose the most appropriate method automatically. Other methods like 'iterated' or 'tiled' can also be specified if needed.
The integral2 function is called with the function, integration limits, and additional options specified in the options cell array. The options{:} syntax unpacks the cell array into a comma-separated list of arguments.
The result of the integral is displayed using disp(q).
When you execute the code in matlab command window the output we get is as follows :
% Define the function with a singularity at the boundary fun = @(x, y) 1 ./ sqrt(x.^2 + y.^2); % Define the limits for x xmin = 0; xmax = 1; % Define the limits for y as functions of x ymin = 0; ymax = @(x) 1 - x; % Define additional options: Relative Tolerance, Absolute Tolerance, and Method options = {'RelTol', 1e-6, 'AbsTol', 1e-8, 'Method', 'auto'}; % Compute the double integral over the triangular region with additional options q = integral2(fun, xmin, xmax, ymin, ymax, options{:}); % Display the result disp(q); 1.2465