EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
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:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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
SPSS Course Bundle - 14 Courses in 1 | 5 Mock Tests
34+ Hours of HD Videos
14 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
MICROSOFT AZURE Course Bundle - 15 Courses in 1 | 12 Mock Tests
62+ Hour of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
HADOOP Course Bundle - 32 Courses in 1 | 4 Mock Tests
125+ Hour of HD Videos
32 Courses
4 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
INFORMATICA Course Bundle - 7 Courses in 1
47+ Hours of HD Videos
7 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
MATLAB Course Bundle - 5 Courses in 1 | 3 Mock Tests
 11+ Hours of HD Videos
5 Courses
3 Mock Tests & Quizzes
  Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

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

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

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more