1 when should we have ta help hours this weekend?? a)sun 4-6pm (superbowl starts at 4:30) b)late sun...

35
1 When should we have TA help hours this weekend?? a)Sun 4-6pm (Superbowl starts at 4:30) b)Late Sun morning (e.g., 10-noon) c)Saturday afternoon sometime

Upload: egbert-terry

Post on 03-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

1

When should we have TA help hours this weekend??

a) Sun 4-6pm (Superbowl starts at 4:30)

b) Late Sun morning (e.g., 10-noon)

c) Saturday afternoon sometime

Introduction to Engineering ComputingGEEN 1300

Lecture 73 February 2010

• Bisection• Start VBA!

Announcements

• HW3 due next Monday, in hardcopy• Liz’s office hour cancelled this Thursday• TA help hours: will post on CULearn calendar• We’ve posted the first few weeks’ worth of clicker

grades on CULearn. There are still lots of unregistered clickers! Please check your grades and (re)register your clicker if they’re not there.

Iteration:

x xn n+1

“A repetition of a statement or statements in a program”

“A problem-solving or computational method in which a succession of approximations, each building on the one preceding, is used to achieve a desired degree of accuracy.”

(both from dictionary.com)

F

Why is that useful?

• If that iteration sequence converges, we have found a solution to the equation x = cos(x)

• Which is equivalent to finding a root of the equation cos(x) – x = 0

• What our textbook calls “direct substitution” (see section 9.5 starting on pp355)

Circular references:

aka “in-cell iteration”

• Sometimes you want or need to use a cell’s value in the calculation that lives in that cell

• Or in a calculation that depends on something that uses the value in that cell (etc.)

• By default, Excel will yell at you if you try this

• But you can override that if you want

• Why might you want to do this?

• Could do that xn+1 = cos xn problem in one cell!

7

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, of course!)

R

V h

The volume of liquid in a spherical tank is related to the inside radius of the tank and the liquid depth by the equation shown above. If we know values for R and h, computing V is straightforward. Backsolving is harder (cf., Solver discussion)

2h 3R hV

3

8

2h 3R hV

3

Rearrange that equation into the following form:

3 2 3Vh 3Rh 0

or f h 0

The roots of this equation correspond to the h values that produce the desired volume!

(but watch out….there may be multiple roots…)

9

3 2 3Vh 3Rh 0

This is a cubic equation in h, which will have three roots.Let’s consider an example to investigate the natureof these roots.

We will try a radius of 10 feet (3.05m) and a liquidvolume of 500 gal (1.9m3). What will h be?

10

-150

-100

-50

0

50

100

-2 0 2 4 6 8 10 12

h

f(h

)

There are three roots:one at about –0.5mone at about 0.5manother at about 9m

feasible rangeof h for tank

Multiple roots, but only one makes sense!

-150

-100

-50

0

50

100

-2 0 2 4 6 8 10 12

h

f(h

)

feasible rangeof h for tank

onlyfeasiblesolution

12

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 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,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.

13

-120.000

-100.000

-80.000

-60.000

-40.000

-20.000

0.000

20.000

0 1 2 3 4 5 6 7

h

f(h

)

1h 2h

1f h

2f 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.

14

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

We can then evaluate f(hmid).

What does it mean if f(hmid) is negative (like f(h1) was)?

15

-120.000

-100.000

-80.000

-60.000

-40.000

-20.000

0.000

20.000

0 1 2 3 4 5 6 7

h

f(h

)

1h2h

1f h

2f h

midh

midf h

16

-120.000

-100.000

-80.000

-60.000

-40.000

-20.000

0.000

20.000

0 1 2 3 4 5 6 7

h

f(h

)

1h2h

1f h

2f h

2h

2f h

Because f(hmid) has the same sign (negative) as f(h2),hmid can become h2 the the method can be repeated.

midh

midf h

As the method is repeated overand over again, hmid will approachthe true solution.

17

Implementation of the bisection method for finding the solution of a nonlinear equation in an Excel spreadsheet

First, we create column headings and put in the initialestimates for h1 and h2:

18

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

Since this formula makes reference to the cellto the left for h, it can be copied directly for f(h2).

19

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)formula to evaluate f(hmid).

and

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 hmid will replace h2 for the next round (or iteration).

20

The general strategy :

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 in an Excel 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

If this condition is TRUE, x and y have to have the same sign.

Here’s how to implement this strategy using Excel’s IF function.

21

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.

And this implementsthe strategy whetherto continue with thecurrent h2 or replace it with hmid.

if(): see pp188-9

22

Enter a simple counter formula, =A11+1, into 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.

And this implementsthe strategy whetherto continue with thecurrent h2 or replace it with hmid.

23

Notice that, this time, hmid replaced h2 for round 2 ofthe method.

The rest of the formulas are “operational” and canjust be copied down to complete the 2nd round.

What should happen in round 3?

24

25

Terminology: • 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

.

The three current estimates, h1, h2 and hmid arequite close now. We’re approaching the solution.

26

What Excel is good for:

• Quick, simple calculations• Involving grids of data• Especially where you want automatic recalculation• Simple plots (XY scatter)• Playing nicely with word processors• Being on pretty much every computer you’ll use

27

How much programming have you done??

a) None

b) A bit, in one language

c) I’m comfortable in one language

d) I’m comfortable in more than one language

28

VBA and Excel

Excel workbook

worksheetswith cells

chart sheetswith graphs

VBA Project

moduleswith procedures

and functions

Option A

Option B

Option C

OKOption A

Option B

Option C

OKOK

Option A

Option B

Option C

OKOption A

Option B

Option C

OKOK

userformswith associated

VBA code

Excel Application

Spreadsheet“environment”

VBA “environment”

29

What is VBA?

It is the programming language embedded in Excel.

What isn’t VBA?

It is not Visual Basic. Visual Basic is a separate,software package that is used for developing stand-aloneWindows applications. It is very different from VBA.

How long has VBA been around?

Microsoft introduced VBA with Excel Version 5.0 in late1993, but only recently has it begun to be used byeveryday users.

Isn’t VBA a “toy” language when compared to C/C++and Fortran 90?

VBA is a full-featured, object-oriented language.

Where isn’t VBA? Excel 2008 for Macs <ouch>

30

What do we use VBA for?

to automate repetitive activities in Excel andstreamline the use of Excel for you or forusers of your spreadsheet projects

to expand Excel's capabilities to have it dowhat you need it to do, rather than you havingto do what it is built to do

to open windows of access to other software programs

to allow for convenient input and output ("I/O")of information to Excel spreadsheet projects

31

(Get this window by clicking “Excel Options” under the button)

33

Excel workbook

worksheetswith cells

chart sheetswith graphs

VBA Project

moduleswith procedures

and functions

Option A

Option B

Option C

OKOption A

Option B

Option C

OKOK

Option A

Option B

Option C

OKOption A

Option B

Option C

OKOK

userformswith associated

VBA code

Excel Application

34

PropertiesWindow

Project Explorer

Code Window

35

How to switch back to the Excel workbook window?

Alt-F11 shortcut switches back and forth

Alt Tab does as well

click on the Excel button on the toolbar