English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

MATLAB Integration

Integration handles two essentially different problems.

  • In the first type, the derivative of the function is given, and we want to find the function. Therefore, we fundamentally reverse the process of differentiation. This reverse process is called anti-differentiation, or finding the original function, or findingindefinite integral.

  • The second type of problem involves adding a large number of very small quantities, and then taking a limit as the size of the quantities approaches zero, while the number of terms tends to infinity. This process leads to the definitiondefinite integral.

Definite integral is used to find area, volume, center of gravity, moment of inertia, work done by force, and many other applications.

Using MATLAB to find the indefinite integral

According to definition, if the derivative of the function f(x) is f'(x), then we say that the indefinite integral of f'(x) with respect to x is f(x). For example, since x 2The derivative (with respect to x) of2x, thus it can be said that2The indefinite integral of x is x 2.

in the symbol-

f'(x2) = 2xTherefore,

∫ 2xdx = x2.

The indefinite integral is not unique because for any value of the constant c, x 2 + The derivative of c will also be2x.

This is represented symbolically as-

∫ 2xdx = x2 + c.

where c is called the 'constant of integration'.

MATLAB providesintThe command used to calculate the integral of an expression. To derive the expression of the indefinite integral of a function, we write:

int(f);

For example, from our previous example-

syms x 
int(2*x)

MATLAB executes the above statements and returns the following results-

ans =
   x^2

Example1

In this example, let's find the integrals of some common expressions. Create a script file and enter the following code-

syms x n
int(sym(x^n))
f = 'sin(n*t)'
int(sym(f))
syms a t
int(a*cos(pi*t))
int(a^x)

When running the file, it displays the following result-

ans =
   piecewise([n == -1, log(x)], [n ~= -1, x^(n + 1)/(n + 1)])
f =
sin(n*t)
ans =
   -cos(n*t)/n
   ans =
   (a*sin(pi*t))/pi
   ans =
   a^x/log(a)

Example2

Create a script file and enter the following code-

syms x n
int(cos(x))
int(exp(x))
int(log(x))
int(x^-1)
int(x^5*cos(5*x))
pretty(int(x^5*cos(5*x)))
int(x^-5)
int(sec(x)^2)
pretty(int(1 - 10*x + 9 * x^2))
int((3 + 5*x -6*x^2 - 7*x^3)/2*x^2)
pretty(int((3 + 5*x -6*x^2 - 7*x^3)/2*x^2))

Please note thatprettyThe function returns the expression in a more readable format.

When running the file, it displays the following result-

ans =
   sin(x)
 
ans =
   exp(x)
 
ans =
   x*(log(x) - 1)
 
ans =
   log(x)
 
ans =
(24*cos(5*x))/3125 + (24*x*sin(5*x))/625 - (12*x^2*cos(5*x))/125 + (x^4*cos(5*x))/5 - (4*x^3*sin(5*x))/25 + (x^5*sin(5*x))/5
                                    2             4 
   24 cos(5 x)   24 x sin(5 x)   12 x  cos(5 x)   x  cos(5 x) 
   ----------- + ------------- - -------------- + ------------ 
      3125            625             125              5 
   
        3             5 
 
   4 x  sin(5 x)   x  sin(5 x) 
   ------------- + ----------- 
         25              5
 
ans =
-1/(4*x^4)
 
ans =
tan(x)
        2 
  x (3 x  - 5 x + 1)
 
ans = 
- (7*x^6)/12 - (3*x^5)/5 + (5*x^4)/8 + x^3/2
 
      6      5      4    3 
    7 x    3 x    5 x    x 
  - ---- - ---- + ---- + -- 
     12     5      8     2

Using MATLAB to find the definite integral

According to definition, the definite integral is essentially the limit of a sum. We use the definite integral to find areas, such as the area between a curve and the x-axis, and the area between two curves. It can also be used in other cases, where the required quantity can be represented as the limit of a sum.

intBy passing the limits of integration to be calculated, this function can be used to determine the integral.

Calculate

We write,

int(x, a, b)

For example, to calculate a value, we write:

int(x, 4, 9)

MATLAB executes the above statements and returns the following results-

ans =
   65/2

Here is the equivalent of the above calculation in Octave-

pkg load symbolic
symbols
x = sym("x");
f = x;
c = [1, 0];
integral = polyint(c);
a = polyval(integral, 9) - polyval(integral, 4);
display('Area:'), disp(double(a));

Octave executes the code and returns the following result-

Area: 
   32.500

You can use the quad() function provided by Octave to give an alternative solution, as shown below:

pkg load symbolic
symbols
f = inline("x");
[a, ierror, nfneval] = quad(f, 4, 9);
display('Area:'), disp(double(a));

Octave executes the code and returns the following result-

Area: 
   32.500

Example1

Let's calculate the area under the curve y = x 3 -2x + 5as well as the y-coordinate x = 1and x = 2The area enclosed between.

The required area is given by the following formula:

Create a script file and enter the following code-

f = x^3 - 2*x +5;
a = int(f, 1, 2)
display('Area:'), disp(double(a));

When running the file, it displays the following result-

a =
23/4
Area: 
   5.7500

Here is the equivalent of the above calculation in Octave-

pkg load symbolic
symbols
x = sym("x");
f = x^3 - 2*x +5;
c = [1, 0, -2, 5];
integral = polyint(c);
a = polyval(integral, 2) - polyval(integral, 1);
display('Area:'), disp(double(a));

Octave executes the code and returns the following result-

Area: 
   5.7500

You can use the quad() function provided by Octave to give an alternative solution, as shown below:

pkg load symbolic
symbols
x = sym("x");
f = inline("x^3 - 2*x +5);
[a, ierror, nfneval] = quad(f, 1, 2);
display('Area:'), disp(double(a));

Octave executes the code and returns the following result-

Area: 
   5.7500

Example2

Find the area under the curve: f(x) = x 2 cos(x) represents the negative4≤x≤9.

Create a script file and write the following code-

f = x^2*cos(x);
ezplot(f, [-4,9])
a = int(f, -4, 9)
disp('Area:'), disp(double(a));

When running the file, MATLAB draws the graph-

Output as follows-

a = 
8*cos(4) + 18*cos(9) + 14*sin(4) + 79*sin(9)
 
Area: 
   0.3326

Here is the equivalent of the above calculation in Octave-

pkg load symbolic
symbols
x = sym("x");
f = inline("x^2*cos(x)
ezplot(f, [-4,9])
print -deps graph.eps
[a, ierror, nfneval] = quad(f, -4, 9);
display('Area:'), disp(double(a));