EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial Trapezoidal Rule Matlab
 

Trapezoidal Rule Matlab

Priya Pedamkar
Article byPriya Pedamkar

Trapezoidal Rule Matlab

Introduction to Trapezoidal Rule Matlab

Trapezoidal rule is used in integration to compute the definite integral of the functions. It is used extensively in the process of numerical analysis. For the purpose of integration, trapezoidal rule considers the area under curve to be made up of small trapezoids and then calculates the total area by summing the area of all these small trapezoids. In MATLAB, we use ‘trapz function’ to get the integration of a function using trapezoidal rule.

 

 

Let us now understand the syntax of trapz function in Matlab:

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax:

I = trapz (X)

I = trapz (Y, X)

Description:

  • I = trapz (X) is used to calculate the integral of X, using trapezoidal rule. For vectors, it will give approximate integral. For a matrix, it will provide integration over all columns and will return the integration values in a row vector
  • I = trapz (Y, X) will integrate X w.r.t coordinates given by Y

Examples of Trapezoidal Rule Matlab

Let us now understand the code to calculate the integration in MATLAB using ‘trapezoidal rule’.

Example #1

In this example, we will take an array representing the (x^2 + 2) and will integrate it using trapezoidal rule. We will follow the following 2 steps:

  • Create the input array
  • Pass the function to the trapz function

Syntax:

X = [3 6 11 18 27]

[Creating the input array for the function x^2 + 2, in the range 1 to 5]

I = trapz (X)

[Calling the trapz function and passing the input array as an argument] [Mathematically, the trapezoidal rule will give integration of (x^2 + 2) as 50]

Input:

X = [3 6 11 18 27] I = trapz (X)

Output:

Trapezoidal Rule Matlab-1.1

As we can see in the output, we have obtained integration of our input function as 50, which is the same as expected by us.

Example #2

In this example, we will take a function of sin and will integrate it using the trapezoidal rule. We will follow the following 3 steps:

  1. Define the limits for input sin function
  2. Create a sin function
  3. Pass the input function and required limits to the trapz function

Syntax:

Y = 0:100;

[Defining the limits for trapezoidal rule]

X = 10 - 10 * sin (pi / 200 * Y);

[Creating the sine wave]

I = trapz (Y, X)

[Calling the trapz function and passing the input function as an argument] [Mathematically, the trapezoidal rule will give integration of (10 – 10 * sin (pi / 200 * Y)) as 363.3933]

Input:

Y = 0:100;
X = 10 - 10 * sin (pi / 200 * Y);
I = trapz (Y, X)

Output:

Trapezoidal Rule Matlab-1.2

As we can see in the output, we have obtained integration of our input function as 363.3933, which is the same as expected by us.

Example #3

In this example, we will take a cos function and will integrate it using the trapezoidal rule. We will follow the following 3 steps:

  1. Define the limits for input cos function
  2. Create a cos function
  3. Pass the input function and required limits to the trapz function

Syntax:

Y = 0:100;

[Defining the limits for trapezoidal rule]

X = 20 - 50 * cos (2*pi / 100 * Y);

[Creating the cos wave]

I = trapz (Y, X)

[Calling the trapz function and passing the input function as an argument] [Mathematically, the trapezoidal rule will give integration of (20 – 50 * cos (2*pi / 100 * Y)) as 2.0000e+03]

Input:

Y = 0:100;
X = 20 - 50 * cos (2*pi / 100 * Y);
I = trapz (Y, X)

Output:

Output-1.3

Example #4

In this example, we will take a matrix and will integrate its rows using the trapezoidal rule. We will follow the following 3 steps:

  1. Create the vector
  2. Create a matrix containing observations i.e input data
  3. Pass the vector and matrix to the trapz function

Syntax:

Y = [2 4.5 7 9];

[Creating the vector]

X = [5    8    6     13;
4     7   15    15;
4     5   10.2 18];

[Creating the input matrix with observations]

I = trapz (Y, X, 2)

[Calling the trapz function and passing the input array and matrix as arguments. We have also passed a third argument as ‘2’, which implies that our target data is present in the rows]

Input: 

Y = [2 4.5 7 9];
X = [5    8    6     13;
4     7   15    15;
4     5   10.2 18];
I = trapz (Y, X, 2)

Output:

Output-1.4

As we can see in the output, we have obtained integration of our input as a column vector, with each element corresponding to one row of the input matrix ‘X’.

Example #5

In this example, we will take a 3×5 matrix and will integrate its rows using the trapezoidal rule. We will follow the following 3 steps:

  1. Create the vector
  2. Create a 3×5 matrix containing observations i.e input data
  3. Pass the vector and matrix to the trapz function

Syntax:

Y = [1 5 8 10 14];

[Creating the vector]

X = [15      8     4     3   0;
12     17    5    11  3;
8      15    10   8   6];

[Creating the input matrix with observations]

I = trapz (Y, X, 2)

[Calling the trapz function and passing the input array and matrix as arguments. The third argument ‘2’ implies that our target data is present in the rows]

Input:

Y = [1 5 8 10 14];
X = [15      8     4     3   0;
12     17    5    11  3;
8      15    10   8   6];
I = trapz (Y, X, 2)

Output:

Output-1.5

As we can see in the output, we have obtained integration of our input as a column vector, with each element corresponding to one row of the input matrix ‘X’.

Recommended Articles

This is a guide to Trapezoidal Rule Matlab. Here we also discuss the introduction and syntax of trapezoidal rule matlab along with different examples and its code implementation. You may also have a look at the following articles to learn more –

  1. MATLAB Derivative
  2. Factorial in Matlab
  3. Fourier Series Matlab
  4. Bar Plot Matlab

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW