Numerical Integration with MATLABhttp://cnx.org/contenthttp://cnx.org/content/m41454/latest/m41454Numerical Integration with MATLAB1.32011/09/27 01:31:18 GMT-52011/12/02 02:14:43.339 US/CentralSerhatBeyenirSerhat Beyenirserhat_beyenir@bcit.caserhat_beyenirserhat_beyenirserhat_beyenirIntegrationMechanical workPV diagramsTrapezoidal RuleWork doneMathematics and StatisticsBasic Numerical Integration with MATLABen
This chapter essentially deals with the problem of computing the area under a curve. First, we will employ a basic approach and form trapezoids under a curve. From these trapezoids, we can calculate the total area under a given curve. This method can be tedious and is prone to errors, so in the second half of the chapter, we will utilize a built-in MATLAB function to carry out numerical integration.
A Basic ApproachThere are various methods to calculating the area under a curve, for example, Rectangle Method, Trapezoidal Rule and Simpson's Rule. The following procedure is a simplified method. Consider the curve below:
Each segment under the curve can be calculated as follows:12y0y1Δx12y1y2Δx12y2y3ΔxTherefore, if we take the sum of the area of each trapezoid, given the limits, we calculate the total area under a curve. Consider the following example.
Given the following data, plot an x-y graph and determine the area under a curve between x=3 and x=30
Indexx [m]y [N]1327.0021014.503159.404206.705255.306304.50

Data Set

First, let us enter the data set. For x, issue the following command x=[3,10,15,20,25,30];. And for y, y=[27,14.5,9.4,6.7,5.3,4.5];. If yu type in [x',y'], you will see the following tabulated result. Here we transpose row vectors with ' and displaying them as columns:ans =
3.0000 27.0000
10.0000 14.5000
15.0000 9.4000
20.0000 6.7000
25.0000 5.3000
30.0000 4.5000Compare the data set above with the given information in the question.
To plot the data type the following:plot(x,y),title('Distance-Force Graph'),xlabel('Distance[m]'),ylabel('Force[N]'),gridThe following figure is generated:To compute dx for consecutive x values, we will use the index for each x value, see the given data in the question.:dx=[x(2)-x(1),x(3)-x(2),x(4)-x(3),x(5)-x(4),x(6)-x(5)];dy is computed by the following command:dy=[0.5*(y(2)+y(1)),0.5*(y(3)+y(2)),0.5*(y(4)+y(3)),0.5*(y(5)+y(4)),0.5*(y(6)+y(5))];dx and dy can be displayed with the following command: [dx',dy']. The result will look like this:
[dx',dy']
ans =
7.0000 20.7500
5.0000 11.9500
5.0000 8.0500
5.0000 6.0000
5.0000 4.9000Our results so far are shown belowx [m]y [N]dx [m]dy [N]327.001014.507.0020.75159.405.0011.95206.705.008.05255.305.006.00304.505.004.90

x, y and corresponding differential elements

If we multiply dx by dy, we find da for each element under the curve. The differential area da=dx*dy, can be computed using the 'term by term multiplication' technique in MATLAB as follows:da=dx.*dy
da =
145.2500 59.7500 40.2500 30.0000 24.5000Each value above represents an element under the curve or the area of trapezoid. By taking the sum of array elements, we find the total area under the curve.sum(da)
ans =
299.7500The following illustrates all the steps and results of our MATLAB computation. x [m]y [N]dx [m]dy [N]dA [Nm]327.001014.507.0020.75145.25159.405.0011.9559.75206.705.008.0540.25255.305.006.0030.00304.505.004.9024.50299.75

Computation of the approximate area under a curve

The Trapezoidal RuleSometimes it is rather convenient to use a numerical approach to solve a definite integral. The trapezoid rule allows us to approximate a definite integral using trapezoids.
The <code>trapz</code> CommandZ = trapz(Y) computes an approximation of the integral of Y using the trapezoidal method.
Now, let us see a typical problem.Given
Areax25x2,
an analytical solution would produce 39. Use trapz command and solve it
Initialize variable x as a row vector, from 2 with increments of 0.1 to 5: x=2:.1:5;Declare variable y as y=x^2;. Note the following error prompt: ??? Error using ==> mpower
Inputs must be a scalar and a square matrix. This is because x is a vector quantity and MATLAB is expecting a scalar input for y. Because of that, we need to compute y as a vector and to do that we will use the dot operator as follows: y=x.^2;. This tells MATLAB to create vector y by taking each x value and raising its power to 2. Now we can issue the following command to calculate the first area, the output will be as follows:area1=trapz(x,y)
area1 =
39.0050Notice that this numerical value is slightly off. So let us increase the number of increments and calculate the area again: x=2:.01:5;
y=x.^2;
area2=trapz(x,y)
area2 =
39.0001Yet another increase in the number of increments:x=2:.001:5;
y=x.^2;
area3=trapz(x,y)
area3 =
39.0000Determine the value of the following integral:x0xInitialize variable x as a row vector, from 0 with increments of pi/100 to pi: x=0:pi/100:pi;Declare variable y as y=sin(x);Issue the following command to calculate the first area, the output will be as follows:area1=trapz(x,y)
area1 =
1.9998let us increase the increments as above:x=0:pi/1000:pi;
y=sin(x);
area2=trapz(x,y)
area2 =
2.0000Summary of Key PointsIn its simplest form, numerical integration involves calculating the areas of segments that make up the area under a curve,MATLAB has built-in functions to perform numerical integration,Z = trapz(Y) computes an approximation of the integral of Y using the trapezoidal method.