Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

8 - Chapman MATLAB Programming for Engineers

.pdf
Скачиваний:
91
Добавлен:
12.03.2015
Размер:
1.97 Mб
Скачать

Figure 10.5: Linear interpolation

to find the two values in the data between which the desired point falls. When these two values are found, the interpolation equation above can be applied. These steps are performed by the Matlab interpolation function interp1.

yi = interp1(x,y,xi) Returns vector yi of the length of xi, containing the interpolated y values corresponding to xi using linear interpolation. The vector x, which must have values in ascending order, specifies the points at which the data y is given. Vectors x and y must be of the same length. The values of xi must be within the range of the x values.

To illustrate use of linear interpolation, consider again the temperature data from the previous section. The range of x in this data is 0.0 to 5.0, so interpolated values can be found for data in this range. Computing linearly interpolated values for x = 2.6 and x = 4.9:

>>x = 0:5;

>>y = [0 20 60 68 77 110];

>>yi = interp1(x,y,[2.6 4.9]) yi =

64.8000 106.7000

If the second argument of the interp1 function is a matrix, the function returns a row vector with the same number of columns, and each value returned will be interpolated from its corresponding

217

column of data. Assume the following data has been acquired:

Time, s

Temp 1

Temp 2

Temp 3

0

0

0

0

1

20

25

52

2

60

62

90

3

68

67

91

4

77

82

93

5

110

103

96

Storing this data in a matrix and interpolating at the time 2.6 seconds:

>>x = (0:5)’;

>>y(:,1) = [0,20,60,68,77,110]’;

>>y(:,2) = [0,25,62,67,82,103]’;

>>y(:,3) = [0,52,90,91,93,96]’;

>>temps = interp1(x,y,2.6) temps =

64.8000 65.0000 90.6000

Cubic-Spline Interpolation

A cubic spline is a third-degree polynomial computed to provide a smooth curve between the two data points bounding the point for which an interpolated value is to be determine and to provide a smooth transition from the third-degree polynomial between the previous two points.

The interpolating equation is

yi = a1(xi − x(k))3 + a2(xi − x(k))2 + a3(xi − x(k)) + a4

for which x(k) ≤ xi ≤ x(k + 1). The coe cients a1, a2, a3 and a4 are determined so that the following three conditions are met:

1.The polynomial must pass through the data points at its end points x(k) and x(k + 1). For xi = x(k), this requires that yi = y(k), so that a4 = y(k).

2.The slopes (first derivatives) of cubic polynomials in adjacent data intervals must be equal at their common data point.

3.The curvatures of adjacent polynomials must be equal at their common data point.

The corresponding Matlab function is:

yi = spline(x,y,xi) Returns vector yi of the length of xi, containing the interpolated y values corresponding to xi using cubic-spline interpolation. The vector x, which must have values in ascending order, specifies the points at which the data y is given. Vectors x and y must be of the same length. The values of xi must be within the range of the x values.

218

To illustrate, apply cubic-spline interpolation to the temperature data considered above.

>>x = 0:5;

>>y = [0,20,60,68,77,110];

>>temp = spline(x,y,[2.6,4.9]) temp =

67.3013 105.2020

To compute and plot linear and cubic-spline interpolation curves over a range of values, an xi vector with the desired resolution of the curve can be generated and used as the third parameter in the interp1 function. For example:

% Comparison of linear and cubic spline interpolation

x = (0:5);

y = [0,20,60,68,77,110];

xi = 0:0.1:5;

ylin = interp1(x,y,xi);

ycub = spline(x,y,xi);

plot(xi,ylin,’:’,xi,ycub,x,y,’o’),...

legend(’Linear’,’Cubic’,’Measured’,4),...

title(’Linear and cubic spline interpolation’),...

xlabel(’x’),...

axis([-1,6,-20,120]),...

grid

The resulting plot is shown in Figure 10.6.

10.4Applied Problem Solving: Human Hearing

To illustrate one-dimensional interpolation, consider the following example: The threshold of audibility (i.e. the lowest perceptible sound level) of the human ear varies with frequency. Plotting frequency in Hz on a log scale against sound pressure level in dB, normalized so that 0 dB appears at 1000 Hz is done with the following script:

f = [20:10:100 200:100:1000 1500 2000:1000:10000]; % frequency in Hz

spl = [76 66 59 54 49 46 43 40 38 22 ... % sound pressure level in dB

14 9 6 3.5 2.5 1.4 0.7 0 -1 -3 ...

-8 -7 -2 2 7 9 11 12];

semilogx(f,spl,’-o’),...

xlabel(’Frequency, Hz’),...

ylabel(’Relative Sound Pressure Level, dB’),...

title(’Threshold of human hearing’),grid on

219

Linear and cubic spline interpolation

120

 

 

 

 

 

 

 

100

 

 

 

 

 

 

 

80

 

 

 

 

 

 

 

60

 

 

 

 

 

 

 

40

 

 

 

 

 

 

 

20

 

 

 

 

 

 

 

0

 

 

 

 

 

Linear

 

 

 

 

 

 

 

Cubic

 

 

 

 

 

 

 

Measured

 

−20

0

1

2

3

4

5

6

−1

 

 

 

 

x

 

 

 

Figure 10.6: Comparison of linear and cubic spline interpolation

Threshold of human hearing

 

80

 

 

 

 

70

 

 

 

 

60

 

 

 

dB

 

 

 

 

Level,

50

 

 

 

 

 

 

 

Pressure

40

 

 

 

 

 

 

 

Sound

30

 

 

 

 

 

 

 

Relative

20

 

 

 

 

 

 

 

 

10

 

 

 

 

0

 

 

 

 

−10

102

103

104

 

101

Frequency, Hz

Figure 10.7: Plot of Threshold of Human Hearing

220

The resulting plot is shown in Figure 10.7, where by default, the data points have been connected with straight lines.

Based on this plot, the human ear is most sensitive to tones around 3 kHz. The function interp1 can be used to estimate the sound pressure level in several di erent ways at a frequency of 2.5 kHz.

>>slin = interp1(f,spl,2500,’linear’) % linear interpolation slin =

-5.5000

>>scub = interp1(f,spl,2500,’spline’) % cubic spline interpolation scub =

-5.6641

>>snn = interp1(f,spl,2500,’nearest’) % nearest neighbor interpolation snn =

-8

where nearest neighbor is another interpolation method, in which, as implied by its name, interpolates with the nearest sample in the original data. Note the di erences in these results. The first result returns exactly what is shown in the figure at 2.5 kHz since Matlab linearly interpolates between data points on plots. Cubic spline interpolation fits a cubic polynomial to each data interval, so it returns a slightly di erent result. The poorest interpolation in this case is the nearest neighbor.

How is an interpolation method to be chosen for a given problem? In many cases, linear interpolation is su cient, which is why it is the default method. While nearest neighbor produced poor results here, it is often used when speed is important or the data set is large. The most time-consuming method is spline, but it often produces the most desirable results.

Now use cubic interpolation to investigate the data at a finer interval near the minimum.

fi =

linspace(2500,5000);

 

spli

= interp1(f,spl,fi,’cubic’);

% interpolate near minumum

k = find(f>=2000 & f<=5000);

% find indices near minumum

semilogx(f(k),spl(k),’--o’,fi,spli),... % plot orig & cubic data

legend(’Linear’,’Cubic’),...

xlabel(’Frequency, Hz’),...

ylabel(’Relative Sound Pressure Level, dB’),...

title(’Threshold of human hearing’),grid on

The resulting plot is shown in Figure 10.8. By specifying a finer resolution on the frequency axis and using cubic convolution, a smoother estimate of the sound pressure level is generated. Note how the slope of the cubic solution does not change abruptly at the data points.

With the cubic spline interpolation, a better estimate can be made of the frequency of greatest sensitivity.

>> [splmin,kmin] = min(spli)

% minimum and index of minimum

221

Relative Sound Pressure Level, dB

Threshold of human hearing

−2

Linear

Cubic

−3

−4

−5

−6

−7

−8

−9

103

104

 

Frequency, Hz

Figure 10.8: Plot of threshold of human hearing near minimum

splmin = -8.2683

kmin = 32

>> fmin = fi(kmin) % frequency at minimum fmin =

3.2828e+003

Thus, by this analysis, the human ear is most sensitive to tones near 3.3 kHz.

222

Section 11

Integration and Di erentiation

Integration and di erentiation are the key concepts presented in the first two calculus courses and they are fundamental to solving many engineering and science problems. While many of these problems can be solved analytically, there are also many problems that require numerical integration or numerical di erentiation techniques.

11.1Numerical Integration

The integral of a function f(x) for a ≤ x ≤ b can be interpreted as the area under the curve of f(x) between x = a and x = b, as shown in Figure 11.1. Denoting this area as A, the integral is written as

b

A = f(x)dx

a

Definitions:

Integrand: f(x)

Lower limit of integration: a

Upper limit of integration: b

Variable of integration: x

Numerical integration, called quadrature, involves methods for estimating the value of a definite integral. In these methods, the function f(x) is estimated or approximated by another function fˆ(x), chosen so that the area under fˆ(x) is easy to compute. The better the estimate of f(x) by fˆ(x), the better the estimate of the integral of f(x). Two of the most common numerical integration techniques estimate f(x) with a set of piecewise linear functions or with a set of piecewise parabolic functions. If the function is estimated with piecewise linear functions, the area of the trapezoids that compose the area under the piecewise linear functions is the approximation to the desired

223

Figure 11.1: Integral of f(x) from a to b

integral, and the method is known as the trapezoidal rule. If the function is estimated with piecewise quadratic functions, the technique is called Simpson’s rule.

Trapezoidal Rule

In the trapezoidal rule for integration, the interval [a, b] is divided into n equal subintervals and the curve f(x) is approximated by fˆ(x) on each subinterval as a straight line connecting the values of f(x) at the ends of each subinterval. The integral A is the sum of the approximate integrals on each subinterval. The width of each subinterval is

x = b − a n

The range of values of x on subinterval i is

[xi, xi+1] = [xi, xi + ∆x] = [a + ix, a + (i + 1)∆x], i = 0, . . . , n − 1

The approximation of f(x) on subinterval i is shown in Figure 11.2. The approximating curve fˆ(x) is represented by the dashed line. The approximate area Ai of f(x) over this subinterval is that of the trapezoid under fˆ(x)

Ai (xi+1 − xi)

f(xi) + f(xi+1) ∆x

2

= 2 (f(xi) + f(xi+1))

The full integral A is then approximated by

AT =

 

n

x

(f(xi) + f(xi+1))

i=0

 

 

2

=

 

x

(f(x0) + 2f(x1) + 2f(x2) + · · · + 2f(xn−1) + f(xn))

 

 

2

The Matlab function for trapezoidal rule integration is

224

Figure 11.2: Approximation of f(x) from xi to xi+1

z = trapz(x,y) Integral of y with respect to x by the trapezoidal rule. x and y must be vectors of the same length, or x must be a column vector and y an array whose first non-singleton dimension is length(x). trapz operates along this dimension.

z = trapz(x,y,dim) Integrates across dimension dim of y. The length of x must be the same as size(y,dim)).

To illustrate integration, consider the function humps(x), a demonstration function provided by Matlab that has strong peaks near x = 0.3 and x = 0.9, shown in Figure 11.3.

Trapezoidal integration of humps(x)

 

100

 

 

 

 

 

 

 

80

 

 

 

 

 

 

 

60

 

 

 

 

 

 

humps(x)

40

 

 

 

 

 

 

 

 

 

 

 

 

 

 

20

 

 

 

 

 

 

 

0

 

 

 

 

 

 

 

−20

−0.5

0

0.5

1

1.5

2

 

−1

 

 

 

 

x

 

 

 

Figure 11.3: Trapezoidal integration of humps(x)

Using computed samples of the humps function, the function trapz approximates the area using the trapezoidal approximation. For n = 18 subintervals, the integral approximation is given by:

>> x = linspace(-1,2,18)

225

>>y = humps(x);

>>area = trapz(x,y) area =

25.1406

Based on the figure, this is probably not a very accurate estimate of the area. However, if a finer discretization is used by increasing the number of subintervals to 401, better accuracy is achieved:

>>x = linspace(-1,2,401);

>>y = humps(x);

>>area = trapz(x,y)

area = 26.3449

This area agrees with the analytic integral to five significant digits.

Another integral of interest is that for which the upper limit is the variable x:

x

f(u)du

a

where the variable of integration has been changed to u to avoid confusion with the upper limit x. The definite integral from the lower limit of integration, a, to any point x is found by evaluating the integral at x. Using the trapezoidal rule, tabulated values of the cumulative integral are computed using the function cumtrapz:

z = cumtrapz(x,y) Computes the cumulative integral of y with respect to x using trapezoidal integration. x and y must be vectors of the same length, or x must be a column vector and y an array whose first non-singleton dimension is length(x). cumtrapz operates across this dimension.

z = cumtrapz(x,y,dim) Integrates along dimension dim of y. The length of x must be the same as size(y,dim)).

For example, consider integration of humps(x)

x = linspace(-1,2,201);

y = humps(x);

z = cumtrapz(x,y);

plot(x,y,x,z,’--’),...

title(’Cumulative integral of humps(x)’),...

xlabel(’x’),ylabel(’humps(x) and integral of humps(x)’),...

grid,legend(’humps(x)’,’integral of humps(x)’)

The resulting plot is shown in Figure 11.4.

Example 11.1 Velocity from an accelerometer

226

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]