spreadsheet problem solvingcribme.com/cu/data/computer science/introduction to...introduction to...
TRANSCRIPT
1
GEEN 1300Introduction to Engineering Computing
Class meeting #8Monday, Sept 21st
circular calculations and the iterative solver
solving nonlinear equations live solution using bisection spherical tank problem TABLE function application
Spreadsheet Problem Solving
11
Homework #4, due WednesdaySection Test 1, next Monday, Sept 28th, 7–9pm,
Math 100 and ECCR 1B40Coverage: Class #1 through #8 today, Labs 1-5
Text, pages 1-274
pp
What is a circular calculation?
Calculation involvingone or many formulas
Answer“x”
Estimate“x”
for the solution to be correct,the estimate “x” going into thecalculation has to be equal tothe answer “x” coming out ofthe calculation
22
the calculationA simple example:
1
xsin x
estimateanswer
2
This is also called
an problem x g x
or a substitution problem
or a fixed-point iteration problem
Note: by rearranging the above to x g x 0
we get a standard equation-solving problem
f 0
33
f x x g x 0
or an f x 0 problem
and we use different methods to solve it (later!)
For our simple example,
1
xsin x
estimateanswer
Circular Calculation of x=1/sin(x)
Estimate Answer
0.5 2.086
1.1492.086
1.096
1.124
1.149
1.096
Circular Calculation of x=1/sin(x)
1.000
1.500
2.000
2.500
x v
alu
e
44
1.109
1.117
1.113
1.115
1.124
1.109
1.117
1.113
0.000
0.500
1 2 3 4 5 6 7 8
try
3
Excel-based solution using formula copying
Copy formulas down
55
Excel-based solution using the iterative solver
Office Button Excel Options Formulas
Set up Excel for iterative solving first
Default settings T i l tti
66
Default settings Typical settings
then click OK
4
Setting up the iterative calculation on the spreadsheet
Initial estimatefor x
g(x) based ong( )what’s in B2
the result of thefirst try
enter a “pointer” formula to
77
enter a “pointer” formula tocell B3 in place of the initialestimate
this “closes the loop” on the circular calculation
as soon as you press Enter,the calculation proceeds byone iteration
and “Calculate” is shown onthe Status bar to indicate thatthe Status bar to indicate thatyou are in an iterative situation
Note: the spreadsheet willnot update automaticallynow you have toforce it to re-calculate with
Pressing the F9 key afew times will causethe calculation toconverge to the correct
( i t l )
88
the F9 [ Calculate ] keyanswer (approximately)
since “estimate” x agreeswith “answer” x, the equationis solved, numerically
5
There are (at least) a couple issues here:
1) if you want to reset the calculation to the startingestimate (0 5) and run through it again you haveestimate (0.5) and run through it again, you haveto replace the pointer formula in B2 with 0.5, andthen put the pointer formula back in -- not convenient!
2) this type of calculation will not always convergein fact, it might just as likely blow up on you –
h t t d if thi h ?
99
what to do if this happens?
Issue 1: How to reset the calculation
Enter labels “Reset” in C1and “Initial” in D1.
Transfer these labels toEnter “true” in C2
Transfer these labels tocells C2 and D2 as names[Formulas Create from Selection Top Row]
and 0.5 in D2
Instead of the pointer formula in B2, enter the formulashown below that uses Excel’s IF function.
1010
6
Excel’s IF function:
IF(logical expression, true alternative, false alternative)
a logical expression evaluates to a true/false result
examples: A1>2
AND(A3=4,A1<=2)
TRUE
FALSE
These are Excel’slogical constants
relational expression
logicalexpression
1111
FALSE
IF(Reset,Initial,B3)
If the value in Reset is TRUE, the value in cell Initial isthe result, otherwise, the value in cell B3 is the result
When Reset is TRUE,
and then when Reset is set to FALSE,
and then after pressing F9 a few times
1212
and then, after pressing F9 a few times,
we have the solution again.
7
Automating the iterative solution
Experimentation with F9 shows that this calculationconverges in about 10 iterations.
Now, if you reset the calculation and enter FALSE again,the solution should appear without having to press F9.
13
Showing the Developer tab
Office Button Excel Options Popular
14This will be necessary for VBA work in the next segment of the course.
8
Automating the reset process
Instead of typing TRUE and FALSE repeatedly, we canautomate this with a checkbox directly on the spreadsheet.
click onceclick onceon thecheckboxthen dragout arectangleon the
15
on theworksheet
Edit the title
Automating the reset process
Right-click the boundaryof the checkbox andclick on Format Control...
Type Reset into theCell Link field
16
Cell Link field
and click OK.Click away from the checkboxfield, then
you can control the value in the Reset cell by clickingthe Initialize checkbox on and off.
9
Issue 2: What to do if the calculation blows up
For example, with our original equation,
1
xsin x
solve for the x in the sin(x) term,
1 1 1x sin
x
This is an alternate form of the same equation. x g x
Try the formula copy technique on this form.
1717
Note: initial estimateof 0.5 won’t work, but1.1 (near the solution)will
Replace the initial estimate of 1.1 with the pointer formula,=B3,
P F9 th ti dPress F9 three times, and . . .
the calculation blows up!
So, when you are setting up a circular calculation, you
1818
always have to rearrange your equation in theform. You do this by solving for one of the x’s in theequation. If your formulation blows up, you can usuallyfind one that will converge by solving for one of the otherx’s in the equation. In other words, we were just luckythat our first formulation converged to the right answer!
x g x
10
What’s behind this convergence and blow-up business?A typical graph of g x vs x will reveal the problem.
the initial estimate, x1,is plugged into g(x) toyield g(x1)
Note: the solution is where the g(x)curve intersects the 45-degree line,shown as a dashed line
g(x)
1g x 2g x g(x1) becomes x2,
the next estimate
the pattern repeats:x2 is plugged into g(x)to yield g(x2), whichbecomes x
1919
x1x 2x 3x becomes x3
evidently, the circular calculation is proceeding towardthe solution, indicated by the circle
This is a similar scenario, but with a different g(x) curve.
Notice that the processis diverging away fromthe solution – in otherwords, it’s blowing up!
g(x)
1x
1g x
2x3x
2g x
Now, observe what’sdifferent between thetwo examples.
The slope of the g(x)line in the first exampleis less than the slope
2020
x is less than the slopeof the 45-degree line,less than 1.The slope in this example is > 1
The condition required for the solution to converge isthat the absolute value of the slope (or derivative) ofg(x) at or near the solution is less than one.
11
Now let’s apply this to a more complicated problem.
Steam in equilibrium with water
Equation of state – the ideal gas law
abs
mPV RT
MW
P pressure, Pa
V volume, m3
m mass, kg
MW molecular wt, kg/kgmolMW
Vapor-liquid equilibrium – the Antoine equation
10
Blog P A
T C
Temperature scale conversion
R gas law constant
absT absolute temperature,Kelvins
T temperature, degC
2121
absT T 273.15
Problem to be solved: m, V known, find absP, T , T
known parameters: R, MW , A, B, C
A,B,C Antoine equationparameters
Strategy for solution
estimate T absT T 273.15
RTabsT
absRTmP
MW V
P
10
BT C
A log P
answer
2222
T
circular arrangement
12
Parameter values3Pa m
R 8314kgmol K
kgMW 18.02
kgmol
m 3 755 kg 3V 3 142 m
A 12.559 B 2354.7 C 280.709
m 3.755 kg V 3.142 m
2323
Initial estimate T 0 C
Solution using formula copying
convergesconvergesquickly!
2424
13
Using the iterative solver
Set up the calculation, starting with an estimate of 0 degC
Make sure that the Iterative Solver is set up properly
2525
Enter a pointer formula to Tans in the Test cell instead ofthe initial estimate
First iteration And, just like before, we canadd an initialization switch
Press F9 three times
2626
Converged!
14
And what about another approach to the solution?
estimate P abs
P MW VT
m R
absT
absT T 273.15
abs
TB
AT CP 10
answer
2727
Panswer
Looks plausible
Try it!
ka-boom!This formulation is unstable andwill not converge to the answer.
That’s the story on solving iterative problems using Excel
2828
That’s the story on solving iterative problems using Excel.
15
Solving single nonlinear equations in one unknown
precautions:
there may be multiple solutionsthere may be no solutionthere may be one solution that’s hard to findthere may be one solution that s hard to find
Simple example 1
xsin x
or x sin x 1 0
x g x form f x 0 form
2929
When possible, it’s useful to observe the behavior of f(x)graphically.
0 00
0.50
1.00
For our example, two solutions evidentfor x between 0 and 4
2 00
-1.50
-1.00
-0.50
0.00
f(x)
3030
-3.00
-2.50
-2.00
0 0.5 1 1.5 2 2.5 3 3.5 4
xNote: a solution is wheref(x) = 0
16
For most engineering problems, multiple solutions are justa nuisance, since only one of the solutions will makephysical sense. That may not be the case in abstractmath problems.
So, we introduce a real problem:
R
V h
Vh R h
2 3
3
b g
3131
This diagram describes liquid stored in a spherical tank.Spherical tanks are commonly used. The volume ofliquid in the tank is related to the inside radius of the tankand the liquid depth by the equation shown above. If weknow values for R and h, computing V is straight-forward.
On the diagram, you will notice a level gauge on the sideof the tank. This “sight gauge” allows one to measure h.Then, the procedure would be to use the formula tocompute V. This is tedious, and may be difficult for
ti l th t d ’t h t t h i loperating personnel that don’t have strong technicaltraining. One could then have a graph of V versus hthat could be read to determine V. This is commonlydone. A better solution would be to put a scale directlyon the sight gauge that is calibrated in volume.
To make such a scale, we would decide on a set of
3232
volume “tick marks” for the scale and need to knowwhere to place them. In other words, we would knowa value of volume, V, and need to find h to mark thetick.
17
Developing a calibration scale for a spherical tank
2h 3R hV
3
The fundamental equation can be rearranged tothe following f(x)=0 form:
3 2 3Vh 3Rh 0
or f h 0
This is a cubic equation, which will have three roots.Let’s consider an example to investigate the naturef th t
3333
of these roots.
We will try a radius of 10 feet (3.05m) and a liquidvolume of 500 gal (1.9m3).
50
100
There are three roots:one at about –0.5mone at about 0.5manother at about 9m
-50
0
f(h
)
another at about 9m
onlyfeasiblesolution
3434
-150
-100
-2 0 2 4 6 8 10 12
h
feasible rangeof h for tank
Multiple roots, but only one makes sense!
18
So, if we confine ourselves to the feasible range for h[from tank empty (h=0) to tank full (h=2R)]there will be only one root.
Let’s say we start by making two estimates for h thatare certain to enclose or bracket the true solutionare certain to enclose or bracket the true solution.
These would be the ones above: h=0 and h=2R
Then we could evaluate f(h) for each of these estimates.
Editorial note: Since we have the graph of f(h) vs h,
3535
we can do a much better job of estimating the solutionthan these two estimates. But we usually don’t havea graph for every solution, so the graph here is justused as a way to illustrate the method.
-20.000
0.000
20.000
1f h
Notice that f(h1) and f(h2) areof opposite sign. That’s important.It means that there is a zerocross-over in f(h) between h1 andh2 – a solution to the equation.
-80.000
-60.000
-40.000
f(h
)
h2 a solution to the equation.
3636
-120.000
-100.000
0 1 2 3 4 5 6 7
h
1h 2h
2f h
19
Now, we would like a better estimate for the solution, basedon our first two. A simple method (perhaps the simplest) isto use the midpoint between h1 and h2.
1 2mid
h hh
2
mid 2
We can then evaluate f(hmid) and see where it’s locatedon the graph.
3737
-20.000
0.000
20.000
1f h
-80.000
-60.000
-40.000
f(h
)
midf h
3838
-120.000
-100.000
0 1 2 3 4 5 6 7
h
1h2h
2f h
midh
20
-20.000
0.000
20.000
1f h
Because f(hmid) has the same sign (negative) as f(h2),hmid can become h2 the method can be repeated.
midf h
As the method is repeated overand over again, hmid will approachthe true solution.
-80.000
-60.000
-40.000
f(h
)
2f h
3939
-120.000
-100.000
0 1 2 3 4 5 6 7
h
1h2h
2f h
2hmidh
This is the basis for the bisection method for finding thesolution of a nonlinear equation. It is a simple, yet effectivemethod. Now, let’s implement it with an Excel spreadsheet.
First, we create column headings and put in the initialestimates for h1 and h2:1 2
4040
21
Next, we need to construct a formula for f(h) that makesreference to the cell to the left for the value of h. We dothat for f(h1) first. 3 2 3V
f h h 3Rh 0
4141
Since this formula makes reference to the cellto the left for h, it can be copied directly for f(h2).
Notice that the signs of f(h1) and f(h2) are opposite.That’s good. It means that there’s a solution in between.
Now we enter a formula for the midpoint and copy the f(h)Now, we enter a formula for the midpoint and copy the f(h)formula to evaluate f(hmid).
and
4242
Note that the sign of f(hmid) is negative, as is the sign of f(h2).We could have predicted that from our graph. This meansthat hmin will replace h2 for the next round (or iteration).
22
But, in general, hmid will not always replace h2. Considerthe following scenarios:
0
5
10(x
)
f(x1)<0f(x2)>0
-10
-5
0
-1 1 3 5
x
f(
x2x1 xmid
( 2)f(xmid)<0xmid replaces x1
5
10
f(x1)<0
4343
-10
-5
0
5
-1 1 3 5
x
f(x
)
x2x1 xmid
f(x1)<0f(x2)>0f(xmid)>0xmid replaces x2
and these too:
0
5
10
f(x
)
f(x1)>0f(x2)<0f(x )>0
-10
-5
-1 1 3 5
x
x2x1 xmid
f(xmid)>0xmid replaces x1
5
10
f(x1)>0
4444
-10
-5
0
-1 1 3 5
x
f(x
)
x2x1 xmid
f(x1) 0f(x2)<0f(xmid)<0xmid replaces x2
23
The strategy that emerges from these examples is:
hmid should replace h1 if f(hmid) is the same sign as f(h1)otherwisehmid should replace h2 if f(hmid) is the same sign as f(h2)
To implement this on the spreadsheet, we have to developa way to check whether the signs of two quantities, x and y,are the same, or different.
The easiest way to do this is to check the conditionx y 0
4545
If this condition is TRUE, x and y have to have the same sign.
This strategy is now implemented in the second row of thespreadsheet table using Excel’s IF function.
Note: a simple counter formula, =A11+1, has been enteredinto cell A12.
1 midf h f h 0 Select hmid if TRUE and h1 if FALSE
This implements the strategy whether to continue with thecurrent h1 or replace it with hmid.
4646And this implements the strategy whether to continue with thecurrent h2 or replace it with hmid.
24
Notice that, this time, hmid replaced h2 for round 2 ofthe methodthe method.
The rest of the formulas are “operational” and canjust be copied down to complete the 2nd round.
4747
just be copied down to complete the 2 round.
What should happen in round 3?
We call the first row of the method the initialization row.It gets things off the ground.
We call the second row of the method the operational row.It can be copied downward to extend the method many rounds.
4848The three current estimates, h1, h2 and hmid arequite close now. We’re approaching the solution.
25
After 20 rounds, things look like this:
The three estimates differ in the 5th significant figure,meaning that we have found the solution to better than1 part in 10 000 That’s close enough for engineering work!1 part in 10,000. That s close enough for engineering work!
This technique is called a “live solution” method becauseit presents the solution to the equation without having torun anything in Excel. To illustrate this, if we change theliquid volume from 500 gal (1.89 m3) to 1000 gal (3.79 m3),the new solution appears instantly as 0.6525 m:
4949
To make things more convenient to see, we put a pointerformula in cell B7 to bring up the value from cell F30 intoview.
pointer formulafor solution toequation
We’ve created a live solution of the equation, f(h)=0,and that solution is dependent on the value of theliquid volume that we specify in cells B5 and B6.
5050
We can now view this as a computing “engine” thatproduces a value of h, given a value of V:
V hBisection solutionof f(h) = 0
26
We can now use this computing engine to solve the f(h)equation for a range of liquid volumes, employing Excel’sTABLE function. First, we create column headings
and a column of liquid volume
The TABLE function will put thesevolume values, one at a time, in
ll B5 th i t ll f l
and a column of liquid volumevalues from 100 to 31300 (justbefore the tank is full). UseHome Fill Series.
51
cell B5, the input cell for volumein gallons in the upper left-handcorner of the spreadsheet.
Then, we need a pointer formula in cell K2 to get thedepth in feet. That is =B8.
52
27
Then, we select the range of cells req’d for the TABLEfunction:
and, from the menu,
Data Data TableWhat-If Analysis
5353
Note: the column input cell field is usedbecause the volume values are in acolumn
Click OK!
and we now have the solution to the f(h) equation –more than 300 times!
We can make a calibration plot ofthese values:
20.00
Spherical Tank Calibration
6.00
8.00
10.00
12.00
14.00
16.00
18.00
Liquid Depth ‐feet
Series1
5454
and we can add a couple columns to the rightto display feet and inches, for ease of measurement.
0.00
2.00
4.00
0 5000 10000 15000 20000 25000 30000 35000
Volume ‐ gallons
28
Formula for integral feet:
Formula for remainderin inches:
5555
and (finally!) we’re readyto mark our calibrationtape in even incrementsof liquid volume.
Some final comments on the bisection live solution method:
1) By replacing the initial estimates of h1 and h2 with someother x1 and x2, and by replacing all the f(h) formulaswith some other f(x) formula, we can use the samespreadsheet to solve another equation.
To get the appropriate accuracy in the solution, we mighthave to adjust the number of iterations (how far the tableis copied down) for another equation. [Later, we’ll showhow this can be automated using VBA.]
5656
[Obviously, we would have to change the basic datain the upper left-hand corner of the spreadsheet toreflect the new equation being solved.]
29
2) The bisection method is one of the simplest methodsfor solving nonlinear equations. The scheme we havedeveloped can be adapted to more sophisticatednumerical methods, such as false position (regula falsi ), and Newton’s method (which uses derivatives)
Excel also has built-in tools that allow for morestreamlined solution of equations.
( )Although bisection is simple, and converges slowly, itis stable and reliable and suitable for mostengineering problems.
5757
These are Goal Seek and the Solver, and wewill introduce them in the next class.
They have advantages, and disadvantages, whencompared to the live solution method introduced here.
It will be advantageous to know all the approaches.