Thuật toán Algorithms (Phần 9)
lượt xem 6
download
Thuật toán Algorithms (Phần 9)
Tham khảo tài liệu 'thuật toán algorithms (phần 9)', khoa học tự nhiên, toán học phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Bình luận(0) Đăng nhập để gửi bình luận!
Nội dung Text: Thuật toán Algorithms (Phần 9)
 CURVE FITTING 73 the same steps of determining the coefficients for each of the spline pieces by solving the system of linear equations derived from imposing constraints on how they are joined. Method of Least Squares A very common experimental situation is that, while the data values that we have are not exact, we do have some idea of the form of the function which is to fit the data. The function might depend on some parameters and the curve fitting procedure is to find the choice of parameters that “best” matches the observed values at the given points. If the function were a poly nomial (with the parameters being the coefficients) and the values were exact, then this would be interpolation. But now we are considering more general functions and inaccurate data. To simplify the discussion, we’ll concentrate on fitting to functions which are expressed as a linear combination of simpler functions, with the unknown parameters being the coefficients: f(x) = Clfl(X) + c2f2(x) +**a+ cA4fdx). This includes most of the functions that we’ll be interested in. After studying this case, we’ll consider more general functions. A common way of measuring how well a function fits is the leastsquares criterion: the error is calculated by adding up the squares of the errors at each of the observation points: E = c (f(xj)yj)2. l
 74 CHAPTER 6 To find the choices of cl and c2 which minimize this error, we simply need to set the derivatives dE/dq and dE/dcz to zero. For cl we have: g =2(cl.fl(G) + Czfz(~1)  Yl)fl(~l) 1 + 2(Clfl(~Z) + czfz(z2)  yz)f1(52) +2(clfl(~3)+CZf2(~3)Y3)fi(X3). Setting the derivative equal to zero leaves an equation which the variables cl and cs must satisfy (fi(~i), etc. are all “constants” with known values): clvl(~1vl(~l) + fl(~Z)fl(~Z) + fl(Z3)fi(53)] +c2[f2(~l)fl(~l) + f2(~2)fl(z2) + f2(~3)fl(z3)] = Yl.fl(G) + YZfl(52) + Y3fl(53). We get a similar equation when we set the derivative dE/dc2 to zero. These rather formidablelooking equations can be greatly simplified using vector notation and the “dot product” operation that we encountered briefly in Chapter 2. If we define the vectors x = (21, zz,23) and y = (yi, yz, ys) and then the dot product of x and y is the real number defined by XY = z1y1+ xzy2 + 23y3 Now, if we define the vectors fi = (fl(xi),fi(xz),fi(xs)) and fs = (fs(xi), f2(52), f2(23)) th e n our equations for the coefficients cl and cz can be very simply expressed: Clfi . fi + CZfi *fz = y . fi ClfZ.fi +c2f2 *f2 =yf2. These can be solved with Gaussian elimination to find the desired coefficients. For example, suppose that we know that the data points (1.0,2.05), (2.0,1.53), (4.0,1.26), (5.0,1.21), (8.0,1.13), (10.0,l.l). should be fit by a function of the form cl + cz/x. (These data points are slightly perturbed from the exact values for 1 + l/x). In this case, we have fi = (l.O,l.O, l.O,l.O,l.O, 1.0) and f2 = (1.0,0.5,0.25,0.2,0.125,0.1) so we have to solve the system of equations
 CURVE FITTING 75 with the result cl = 0.998 and c2 = 1.054 (both close to 1, as expected). The method outlined above easily generalizes to find more than two coefficients. To find the constants ~1~2,. . . ,CM in f(z) = clfl(z) + czfz(s) + *+* + CA4fM(Z) which minimize the least squares error for the point and observation vectors first compute the function component vectors fl = (fl(d, f1(s2), . . . , fl(ZN)), f2 = (f2(~1), f2(z2), . * *, f2(Ziv)), Then make up an MbyM linear system of equations AC = b with a,j = fi ”fy, b, = fj . y. The solution to this system of simultaneous equations yields the required coefficients. This method is easily implemented by maintaining a two dimensional array for the f vectors, considering y as the (M + 1)st vector. Then an array a[l..M, I..M+I] can be filled as follows: for i:=l to Mdo for j:==l to M+l do begin it:= 0.0; for k:=l to N do t:=t+f[i, k]*fb, k]; a[& j]:=t; end; and then solved using the Gaussian elimination procedure from Chapter 5. The method of least squares can be extended to handle nonlinear func tions (for example a function such as f(x) = cleC2Zsincg~), and it is often
 76 CHAPTER 6 used for this type of application. The idea is fundamentally the same; the problem is that the derivatives may not be easy to compute. What is used is an iterative method: use some estimate for the coefficients, then use these within the method of least squares to compute the derivatives, thus producing a better estimate for the coefficients. This basic method, which is widely used today, was outlined by Gauss in the 1820s.
 CURVE FITTING 77 Exercises 1. Approximate the function lgx with a degree 4 interpolating polynomial at the points 1,2,3,4, and 5. Estimat.e the quality of the fit by computing the sum of the squares of the errors at 1.5, 2.5, 3.5, and 4.5. 2. Solve the previous problem for the function sinx. Plot the function and the approximation, if that’s possible on your computer system. 3. Solve the previous problems using a cubic spline instead of an interpolat ing polynomial. 4. Approximate the function lgx with a cubic spline with knots at 2N for N between 1 and 10. Experiment with different placements of knots in the same range to try to obtain a better fit. 5. What would happen in least squares data fitting if one of the functions was the function Ii(x) = 0 for some i? 6. What would happen in least squares datafitting if all the observed values were O? 7. What values of a, b, c minimize the leastsquares error in using the function f(x) = ux log x + bx + c to approximate the observations f(1) = 0, f(4) = 13, f(8) = 41? 8. Excluding the Gaussian elimination phase, how many multiplications are involved in using the method of least squares to find M coefficients based on N observations? 9. Under what circumstances would the matrix which arises in leastsquares curve fitting be singular? 10. Does the leastsquares method work if two different observations are in cluded for the same point?
 7. Integration Computing the integral is a fundamental analytic operation often per formed on functions being processed on computers. One of two com pletely different approaches can be used, depending on the way the function is represented. If an explicit representation of the function is available, then it may be possible to do symbolic integrathn to compute a similar representation for the integral. At the other extreme, the function may be defined by a table, so that function values are known for only a few points. The most common situation is between these: the function to be integrated is represented in such a way that its value at any particular point can be computed. In this case, the goal is to compute a reasonable approximation to the integral of the func tion, without performing an excessive number of function evaluations. This computation is often called quadrature by numerical analysts. Symbolic Integration If full information is available about a function, then it may be worthwhile to consider using a method which involves manipulating some representation of the function rather than working with numeric values. The goal is to transform a representation of the function into a representation of the integral, in much the same way that indefinite integration is done by hand. A simple example of this is the integ,ration of polynomials. In Chapters 2 and 4 we examined methods for “symbolically” computing sums and products of polynomials, with programs that work.ed on a particular representation for the polynomials and produced the representation for the answers from the rep resentation for the inputs. The operation of integration (and differentiation) of polynomials can also be done in this way. If a polynomial 79
 80 CHAPTER 7 is represented simply by keeping the values of the coefficients in an array p then the integral can be easily computed as follows: !;;:I!downto 1 do p [i] :=p[iII/i; : ; This is a direct implementation of the wellknown symbolic integration rule Jc tie’ dt = xi/i for i > 0. Obviously a wider class of functions than just polynomials can be handled by adding more symbolic rules. The addition of composite rules such as integration by parts, udv=uv v du, / s can greatly expand the set of functions which can be handled. (Integration by parts requires a differentiation capability. Symbolic differentiation is some what easier than symbolic integration, since a reasonable set of elementary rules plus the composite chain rule will suffice for most common functions.) The large number of rules available to be applied to a particular function makes symbolic integration a difficult task. Indeed, it has only recently been shown that there is an algorithm for this task: a procedure which either returns the integral of any given function or says that the answer cannot be expressed in terms of elementary functions. A description of this algorithm in its full generality would be beyond the scope of this book. However, when the functions being processed are from a small restricted class, symbolic integration can be a powerful tool. Of course, symbolic techniques have the fundamental limitation that there are a great many integrals (many of which occur in practice) which can’t be evaluated symbolically. Next, we’ll examine some techniques which have been developed to compute approximations to the values of real integrals. Simple Quadrature Methods Perhaps the most obvious way to approximate the value of an integral is the rectangle method: evaluating an integral is the same as computing the area under a curve, and we can estimate the area under a curve by summing the areas of small rectangles which nearly fit under the curve, as diagrammed below.
 INTEGRATION 81 To be precise, suppose that we are to compute Jab f(x)dx, and that the interval [a, b] over which the integral is to be computed is divided into N parts, delimited by the points x1, x2,. . . ,xN+l. Then we have N rectangles, with the width of the ith rectangle (1 5 i 5 N)) given by x,+1  x,. For the height of the ith rectangle, we could use f(x,) or f(xi+l), but it would seem that the result would be more accurate if the value of f at the midpoint of the interval (f((xi + xi+l)/2)) is used, as in the above diagram. This leads to the quadrature formula which estimates the value of the integral ‘of f(x) over the interval from a = x1 to b = xN+l. In the common case where all the intervals are to be the same size, say x$+1  xi = 20, we have xi+1 + zz = (2i + l)w, so the approximation r to the integral is easily computed. function inf,rect(a, b: real; N: integer) : real; var i: intieger; w, i: real; begin r:=O; w:=(ba)/N; for i:=l to N do r:=r+w*f(aw,/2+i*w); intrect :==r; end ; Of course, as N gets larger, the answer becomes more accurate, For example, the following table shows the estimate produced by this function for J: dxlx (which we know to be In 2 = 0.6931471805599.. . ) when invoked with the call intrect(l.0,2.O,N) for N = 10,100,1000:
 82 CHAPTER 7 10 0.6928353604100 100 0.6931440556283 1000 0.6931471493100 When N = 1000, our answer is accurate to about seven decimal places. More sophisticated quadrature methods can achieve better accuracy with much less work. It is not difficult to derive an analytic expression for the error made in the rectangle method by expanding f(z) in a Taylor series about the midpoint of each interval, integrating, then summing over all intervals. We won’t go through the details of this calculation: our purpose is not to derive detailed error bounds, but rather to show error estimates for simple methods and how these estimates suggest more accurate methods. This can be appreciated even by a reader not familiar with Taylor series. It turns out that ~bf(Z)dz=7+~313+W5e5f... s where w is the interval width ((b  a)/N) and es depends on the value of the third derivative of f at the interval midpoints, etc. (Normally, this is a good approximation because most “reasonable” functions have small high order derivatives, though this is not always true.) For example, if we choose to make w = .Ol (which would correspond to N = 200 in the example above), this formula says the integral computed by the procedure above should be accurate to about six places. Another way to approximate the integral is to divide the area under the curve into trapezoids, as diagrammed below. This trapezoid method leads to the quadrature formula f(G) + f(si+d t = c (x,+1 Xi) l
CÓ THỂ BẠN MUỐN DOWNLOAD

Thuật toán Algorithms (Phần 1)
10 p  73  18

Thuật toán Algorithms (Phần 16)
10 p  60  15

Thuật toán Algorithms (Phần 2)
10 p  52  10

Thuật toán Algorithms (Phần 8)
10 p  55  9

Thuật toán Algorithms (Phần 11)
10 p  57  9

Thuật toán Algorithms (Phần 3)
10 p  60  8

Thuật toán Algorithms (Phần 12)
10 p  50  8

Thuật toán Algorithms (Phần 4)
10 p  49  7

Thuật toán Algorithms (Phần 13)
10 p  50  7

Thuật toán Algorithms (Phần 6)
10 p  57  7

Thuật toán Algorithms (Phần 10)
10 p  46  6

Thuật toán Algorithms (Phần 7)
10 p  43  6

Thuật toán Algorithms (Phần 5)
10 p  56  6

Thuật toán Algorithms (Phần 14)
10 p  31  5

Thuật toán Algorithms (Phần 15)
10 p  36  4

Thuật toán Algorithms (Phần 17)
10 p  35  4

Thuật toán Algorithms (Phần 18)
10 p  29  3