Matlab

How to Solve Integrals in MATLAB

Integration is a well-known mathematical operation used for finding the anti-derivatives of the function and has many applications in science and engineering. We can easily integrate simple functions, but it is very hard to integrate them manually when dealing with very complex ones. So, to integrate complex functions MATLAB provides the built-in int() function which solves the integrals of any complex functions in a short time interval.

In this guide, we will explore how to solve integrals in MATLAB.

How to Solve Integrals in MATLAB?

Generally, integration is used to solve the two types of integrals:

Now we will demonstrate how to solve the integrals of these two types.

How to Solve the Definite Integral of a Function in MATLAB?

Definite Integrals are utilized for integrating the function at the given points. We use definite integrals in the many applications of science and engineering.

Example 1

The given example uses the int() function to find the definite integral of the given function.

syms x

f = 3*x^7-5*x^4+9;

a = int(f, 10, 20)

In the above example, 10 and 20 are the lower and upper bounds of the given function.

Example 2

The given example uses the int() function to find the definite integral of the given function from –inf to inf.

syms a x

f = 1/(x^2 + a^2);

F = int(f, x, -inf, inf)

How to Solve the Indefinite Integral of a Function in MATLAB?

Indefinite integrals are utilized for finding the antiderivative of the function.

Example 1

The given example uses the int() function to find the indefinite integral of the polynomial function, trigonometric function, and power function respectively.

syms x n a t

int((x^n))

int(cos(n*t))

int(a*sin(pi*t))

int(a^x)

When you will run the above code, the results printed on the screen are given below.

Example 2

This MATLAB code includes some complex functions and finds their respective indefinite integral using the MATLAB int() function.

syms x n

int(exp(x))

int(log(x))

int(x^3*sin(3*x))

pretty(int(x^5*cos(5*x)))

int(x^-5)

int(tan(x)^2)

pretty(int(1 - 8*x^3 - 5 * x^5))

int((3*x + x^2 -8*x^3 - 9*x^4)/8*x^9)

In the above code, we used the pretty() function which returns the calculated result in a more readable format.

Conclusion

Integration is a well-known mathematical operation used for finding the anti-derivatives of the function and has many applications in science and engineering. To integrate complex functions MATLAB provides the built-in int() function which finds the integration of any complex functions quickly. There are two types of integrals to solve a problem: definite integrals and indefinite integrals. This guide illustrated how to solve definite and indefinite integrals with examples.

About the author

Komal Batool Batool

I am passionate to research technologies and new ideas and that has brought me here to write for the LinuxHint. My major focus is to write on programming languages and computer science related topics.