Excel for Scientists and Engineers phần 5 pdf

48 365 0
Excel for Scientists and Engineers phần 5 pdf

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

170 EXCEL: NUMERICAL METHODS Option Explicit Function Bairstow(equation, reference) 'Obtains the coefficients of a regular polynomial (maximum order = 6). 'Polynomial is a cell formula. 'Polynomial can contain cell references or names. 'Poynomial can be text. 'Reference can be a cell reference or a name. Dim A() As Double, Root() As Double Dim J As Integer, N As Integer Dim pl As Integer, p2 As Integer, p3 As Integer Dim expnumber As Integer, ParenFlag As Integer Dim R As Integer, C As Integer Dim FormulaText As String, Reffext As String, NameText As String Dim char As String, term As String ReDim A(6) ' GET equation EITHER AS CELL FORMULA OR AS TEXT. If Application.lsText(equation) Then FormulaText = equation 'If in quotes, remove them. If Asc(Left(FormulaText, 1)) = 34 Then - FormulaText = Mid(FormulaText, 2, Len(Formu1aText) - 1) Else FormulaText = equation.Formula End If If Left(FormulaText, 1) = "=" Then FormulaText = Mid(FormulaText, 2, 1024) FormulaText = Application.ConvertFormula(FormulaText, xlAl , xlAl , - FormulaText = Application.Substitute(FormulaText, " 'I, "") 'remove all spaces 'GET THE NAME CORRESPONDING TO reference NameText = "" On Error Resume Next 'Handles case where no name has been assigned NameText = reference.Name.Name On Error GoTo 0 NameText = Mid(NameText, InStr(1, NameText, "!") + 1) 'HANDLE CASE WHERE reference IS A RANGE 'by finding cell in same row or column as cell containing function. If reference.Rows.Count > 1 Then ' xlAbsolute) R = equation.Row Set reference = Intersect(reference, Range(R & ":" & R)) C = equation.Column Set reference = Intersect(reference, Range(C & ":" & C)) Elself reference.Columns.Count > 1 Then This procedure contains code, not found in other procedures in this book, that allows the macro to accept a polynomial equation as a reference to a cell that contains a formula or as a reference to a cell that contains a formula as text. The procedure also handles an implicit reference. CHAPTER 8 ROOTS OF EOUATIONS 171 End If Reffext = reference.Address 'PARSE THE FORMULA INTO TERMS 'pointers: pl, beginning; p2, end of string. FormulaText = FormulaText & " I' 'add extra character for parsing ParenFlag = 0 'Keep track of left and right parentheses For J = 1 To Len(Formu1aText) char = Mid(FormulaText, J, 1) If char = "(" Then ParenFlag = ParenFlag + 1 If char = ")" Then ParenFlag = ParenFlag - 1 If ((char = "+" Or char = "-") And ParenFlag = 0) Or J = Len(Formu1aText) - pl = 1 Then -term = Mid(FormulaText, pl , J - pl) term = Application.Substitute(term, NameText, Reffext) p2 = J: pl = p2 'GET THE EXPONENT AND COEFFICIENT FOR EACH TERM 'p3: location of reference in term. If InStr(1, term, Reffext & 'IA") Then 'function returns zero if not found 'These are the x"2 and higher terms p3 = InStr(1, term, Reffext & """) expnumber = Mid(term, p3 + Len(Reffext) + 1, 1) term = Left(term, p3 - 1) 'term is now the coefficient part Elself InStr(1, term, Reffext) Then 'This is the x term p3 = InStr(1, term, Reffext) expnumber = 1 term = Left(term, p3 - 1) 'term is now the coefficient part Else 'This is the constant term End If If term = I"' Then term = "=I" 'If missing, Evaluate will require a string. If term = "+" Or term = "-" Then term = term & "1" If Right(term, 1) = "*" Then term = Left(term, Len(term) - 1) A(expnumber) = Evaluate(term) End If Next J 'RESIZE THE ARRAY For J = 6 To 0 Step -1 If A(J) <> 0 Then N = J: Exit For Next ReDim Preserve A(N) ReDim Root(1 To N, 1) 'REDUCE POLYNOMIAL SO THAT FIRST COEFF = 1 For J = 0 To N: A(J) = A(J) / A(N): Next 'SCALE THE POLYNOMIAL, IF NECESSARY '<code to be added later> expnumber = 0 172 EXCEL: NUMERICAL METHODS Call EvaluateByBairstowMethod(N, A, Root) Bairstow = Root() End Function Sub EvaluateByBairstowMethod(N, A, Root) Code adapted from Shoup, "Numerical Methods for the Personal Computer". Dim B() As Double, C() As Double Dim M As Integer, I As Integer, J As Integer, IT As Integer Dim P As Double, Q As Double, delP As Double, delQ As Double Dim denom As Double, S1 As Double Dim tolerance As Double ReDirn B(N + 2), C(N + 2) tolerance = 0.000000000000001 M=N While M > 0 If M = 1 Then Root(M, 0) = -A(O): Call Sort(Root, N): Exit Sub P = 0: Q = 0: delP = 1 : delQ = 1 For I = 0 To N: B(I) = 0: C(I) = 0: Next For IT = 1 To 20 If Abs(delP) < tolerance And Abs(delQ) < tolerance Then Exit For For J = 0 To M B(M - J) =A(M - J) + P * B(M - J + 1) + Q * B(M- J + 2) C(M - J) = B(M - J) + P * C(M - J + 1) + Q * C(M - J + 2) Next J denom = C(2) A 2 - C(l) * C(3) delP = (-B(l) * C(2) + B(0) * C(3)) / denom delQ = (-C(2) * B(0) + C(l) * B(1)) I denom P = P + delP Q = Q + delQ Next IT S1 =PA2+4*Q If S1 i 0 Then 'Handle imaginary roots Root(M, 0) = P / 2: Root(M, 1) = Sqr(-S1) / 2 Root(M - 1, 0) = P I2: Root(M - 1, 1) = -Sqr(-S1) / 2 'Handle real roots Root(M, 0) = (P + Sqr(S1)) / 2 Root(M - 1, 0) = (P - Sqr(S1)) / 2 Else End If For I = M To 0 Step -1: A(I) = B(I + 2): Next Wend End Sub '+++++++++i+++++++++++i+++++i+++i~++++++++i+++i++++i+++i+++ Sub Sort(Root, N) 'SORT ROOTS IN ASCENDING ORDER Dim I As Integer, J As Integer M=M-2 CHAPTER 8 ROOTS OF EOUATIONS 173 Dim tempo As Double, templ As Double For I = 1 To N For J = I To N If Root(l, 0) > Root(J, 0) Then tempo = Root(l, 0): temp1 = Root(l, 1) Root(l, 0) = Root(J, 0): Root(l, 1) = Root(J, 1) Root(J, 0) =tempo: Root(J, 1) = templ End If Next J Next I End Sub Figure 8-28. VBA code for the Bairstow custom function. (folder 'Chapter 08 Examples', workbook 'Bairstow', module 'BairstowFn') The syntax of the Bairstow function is Bairstow( equation, reference) Equation is a reference to a cell that contains the formula of the function, reference is the cell reference of the argument to be varied (the x value of F(x)). To return the roots of a polynomial of order N, you must select a range of cells 2 columns by N rows, enter the function and then press CONTROL+SHIFT+ENTER. The Bairstow function is an array function. Figure 8-29 shows a chart of the polynomial y=x3-0.0031x2+2.3 x 10-*x+5 x lo-' 6.OE-09 T 5.OE-0 A f:: //J 1.OE-09 f - -0.002 0.002 0.004 -1 .OE-09 -2.OE-09 I Figure 8-29. A regular polynomial with one real root and two imaginary roots. (folder 'Chapter 08 Examples', workbook 'Bairstow', sheet 'Example') 174 EXCEL: NUMERICAL METHODS The function has one real root and a pair of imaginary roots. Figure 8-30 shows a portion of the spreadsheet in which the Bairstow custom function is used to obtain the roots of the function. Figure 8-30. Calculation of all roots (real and imaginary) of a regular polynomial by the Bairstow custom function. (folder 'Chapter 08 Examples', workbook 'Bairstow', sheet 'Example 2') The formula =A2"3-0.0031 *A2"2+0.000000023*A2+0.000000005 was entered in cell B2 and the Bairstow custom function {=Bairstow(B2,A2)} in cells A27:B29. The real part of the root is in the left cell and the imaginary part in the right cell. Note that, since the custom function handles only polynomials with real coefficients, the complex roots (if any) occur in conjugate pairs. Finding Values Other than Zeroes of a Function Many of the preceding methods can be modified so as to find the x of a function for a y value other than zero. In this way you can find, for example, the point of intersection of two curves (the x value where the y value of one function equals they value of another function). Some examples follow. Using Goal Seek to Find the Point of Intersection of Two Lines It is a simple matter to use Goal Seek to find the intersection of two lines, as illustrated in Figure 8-3 1 CHAPTER 8 ROOTS OF EQUATIONS 175 120 100 80 60 40 20 0 -20 I 20 Figure 8-31. Finding the intersection of two lines in a chart. (folder 'Chapter 08 Examples', workbook 'Intersecting Lines', sheet 'Two Straight Lines') In the spreadsheet cells shown in Figure 8-32, the formula in cell 824 is =slope 1 *A24+ i n t 1 and the formula in cell C24 is =slope2*A24+int2 Both formulas use A24 as input. The formula in cell D24 (the target cell) is =B24-C24 Now use Goal Seek to vary A24 to make the target cell, D24, equal to zero. The result is shown in Figure 8-32. Figure 8-32. Using Goal Seek to find the intersection of two lines. (folder 'Chapter 08 Examples', workbook 'Intersecting Lines', sheet 'Two Straight Lines') 176 EXCEL: NUMERICAL METHODS This approach is very simple, but it has one major drawback-you must run Goal Seek . each time you want to find the point of intersection. A much more satisfactory approach is to use the Newton-Raphson technique to find the intersection point, as illustrated in the following section. The "drop line" in Figure 8-31 was added to the chart to emphasize the intersection point. The line was added to the chart in the following way: cell A25 contains the formula =A24 and cell B25 contains the value 0. The highlighted cells A24:B25 were copied and pasted in the chart to create a new series, as follows: Copy A24:B25, activate the chart, choose Paste Special from the Edit menu, check the boxes for Add Cells As New Series and X Values In First Column, press OK. Figure 8-33 shows the portion of the worksheet where the drop line is specified. Figure 8-33. Adding a "drop line" from the intersection of two lines. (folder 'Chapter 08 Examples', workbook 'Intersecting Lines', sheet 'Two Straight Lines') Using the Newton-Raphson Method to Find the Point of Intersection of Two Curves The Newton-Raphson method can be modified to find the x value that makes a function have a specified value, instead of the zero value that was used in a previous section. Equation 8-5 becomes x2 = (mxl -y1 +y2)/m (8-38) You can set up the calculation in the same way that was used for the Newton- Raphson method with intentional circular reference. In the following example we will find the intersection of a straight line and a curve (Figure 8-34). CHAPTER 8 ROOTS OF EQUATIONS 177 300 250 200 150 100 50 0 5 10 15 20 -50 Figure 8-34. Finding the intersection of two lines in a chart. (folder 'Chapter 08 Examples', workbook 'Intersecting Lines', sheet 'Using Circular Reference') A portion of the data table that generated the two lines is shown in Figure 8- 35. Figure 8-35. Portion of the data table for Figure 8-32. (folder 'Chapter 08 Examples', workbook 'Intersecting Lines', sheet 'Using Circular Reference') The formula in cell B5 is =slope*AS+int and in cell C5 =aa*A5"2+ bb*A5+cc Using the same method as in the preceding section, y1 is the function for which the slope is calculated, and y2 is the value used as the "constant." Of course, both yl and y2 change as the value of x changes. 178 EXCEL: NUMERICAL METHODS Figure 8-36. Using the Newton-Raphson method to find the intersection of two lines. (folder 'Chapter 08 Examples', workbook 'Intersecting Lines', sheet 'Using Circular Reference') Figure 8-36 shows the cells where the Newton-Raphson calculation is performed, using an intentional circular reference (refer to the section "The Newton-Raphson Method Using Circular Reference and Iteration" earlier in this chapter if the method of calculation is not apparent). The formula in cell G38 is =(C38+F38*A38-B38)/F38 The advantage of using the Newton-Raphson method with circular references, compared to using Goal Seek , is that calculation of the x, y coordinates of the intersection occurs automatically, "in the background." If you change one or more of the parameters (for example, if you change the slope of the straight line), the new intersection point and new drop line will be calculated and displayed on the chart. Using the Newton-Raphson Method to Find Multiple Intersections of a Straight Line and a Curve The preceding technique can be easily extended to find multiple intersections of two curves. The following figure illustrates how to find the two intersections of a horizontal straight line with a parabola, but many other types of curve can be handled. CHAPTER 8 ROOTS OF EQUATIONS 179 600 - 500 - 400 - 300 - 200 ' 100 I -30 -20 -10 0 10 20 30 Figure 8-37. Two intersections of a straight line and a curve, calculated by using the Newton-Raphson method with intentional circular references. (folder 'Chapter 08 Examples', workbook 'Intersecting Lines', sheet 'Using Circular Reference (2)') It is merely necessary to use two identical Newton-Raphson formulas and provide two different start values that will result in convergence to the two different "roots." Figure 8-38 illustrates the set-up of the table. Cells C66 and C67 contain the formula =$1$5 (pointing to the cell that contains a constant). Guided by Figure 8-37, initial x values of 10 and -10 were chosen. Figure 8-38 shows the cell values before the intentional circular references have been created. Figure 8-38. Calculating two intersections of a line and a curve by the Newton-Raphson method (before creating intentional circular references). (folder 'Chapter 08 Examples', workbook 'Intersecting Lines', sheet 'Using Circular Reference (2)') Once the formulas have been entered, replace the initial x values in cells A66 and A67 by the formulas =G66 and =G67, respectively, to create the two circular references. The "Cannot resolve circular references" message will be displayed [...]... the (1,l) element of the following matrix that gives a determinant value of zero 0. 75 0 .5 0. 25 0 .5 1 0 .51 [ 0. 25 0 .5 0. 75 Which elements in the matrix cannot be changed in order to give a determinant of zero? 12 Use the Bairstow custom function to find all of the roots of the polynomial 188 EXCEL: NUMERICAL METHODS X5- 1oX4+30X3-20X2-31X+30 13 Use the Bairstow custom function to find all of the roots... 1 65 256 361- The x1 terms are eliminated from column 1 of rows 2 , 3 and 4 by subtracting: Row 2 is normalized: The x2 terms are eliminated from column 2 of rows 3 and 4: EXCEL: NUMERICAL METHODS 194 I I 1 1 0.2 0.2 0.2 0 1 1 -0.4286 0 0 3 0 0 7 7 ]: ; 7 -5. 1429 -3.2 857 -30.429 65. 286 Row 3 is normalized and the x3 terms are eliminated from column 3 of row 4: 1 0.2 0.2 0.2 0 1 1 -0.4286 0 0 1 -1.0 952 ... intentional circular references, as follows After entering the starting values in row 13 and the formulas in row 14 as before (Figure 9-11), change the cell CHAPTER 9 SYSTEMS OF SIMULTANEOUS EQUATIONS 2 05 references in the formulas in cells B14 and C14 from references to row 13 to references to row 14 The formulas in cells 814, C14 and D14 are now, respectively, =($E$8-$C$8*CI4-$D$8*D14)/$8$8 =($E$9-$B$9*BI4-$D$9*D14)/$C$9... circle of radius r (the equation of a circle is x2 + y2 = r; thus y= ) For example, use r = 1 and h = some value between 0 and 1 The intersections will be at x , y = h and -x, y = h Make a chart to show the circle (values of x from -1 to 1 and calculated values of y , also same values of x and -y) 4 3 7 Having solved problem #8, and having created the chart, use the values of the intersections to create... table in $A $5: $B20 provides the x, y values of the function that are plotted in Figure 8-42 The adjustable parameters of the function are in $E $5: $E$7 The adjustable value of the intersection point H is in cell E10 Cell D14 contains the formula 182 EXCEL: NUMERICAL METHODS =goalseek(B5,A5,ElO) Note that the GoalSeek function does not modify the value of the changing cell (in this example cell A5) nor does... , , [PI , E &?? This chapter describes direct methods (involving the use of matrices) and indirect (iterative) methods for the solution of such systems The chapter begins 189 EXCEL: NUMERICAL METHODS 190 by describing methods for the solution of systems of linear equations, and concludes by describing a method for handling nonlinear systems of equations Cramer's Rule According to Cramer's rule, a system... function is that fargetcell must contain the complete expression dependent on changing-cell Only the instances of changing-cell that appear in the formula in targef-cell will be used in the Newton-Raphson calculation 300 250 200 > 150 100 50 0 -50 1 10 5 15 X Figure 8-42 The value of x that makes the function y = x2 + 6x - 10 have the value 210 (folder 'Chapter 08 Examples', workbook 'Goalseek Fn' sheet... AugMatrix(K, K) End If For C = K T o N + 1 AugMatrix(K, C) = AugMatrix(K, C) / NormFactor Next C 'Eliminate For R = K + 1 To N ElimFactor = AugMatrix(R, K) For C = K To N + 1 AugMatrix(R, C) = AugMatrix(R, C) - AugMatrix(K, C) * ElimFactor Next C Next R Next K 'Calculate and return the coefficients 'Selected range can be either horizontal or vertical For K = N To 1 Step -1 1 95 196 EXCEL: NUMERICAL METHODS... 'Jacobi Method') CHAPTER 9 SYSTEMS OF SIMULTANEOUS EOUATIONS 203 Cells BIO, C10 and DIO contain, respectively, the formulas =($E$4-$C$4*Cg-$D$4*Dg)/$B$4 =($E $5- $8 $5* 89-$D$S*D9)/$C $5 =($E$6-$B$6*Bg-$C$6*Cg)/$D$6 When these formulas are filled down into successive rows, as shown in Figure 9-10, the values of the variables X I , x2 and x3 converge Convergence to a suitable level is observed visually In this... 0.10 + S = K, Find the solubility S 5 Find the two sets of coordinates of the intersection of the straight line y = mx + b, where m = 5 and b = 50 , with the parabola y = ax2 + bx + c, where a = 1.1, b = -2.3 and c = -30 .5 Make a chart of the two series to show the intersections 6 Find the two sets of coordinates of the intersection of the straight line with y = h and the circle of radius r (the equation . equation.Formula End If If Left(FormulaText, 1) = "=" Then FormulaText = Mid(FormulaText, 2, 1024) FormulaText = Application.ConvertFormula(FormulaText, xlAl , xlAl , - FormulaText. element of the following matrix that gives a determinant value of zero. 0. 75 0 .5 0. 25 [ 0 .5 1 0 .51 0. 25 0 .5 0. 75 Which elements in the matrix cannot be changed in order to give a determinant. the intersection of a straight line and a curve (Figure 8-34). CHAPTER 8 ROOTS OF EQUATIONS 177 300 250 200 150 100 50 0 5 10 15 20 -50 Figure 8-34. Finding the intersection

Ngày đăng: 14/08/2014, 06:22

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan