Open In App

Definite Numerical Integration Using Quad in MATLAB

Last Updated : 28 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Pre-requisites: Definite Integration

MATLAB is a high-performance language that is used for matrix manipulation, performing technical computations, graph plottings, etc. It stands for Matrix Laboratory. With the help of this function, we can compute definite integration. We use quad function which takes the function and its limits as its arguments. 

Syntax:

ans = quad(‘f(x)’ , a, b);

where ans is the variable in which we are storing the integral’s value;

f(x) is the function which is to be integrated;

a and b are the lower and upper limits of the integral.

Suppose that, f(x) = ∫x^2 dx with lower limit = 2 and upper limit = 3. then solution is:

We know that, ∫x^n dx = (x^(n+1))/(n+1)
So ∫x^2 dx = (x^3)/3

f(x) = (x^3)/3 with lower limit = 2 
and upper limit = 3.
     = (3^3)/3 - (2^3)/3 
     = 19/3 
     = 6.3333

Example 1: 

Matlab




% MATLAB code for quad function
clc;
clear all;
ans = quad('x.^2',2,3);
disp(ans);


Output:

 

Explanation:

  • Here we are using .^ because x is a vector.
  • quad is the function to calculate the definite integral of the function x2. 
  • 2 and 3 are the upper and lower limits respectively of the integral.
  • disp is the function to display ans.

Now take another example, f(x) = ∫sin(x)dx  with lower limit = 0 and upper limit = 3.14

Solution:

We know that, ∫sin(x)dx = -cos(x)

f(x) = -cos(x) with lower limit = 0
and upper limit = 3.14
    = -cos(3.14) - (-cos(0)) 
    = 1 + 1 
    = 2

Example 2:

Matlab




% MATLAB code for quad function
ans = quad('sin(x)',0,3.14);
disp(ans);


Output:

 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads