Matlab

How to Implement Numerical Integration in MATLAB?

Numerical integration is a mathematical operation used in science and engineering applications to solve problems, such as calculating the heat transferred in the system or the force acting on the objects. Its main purpose is to compute the area under the curve of the given function between boundary points. MATLAB facilitates us with a built-in integral() function that solves complex integrals numerically.

In this guide, we will learn how to implement numerical integration in MATLAB using some examples.

What is a Numerical Integration?

Numerical integration is a mathematical technique that helps you to compute the approximate value of a definite integral. It performs the process by dividing the integration’s interval into multiple subintervals, after that it approximates the integral as the sum of the values of the integrand at the boundary points of the subintervals. The accuracy of the approximation relies on the number of sub-intervals used since more subintervals will provide a more accurate approximation.

How to Implement Numerical Integration in MATLAB?

We can implement numerical integration in MATLAB using a built-in integral() function. This function allows us to numerically integrate a function on the specified boundary conditions. This function takes three mandatory inputs and provides a numeric value after calculating the numerical integration of the given function on the given boundary values.

Syntax

The integral() function’s syntax is given below:

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

 

Here:

The function q = integral(fun,xmin,xmax) yields to numerically integrate the given function fun from xmin to xmax using global adaptive quadrature as well as the preset error tolerances where xmin and xmax are real parameters.

The function q = integral(fun,xmin,xmax, Name, Value) yields to specify the Name, and Value pairs as additional arguments.

Examples

Consider some examples to practically implement the numerical integration in MATLAB.

Example 1: How to Implement Numerical Integration in MATLAB Using integral() Function?

In this example, we compute the numerical integration of the given function with respect to variable x on the given boundary values -1 and 1 using the integral() function.

fun = @(x) cos(x.^2).*exp(x);
q = integral(fun,-1, 1)</td>

 

Example 2: How to Compute Numerical Integration of the Vector-Valued Function in MATLAB Using integral() Function?

This MATLAB code calculates the numerical integration of the given vector-valued function with respect to variable x on the given boundary points -1 and 1 using the integral() function with additional Name and value parameters.

fun = @(x)exp((2:7)*x);
q = integral(fun,-1,1,'ArrayValued',true)

 

Conclusion

Numerical integration is a mathematical operation widely used in many applications of science and engineering. Its main purpose is to calculate the area under the curve. We can easily implement numerical integration in MATLAB using a built-in integral() function. This tutorial has explored the implementation of numerical integration with examples in MATLAB, allowing you to learn the basics of using the integral() function.

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.