Definite integrals in Octave

In this lesson we see how to calculate the definite integral of a mathematical function in Octave with some practical examples.

What do you need? You must have already installed the Symbolic module on Gnu Octave.

Go to the Octave command line.

Define the variable symbol you want to use using the command syms. For example the variable x.

syms x

Now calculate the definite integral of the function f(x)=x2 in the interval (1,3)

$$ \int_1^3 x^2 \ dx $$

Type the command int() indicating the function x2 as the first parameter.

In the Octave syntax, the exponentiation is written x**2.

Write as the second parameter the lower bound of integration (1) and as the third parameter the upper bound of integration (3). Then hit enter.


The result is the definite integral of x2 in the interval (1,3)

ans = (sym) 26/3

In this case the definite integral is equal to 26/3 that is about 8.6

Verifica. The integral of x2 in the range (1,3) is 26/3 because its primitive function is x3/3 $$ \int_1^3 x^2 \ dx = [\frac{x^3}{3}]_1^3 = \frac{3^3}{3} - \frac{1^3}{3} = \frac{3^3-3^1}{3} = \frac{27-1}{3} = \frac{26}{3} $$

An alternative method

There is also another way to calculate the definite integral of a function without using Symbolic.

Create the integral function in Octave as an anonymous function and calculate the integral defined by the function quad()

quad(function name, a,b)

For example, if the integral is

$$ \int_1^3 x^2 \ dx $$

The function of integral is f(x)=x2

Write the anonymous function in Octave

>> f = @(x) x**2

To calculate the definite integral in the range from 1 to 3 use the function quad()

>> quad(f,1,3)

The quad function calculates the area under the function graph f(x)=x2 in the interval (1,3).


The definite integral is equal to 8.6667.

Note. It is the same result obtained in the previous example. $$ \int_1^3 x^2 \ dx = \frac{26}{3} = 8.667 $$

Report a mistake or post a question