1. Trang chủ
  2. » Công Nghệ Thông Tin

Excel for Scientists and Engineers phần 5 pdf

48 366 0

Đ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

Thông tin cơ bản

Tiêu đề Excel: Numerical Methods
Trường học Standard University
Chuyên ngành Numerical Methods
Thể loại Tài liệu
Định dạng
Số trang 48
Dung lượng 3,25 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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 contai

Trang 1

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 p l 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

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

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

Trang 2

End If

Reffext = reference.Address

'PARSE THE FORMULA INTO TERMS

'pointers: p l , 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) -

'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)

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)

'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

Trang 3

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

'SORT ROOTS IN ASCENDING ORDER

Dim I As Integer, J As Integer

M = M - 2

Trang 4

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

Figure 8-29 A regular polynomial with one real root and two imaginary roots

(folder 'Chapter 08 Examples', workbook 'Bairstow', sheet 'Example')

Trang 5

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')

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

Trang 6

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

Figure 8-32 Using Goal Seek t o find the intersection of two lines

(folder 'Chapter 08 Examples', workbook 'Intersecting Lines', sheet 'Two Straight Lines')

Trang 7

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)

Trang 8

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

Trang 9

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

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

Trang 10

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

Trang 11

and the two cells will display zero values Now choose Options from the

Tools menu and choose the Calculation tab Check the Iteration box and press

OK Figure 8-39 shows the final values in the table, after circular reference iteration is complete

Figure 8-39 Calculating two intersections of a line and a curve

by the Newton-Raphson method (after creating intentional circular references)

(folder 'Chapter 08 Examples', workbook 'Intersecting Lines', sheet 'Using Circular Reference (2)')

The Newton-Raphson custom function described in a previous section was

modified to create a custom function that performs goal seeking This custom

function can be used in the same way as Excel's built-in Goal Seek tool - to find the value of x (the changing cell) that makes the function y (the target cell)

have a specified value The VBA code is shown in Figure 8-40

Option Explicit

Function GoalSeek(target-cell, changing-cell, objective-value, Optional -

initial-value) As Double

'Finds value of X to make Y have a desired value

'This is a modified version of NewtRaph

Dim tolerance As Double, incr As Double

Dim XRef As String, Formulastring As String

Dim I As Integer

Dim X I As Double, Y1 As Double, X2 As Double, Y2 As Double

Dim m As Double

If IsMissing(initia1-value) Then initial-value = changing-cell

If initial-value = I"' Then initial-value = changing-cell

Trang 12

'Exit here if a root is found

If Abs((X2 - X I ) I X2) c tolerance Then GoalSeek = X2: Exit Function

X I = x 2

Next I

'Exit here with error value if no root found

GoalSeek = CVErr(x1ErrNA): Exit Function

End Function

End Sub

Figure 8-40 VBA code for the GoalSeek custom function

(folder 'Chapter 08 Examples', workbook 'GoalSeek Fn', module 'Module 1') This custom function can be used in the same way as Excel's built-in Goal Seek tool to find the value of x (the changing cell) that makes the function y

(the target cell) have a specified value

The syntax of the function is

GoaISeek(target-cel/, changing-cell, objective-value, initial_value)

The argument targetcell is a reference to a cell containing a formula F(x)

The argument changing-cell is a cell reference corresponding to x, the independent variable (The formula in fargef-cell must depend on

changing-cell.) These two arguments correspond exactly to the Goal Seek tool's inputs Set Cell and By Changing Cell The argument objective-value (Goal Seek's To Value input) is the value you want fargef-cell to attain The optional argument inifial_value is used, in cases where more that one value of x can result

in the function F(x) having the desired value, to control the value of x that is returned

Note that when using the Goal Seek tool, To Value can only be a fixed

value, not a cell reference, whereas when using the GoalSeek custom function, the argument can be a cell reference Thus, when objecfive-value is changed, the

GoalSeek return value updates automatically

As an illustration, we will use the GoalSeek custom function to find the value of x that makes the function y = x2 + 6x -10 have a specified value, namely

y = 210 In the spreadsheet shown in Figure 8-41 the 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

Trang 13

=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 it result in a change in the cell containing the function (in this example cell 85) These values are merely copied and used

as inputs for the VBA code The final value of the changing cell is returned by

the GoalSeek function (in this example in cell D14) As a check, the target cell

formula was entered in cell E l 4 so as to calculate F(x) using the value of x

returned by Goalseek

Some functions have more than one value of x that can satisfy the

relationship F(x) = objective-value; in these cases the user must use the optional argument initial_va/ue to control the value of x that is returned

Figure 8-41 Using the GoalSeek custom function to find the value ofx

that makes the function y = x2 + 6x - 10 have a specified value (here, y = 2 10)

(folder 'Chapter 08 Examples', workbook 'Goalseek Fn', sheet 'Intersection of line with h (2)')

If you change the values of aa, bb, cc, or H, the function value will update to

find the new intersection value In contrast, if you use the Goal Seek tool, you

Trang 14

must repeat the action of goal-seeking each time you change any of the parameters

A limitation of the GoalSeek custom 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

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 'Intersection of line with h (2)') The CD contains an example of the use of the GoalSeek function to find approximately 180 intersection points of lines with a curve in a chart (see folder 'Chapter 08 Examples', workbook 'Diatomic Molecule', sheet 'Vibrational Energy

Levels') The resulting chart is shown in Figure 8-43 The chart contains two

data series The first data series shows the continuous function of energy as a

function of distance r The second data series shows the approximately 90

horizontal vibrational energy levels

Trang 15

Figure 8-43 Using the GoalSeek custom function

to find multiple intersections of lines in a chart

(folder 'Chapter 08 Examples', workbook 'Diatomic Molecule', sheet 'Sheetl')

8

Trang 16

Problems

Answers to the following problems are found in the folder "Ch 08 (Roots of

Equations)" in the "Problems & Solutions" folder on the CD

1 A circuit consisting of a source, a resistor and a load, has a current i that

oscillates as a function of time t according to the following:

i = 2.5 sin(-)e-2.5' + 2.5 sin(2.5t - -)

Find the first time after t = 0 when the current reaches zero.)

2 In pipe flow problems the relationship

is encountered Solve for D, if a = 700, b = -2.9, c = -300

aD3 + bD + c = 0

3 When the sparingly soluble salt BaC03 is dissolved in water, the following

simultaneous equilibria apply:

BaC03 e Ba2++ C03'- Ksp = [Ba2'][C032-] = 5.1 x lo-' C0:- + H 2 0 + HCO< + OH- Kb = [OH-][ HCO<]/[ C032-] = 2.1 x lo4 Employing mass- and charge-balance equations, the following relationship can be obtained for a saturated solution of BaC03 in water:

[Ba2+I2 - JKbKsp [Ba2+]1'2 - Ksp = 0 Find the concentration of free Ba2' in the saturated solution

4 A solution of 0.10 M nitric acid (HNO3) is saturated with silver acetate

(AgAc), a sparingly soluble salt The system of mass- and charge-balance equations describing the system is

Trang 17

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

Find the two sets of coordinates of the intersection of the straight line with y

= h and the circle of radius r (the equation of a circle is x2 + y2 = r; thus

y = 4 3 ) 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)

Having solved problem #8, and having created the chart, use the values of the intersections to create a chart series that shows the circumscribed rectangle (four sets of coordinates: x, y = h; -x, y = h; x, y = -h; -x, y = -h) Use any suitable method to find the coordinates of the circumscribed square

For the chemical reaction

2 A = B + 2 C

the equilibrium constant expression is

For this reaction, the value of the equilibrium constant K at a certain temperature is 0.288 mol L-I

A reaction mixture is prepared in which the initial concentrations are [A] = 1,

[B] = 0, [C] = 0 mol L-I From mass balance and stoichiometry, the concentrations at equilibrium are [A] = 1 - 2x, [B] = x, [C] = 2x mol L-',

from which the expression for K is 4x3 Find the value of x that

1 - 4~ - 4x2

Trang 18

makes the expression have a value of 0.288, and calculate the concentrations

of A, B and C at equilibrium

9 For the gas-phase chemical reaction

the equilibrium constant expression for reaction is

fluid For flow in pipes, a Reynolds number of less than 2000 indicates that

the flow is laminar, while a value of greater than 10,000 indicates that the flow is turbulent For a pipe diameter of 5 cm, and fluid of density 1 g/cm3 and viscosity of 1 centipoise, find the minimum velocity that results in turbulent flow

1 1 Find the value of the (1,l) element of the following matrix that gives a determinant value of zero

Trang 19

X 5 - 1oX4+30X3-20X2-31X+30

13 Use the Bairstow custom function to find all of the roots of the polynomial

16200000~~ - 64800000~~ + 97 1 9 9 9 9 6 ~ ~ - 64800000~ + 16200000

Trang 20

Systems of

Simultaneous Equations

Sometimes a scientific or engineering problem can be represented by a set of

n linear equations in n unknowns, for example

x + 2 y = 15

3 x + 8y= 57

or, in the general case

allxl + a12x2 + a13x3 + “ ’ + al&,x,, = c1

~~21x1 + ~22x2 + ~23x3 + + ~ 2 , & , , = ~2

a17lX1 + a m + a , 4 3 + * * + a,,,&,, = c, where xl, x2, x3, , x , are the experimental unknowns, c is the experimentally measured quantity, and the aii are known coefficients The equations must be linearly independent; in other words, no equation is simply a multiple of another equation, or the sum of other equations

A familiar example is the spectrophotometric determination of the

concentrations of a mixture of n components by absorbance measurements at n

different wavelengths The coefficients ay are the E, the molar absorptivities of the components at different wavelengths (for simplicity, the cell path length, usually 1.00 cm, has been omitted from these equations) For example, for a mixture of three species P, Q and R, where absorbance measurements are made

at hl,h2 and h3, the equations are

E 1, [PI + E?, [QI + E:, [RI =An,

E:~ [PI + E:, [QI + &f2 [RI =A,,

&I, [PI + &?? [QI + ~ n ” , [RI =A,,

This chapter describes direct methods (involving the use of matrices) and indirect (iterative) methods for the solution of such systems The chapter begins

189

Trang 21

in the determinant corresponding to the desired unknown with the column of constants

Thus, for example, for the set of equations

Figure 9-1 Spreadsheet data for three equations in three unknowns

(folder 'Chapter 09 Simultaneous Equations', workbook 'Simult Eqns 1', sheet 'Cramer's Rule')

Figure 9-2 The determinant for obtaining x

(folder 'Chapter 09 Simultaneous Equations', workbook 'Simult Eqns I', sheet 'Cramer's Rule')

Trang 22

The x values that comprise the solution of the set of equations can be calculated in the following manner: xk is given by a quotient in which the denominator is D and the numerator is obtained from D by replacing the gh

column of coefficients by the constants c,, cz The unknowns are obtained readily by copying the coefficients and constants to appropriate columns in another location in the sheet For example, to obtain x, the determinant is shown

in Figure 9-2, and x = 2 is obtained from the formula

=MDETERM(A8:CI O)/MDETERM(A2:C4)

y = -1 and z = 3 are obtained from appropriate forms of the same formula

only a few equations Cramer's method is very inefficient and should be used only for systems of

Solving Simultaneous Equations

by Matrix Inversion

Simultaneous equations can be represented in matrix notation by

where A is the matrix of coefficients, B the matrix of unknowns, and C the

matrix of constants Multiplying both sides of equation 9-1 by A-' yields

In other words, the solution matrix is obtained by multiplying the matrix of constants by the inverse matrix of the coefficients To return the solution values shown in Figure 9-3, the array formula

{=MMULT(MINVERSE(A2:C4),D2:D4)}

was entered in cells E2:E4

Figure 9-3 Solving a set of simultaneous equations by means of matrix methods

(folder 'Chapter 09 Simultaneous Equations', workbook 'Simult Eqns 1', sheet 'Matrix Inversion')

Solving Simultaneous Equations

by Gaussian Elimination

A system of linear equations such as

x + 2 y = 15

3 x + S y = 5 7

Trang 23

can be solved by successive substitution and elimination of variables For example, you can multiply the first equation by 3, so that the coefficient of x is the same as in the second equation, and then subtract it from the second equation, thus

3 x + S y = 5 7 -3x + 6 ~ 4 5

2 y = 12

to produce a single equation in one unknown from which y = 6 Using the value

of y, you can now calculate x

To extend this procedure to a system of n equations in n unknowns requires that one work in a systematic fashion The solution process is equivalent to converting the n x n matrix above into a triangular matrix, such as the upper triangular matrix

of linear equations to an upper triangular matrix In the example at the beginning

of this chapter, we used the first equation to eliminate x1 from the other equation

To eliminate x1 in a system of n equations:

allxl + a12x2 f 013x3 + + al$n = bl

termed the pivot equation, and the coefficient of x1 the pivot

We then use equation 2 as the pivot equation, the coefficient of x2 as the

pivot, and eliminate x2 from equations 3 , , n

Trang 24

If the pivot equation is normalized by dividing it by the coefficient of xJ, the

It will be instructive to show the progress of the calculations with a simple coefficient of x, is 1 and the calculations are simplified somewhat

example, such as the following:

The Gaussian elimination method operates on an n x n matrix of coefficients, augmented by the vector of constants In our example this matrix will be a 4 x 5

matrix, as shown:

First, row 1 is normalized:

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:

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

TỪ KHÓA LIÊN QUAN