Categories

# Matlab Trapz

The MATLAB function trapz (x, y, n) where y is the integral for x, approximates the integral of a function y=f(x) using the trapezoidal rule, and n (optional) performs integration along with dimension n.

### Syntax

1. Z = trapz(Y)
2. Z = trapz (X, Y)
3. Z = trapz (…, dim)

### Example1

Use the MATLAB function trapz (x, y) to approximate the cost of the integral

and by comparison with the exact value, evaluate the percent error when n=5 and n=10.

Solution

The exact value is found from

For the approximation using the trapezoidal rule, we let x5represent the row vector with n=5, and x10 the vector with n=10, that is, ∆x =1/5 and ∆x=1/10, respectively. The corresponding values are denoted as y5and y10, and the areas under the curve as area5 and area10, respectively.

Create the following Script

1. x5=linspace (1,2,5);
2. x10=linspace (1,2,10);
3. y5=1. /x5; y10=1. /x10;
4. area5=trapz (x5, y5), area10=trapz (x10, y10)

MATLAB display the following result:

1. area5 =
2.           0.6970
3. area10 =
4.              0.6939

The percent error when ∆x =1/5 is used is

The percent error when ∆x =1/10 is used is

### Example2

The integral

where τ is a dummy variable of integration, is called the error function, and it is used extensively in communications theory. Use the MATLAB trapz (x, y) function to find the area under this integral with n=10 when the upper limit of integration is t=2.

Solution

Create the following Script

1. t=linspace (0,2,10);
2. y=exp(-t.^2);
3. area=trapz (t, y)

MATLAB displays the following result:

1. area =
2.             0.8818

### Example3

The i-v (current-voltage) relation of a non-linear electrical machine is given by

where v(t)=sin3t.

By any means, find

• The instantaneous power is
p(t)=v(t)i(t)=0.1 sin3t(e0.2sin3t-1)
• The energy is the integral of the instantaneous is An analytical solution of the last integral is possible using integration by parts, but it is not easy. We can try the MATLAB int (f, a, b) function where f is a symbolic expression, and a and b are the lower and upper limits of integration, respectively.

When MATLAB cannot found a solution, it returns a warning. For example, MATLAB returns the following message when integration is attempted with the symbolic expression of equation

1. t=sym(‘t’);
2. s=int (0.1*sin(3*t) *(exp (0.2*sin(3*t))-1),0,10)

When this script is executed, MATLAB show the following message.

1. Warning: Explicit integral could not be found.

Next, we will find and sketch the power and energy by the trapezoidal rule using the MATLAB trapz (x, y) function. For this example, we choose n=100, so that ∆x=1/100. The MATLAB script below will compute and plot the power.

1. t=linspace (0,10,100);
2. v=sin (3. *t); i=0.1. *(exp (0.2.*v)-1); p=v.*i;
3. plot(t,p); grid; title(‘Power vs Time’); xlabel(‘seconds’); ylabel(‘watts’)

The power varies in a uniform fashion as shown by the plot of figure

The MATLAB script below computes and plots the energy.

1. energy=trapz (t, p), plot (t, energy, ‘+’); grid; title (‘Energy vs Time’); …
2. xlabel(‘seconds’); ylabel(‘joules’)
3. energy =
4.                0.1013

Thus, the value of the energy is 0.1013 joule. The energy is shown in figure: