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.
- Z = trapz(Y)
- Z = trapz (X, Y)
- Z = trapz (…, dim)
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.
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.
- x5=linspace (1,2,5);
- x10=linspace (1,2,10);
- y5=1. /x5; y10=1. /x10;
- area5=trapz (x5, y5), area10=trapz (x10, y10)
- area5 =
- area10 =
The percent error when ∆x =1/5 is used is
The percent error when ∆x =1/10 is used is
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.
- t=linspace (0,2,10);
- area=trapz (t, y)
- area =
The i-v (current-voltage) relation of a non-linear electrical machine is given by
By any means, find
- The instantaneous power is
- 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.
- s=int (0.1*sin(3*t) *(exp (0.2*sin(3*t))-1),0,10)
- 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.
- t=linspace (0,10,100);
- v=sin (3. *t); i=0.1. *(exp (0.2.*v)-1); p=v.*i;
- 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
- energy=trapz (t, p), plot (t, energy, ‘+’); grid; title (‘Energy vs Time’); …
- xlabel(‘seconds’); ylabel(‘joules’)
- energy =
Thus, the value of the energy is 0.1013 joule. The energy is shown in figure: