Categories

# Matlab trapz()

MATLAB has incorporated the function trapz to process numerical integration using trapezoidal rule-following unit spacing. This rule is defined to approximate a numerical integration for a definite time frame by dividing the area under the plot into tine sized trapezoids. It helps in forming easily computable areas. The function returns the single return value of type scalar which is the final value of the integration.

Preferably trapz is recommended to use for numerical integration on discrete type data sets.

### Syntax:

This integrating function trapz accepts different input arguments to decide on the behavior of the integration function, being used with different syntaxes respectively.

They are:

The input arguments that contribute to different syntaxes of the function trapz can be explained as below:

## Examples of Matlab trapz()

Here are the following examples mention below:

## Example 1 – Numerical integration with Unit spacing

This operation can be executed by applying the syntax Q = trapz(Y) in the trapz function implementation in the MATLAB code.

## Code:

The below code snippet is written to execute trapz operation on input numeric ‘Y’ as f(x)= 10*2^x.

``````Y = [10 20 40 80 160 320];
Q = trapz(Y)``````

## Example 2 – Numerical integration with Non-Unit spacing

This operation can be executed by applying the syntax Q = trapz(Y, X) in the trapz function implementation in the MATLAB code.

## Code:

The below code is written to call trapz function on input numeric data ‘Y’ having point spacing value of pi/10.

``````X = 0: pi/10:2*pi;
Y = cos(X);
Q = trapz(X,Y)``````

## Example 3 – Numerical integration with non-uniform spacing

This operation can be executed by applying the syntax Q = trapz(Y, X, dim) in the trapz function implementation in the MATLAB code.

## Code:

The below code is written to call the trapz function on the input numeric data ‘Y’ with non-uniform point spacing values defined by ‘X’ with the value of ‘dim’ as ‘2’.

The point spacing is obtained by matrix ‘X’ which indicates that the trapezoids are formed non-uniformly.

The rows in input numeric data ‘Y’ is obtained from velocity data taken at 3 different trials.

``````X = [1 3.5 5 10];
Y = [15.2   27.7   19.6   13.2;
24.8   17.0  20.5   24.5;
14.9   26.5  10.2   13.8];
Q = trapz(X,Y,2)``````

## Output:

Trapz is executed on each row of the input numeric ‘Y’ as the value for ‘dim’ is set to 2. The resultant output is a column vector having an integration result for each row.

## Example 4 – Multiple numerical integrations

The integration function trapz can also be used to perform double integral i.e integration in the format:

In order to perform trapz function on double integral expression, the function is called in the nested loop as demonstrated in the below example:

## Code:

``````p = -5:.1:5;
q = -3:.1:3;
[P,Q] = meshgrid(p,q);
F = P.^2 + Q.^2;
I = trapz(q,trapz(p,F,2))``````

## Example 5 – Real-time application of trapz

The trapz in MATLAB plays a vital role in integration calculations applied in real-time applications.

## Code:

Trapz can be used to compute the average energy exhibited by an electrical machine for which v-i nonlinear relation is designed as:

`The instantaneous voltage v(t)= sin(3t).`

In the below code snippet the point spacing is defined by ‘time’ which is non-unit uniform spacing with the value of ‘10’.

``The instantaneous power is calculated as p = f(i,v) = v*i.``

The average energy (enrgy)  is calculated by applying the trapz function.

``````time=linspace (0,10,100);
volt=sin (3. *time); i=0.1*(exp (0.2.*volt)-1); p=volt.*i;
enrgy=trapz (time, p), plot (time, enrgy, '+'); grid; title ('Energy vs Time'); ...
xlabel('in seconds'); ylabel('in joules')``````