How to calculate integral in Matlab

Posted: November 15, 2013 in Matlab

Do you want to calculate integral of a function right now ? Ok, let see

In Matlab, “integral”, “integral2” and “integral3” refer to “adaptive integral”, “double integral”, “triple integral”, respectively

1. Adaptive integral with “integral”

1.1. Syntax

q = integral(f,xmin,xmax)

approximates the integral of funtion f from xmin to xmax

q = integral(f,xmin,xmax,Name,Value)

likes the command above but in this case it specifies additional options with one or more Name, Value pair arguments

1.2. Example

Example1

Example 1_1

Solution

>> f = @(x) exp(-x.^2).*(log(x)).^2;

>> integral(f,0,Inf)

ans =

1.9475

Example 2

Example 2_1

Calculate the integral of f(x) from x = 0 to x = 1

Solution

In this case, f(x) work as an array of functions, so the result from integral will be an array of values

>> f=@(x)sin((1:5)*x)

f =

@(x)sin((1:5)*x)

>> integral(f,0,1,'ArrayValued',true)

ans =

0.4597    0.7081    0.6633    0.4134    0.1433

In this case, we use a pair of argument: “ArrayValued” and “true” to indicate that the result will be an array

For more detail, please see [1]

2. Double integral with “integral2”

2.1. Syntax

q = integral2(f,xmin,xmax,ymin,ymax)

approximates the integral of the function  over the region [xmin, xmax] and [ymin, ymax]

q = integral2(f,xmin,xmax,ymin,ymax,Name,Value)

specifies additional options with one or more pairs of arguments (Name,Value)

2.2. Example

Example 1

Example 1_2

Solution

>> f = @(x,y) 1./(sqrt(x+y).*(1+x+y));

>> ymax = @(x) 1-x;

>> q = integral2(f,0,1,0,ymax)

q =

0.4292

For more detail, please see [2]

3. Triple integral with “integral3”

3.1. Syntax

q = integral3(f,xmin,xmax,ymin,ymax,zmin,zmax)

approximates the integral of the function  over the region [xmin,xmax], [ymin,ymax] and [zmin,zmax]

q = integral3(f,xmin,xmax,ymin,ymax,zmin,zmax,Name,Value)

specifies some additional options in the pair of arguments (Name,Value)

3.2. Example

Example 1

Example 1_3

Solution

>> f = @(x,y,z)y.*sin(x) + z.*cos(x)

>> q = integral3(f,0,pi,0,1,-1,1)

q =

2.0000

Example 2

Example 2_3

Solution

>> f = @(x,y,z) x.*cos(y)+x.^2.*cos(z);

>> zmax = @(x,y) sqrt(1-x.^2-y.^2);

>> zmin = @(x,y) -sqrt(1-x.^2-y.^2);

>> ymax = @(x) sqrt(1-x.^2);

>> ymin = @(x) -sqrt(1-x.^2);

>> q = integral3(f,-1,1,ymin,ymax,zmin,zmax)

q =

0.7796

For more detail, please see [2]

4. References:

[1] http://www.mathworks.com/help/matlab/ref/integral.html

[2] http://www.mathworks.com/help/matlab/ref/integral2.html

[3] http://www.mathworks.com/help/matlab/ref/integral3.html

Here is the pdf version for this article

Seoul, Nov 15,2013

                                                                                                                           ledlong

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s