Open In App

Double Integral in MATLAB

The double integral of a non-negative function f(x, y) defined on a region in the plane tells us about the volume of the region under the graph. The double integral of a function of two variables, f(x, y) over the region R can be expressed as follows :



MATLAB allows users to calculate the double integral of a function using the integral2() method. Different syntax of integral2() method are:

Syntax:

 integral2(fun,xmin,xmax,ymin,ymax)

This function approximates the integral of any function f = fun(x,y) in the region xmin ≤ x ≤ xmax and ymin(x) ≤ y ≤ ymax(x)



Example 1:

% MATLAB Code to 
% Create a function in x and y
% Function : y*sin(x) + x*cos(y)
f = @(x,y) y.*sin(x)+x.*cos(y);
disp("f(x,y) :");
disp(f);
  
% Double Integral of f(x)
% over pi≤x≤2*pi and 0≤y≤pi
d = integral2(f,pi,2*pi,0,pi);
disp("Double Integral of f(x) :");
disp(d);

                    

Output : 

Example 2:

% MATLAB Code to
% Create a function in x and y
% Function : y*sin(x) + x*cos(y)
f = @(x,y) y.*sin(x)+x.*cos(y);
disp("f(x,y) :");
disp(f);
  
% Double Integral of f(x)
% over pi≤x≤2*pi and 0≤y≤x
ymax = @(x) x;
d = integral2(f,pi,2*pi,0,ymax);
disp("Double Integral of f(x) :");
disp(d);

                    

Output : 

Syntax: 

 F = integral2(fun,xmin,xmax,ymin,ymax,Name,Value) 

It specifies additional options with one or more Name, Value pair arguments. The most popular Name used is ‘Method’

The Possible values are:

Example 3:

% MATLAB Code to
% Create a function in x and y
% Function : y*sin(x) + x*cos(y)
f = @(x,y) y.*sin(x)+x.*cos(y);
disp("f(x,y) :");
disp(f);
  
% Double Integral of f(x) using Tiled Method
% over pi≤x≤2*pi and 0≤y≤pi
d = integral2(f,pi,2*pi,0,pi,'Method','Tiled');
disp("Double Integral of f(x) :");
disp(d);

                    

Output :

Example 4:

% MATLAB code for iterated 
% Create a function in x and y
% Function : x*y*e^-(x+y)
f = @(x,y) y.*x.*exp(-(x+y))
disp("f(x,y) :");
disp(f);
  
% Double Integral of f(x) using Iterated method
% over 0≤x≤Inf and 0≤y≤Inf
d = integral2(f,0,Inf,0,Inf,'Method','Iterated');
disp("Double Integral of f(x) :");
disp(d);

                    

Output:


Article Tags :