Chapter 2 Part 2: Excel Fundamentals: IF Statements

advertisement
Logical IF Statements, Lookup
Tables, Linear Interpolation
Chapter 2
Excel Fundamentals
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
The IF Statement
• Logical statements are used to control the
sequence of computations in programs and in
spreadsheet
• MATLAB and Excel both contain IF statements for
adding logical control
• An IF statement tests a condition, and the flow of
calculations is dependent on whether the
condition is true or false
• This is best illustrated with a simple example
Logical Test Example
• Consider taking the absolute value of a number
• How would you instruct Excel to perform this
function (without using the built-in function ABS)
• There are ways to accomplish this without an IF
statement (for example, you can square the
number and then take its square root, which
always returns the positive root), but the most
straightforward method is to use a logical test
Engineering Computation: An Introduction Using MATLAB and Excel
Logical Test Example (cont.)
• In words, you might describe your procedure as:
“If the number is less then zero, then take the negative of
that number, else leave the number as it is”
• This if-then-else logic is widely used in
programming, and can also be used in Excel
• Format of Excel IF statement:
-if(logical_test,value_if_true,value_if_false)
Engineering Computation: An Introduction Using MATLAB and Excel
Excel IF Statement
• The IF statement is a function, with three arguments
• The first argument is the logical test (the condition).
For our example, the logical test will be that the value
of the number considered is less than zero
• The second argument is the action to be taken if the
condition is true (take the negative of the number)
• The third argument is the action to be taken if the
condition is false (report the input number
unchanged)
Engineering Computation: An Introduction Using MATLAB and Excel
Absolute Value Example
• The number to be evaluated is entered into cell A1
• The formula is entered into cell B1
Engineering Computation: An Introduction Using MATLAB and Excel
Nesting IF Statements
• Often, there will be more than two possible outputs for a given
input
• In MATLAB, we use the if-then-else function
• In Excel, we must nest IF statements
• Consider this example: we want to read in a numerical grade and
convert it to a letter grade on a scale of:
– Greater than or equal to 90 = A,
– Greater than or equal to 80 and less than 90 = B
– Greater than or equal to 70 and less than 80 = C
– Greater than or equal to 60 and less than 70 = D
– Less than or equal to 60 = F
Engineering Computation: An Introduction Using MATLAB and Excel
Excel Formula
Consider this first IF statement:
If true, “A” is entered into cell B1
If false, then another IF statement is encountered
Note the use of “>=” for greater than or equal to:
If just “>” (greater than) is used, then a grade of exactly
90 would be a B.
Engineering Computation: An Introduction Using MATLAB and Excel
Examples
Engineering Computation: An Introduction Using MATLAB and Excel
Alternate Solution
Engineering Computation: An Introduction Using MATLAB and Excel
Alternate Solution #2
First
Logical
Test
Value if True
(leads to further
tests)
Value if
False
Pay close attention to parentheses!
Engineering Computation: An Introduction Using MATLAB and Excel
Combining Conditions
• The AND and OR statements can be used for
multiple conditions
• Formats:
AND(condition1, condition2…) True only if all
conditions are true
OR(condition1, condition2…) True if any one of the
conditions is true
Engineering Computation: An Introduction Using MATLAB and Excel
Example
• In a manufacturing operation, a part must have a measured
length between 1.99 and 2.02 inches, or it is rejected
• Excel formula:
• The condition for the IF statement is true only if both
conditions following the AND statement are true:
To be accepted, the part must be greater than the
minimum allowed AND less than the maximum allowed.
Engineering Computation: An Introduction Using MATLAB and Excel
Example
• Alternate solution:
• The condition for the IF statement is true only if either of
the conditions following the OR statement is true:
To be rejected, the part length must be less than the
minimum allowed OR greater than the maximum
allowed.
Engineering Computation: An Introduction Using MATLAB and Excel
Example
• Both solutions give the same results
• Could also have used nested IF statements rather
than AND/OR for this example
Engineering Computation: An Introduction Using MATLAB and Excel
Lookup Tables
• Often, we need to retrieve data that is stored in a
table
• For example, consider these metals and their
properties:
Metal
Modulus of
Elasticity, psi
Density, lb/in3
Yield Strength, psi
Aluminum 2014-T6
10,600,000
0.101
60,000
Aluminum 6061-T6
10,000,000
0.098
37,000
Stainless Steel 304
28,000,000
0.284
30,000
Structural Steel A36
29,000,000
0.284
36,000
Engineering Computation: An Introduction Using MATLAB and Excel
Lookup Table Example
• A design engineer at a certain company performs
calculations using the properties of these four
materials often
• Rather than looking up and typing in these values
every time, Excel can use a lookup table to store
these values and retrieve the correct values for the
specified material
Engineering Computation: An Introduction Using MATLAB and Excel
Lookup Table Example
• In this example, since the properties to be
retrieved are in columns, the table is called a
vertical lookup table
• If the data to be retrieved were in rows, instead,
the table would be a horizontal lookup table
• The vertical lookup table command in Excel is:
VLOOKUP(lookup value, table range, column
number, true/false)
Engineering Computation: An Introduction Using MATLAB and Excel
Vertical Lookup Table Command
VLOOKUP(lookup value, table range, column number,
true/false)
• Lookup value is the cell containing the identifier for the
data to be looked up. In our case, the cell will contain the
name of the material
• Table range is the group of cells containing the lookup
table
• Column number is the column containing the desired
property of the material
• The true/false argument is optional: true allows for an
approximate match, while false requires an exact match
Engineering Computation: An Introduction Using MATLAB and Excel
VLOOKUP Example
• Here is the data entered into Excel
• Sometimes it is convenient to enter the data table into a
separate sheet of the workbook
• Note that we have given each material a “Short Name” so that we
do not have to type in the complete name every time
Engineering Computation: An Introduction Using MATLAB and Excel
VLOOKUP EXAMPLE
• The short name is entered into cell B1. We want to
have the other information filled in automatically
Engineering Computation: An Introduction Using MATLAB and Excel
VLOOKUP EXAMPLE
• The first argument is the cell address containing
the short name of the material to be looked up
Engineering Computation: An Introduction Using MATLAB and Excel
VLOOKUP EXAMPLE
• The second argument is the cell range containing the
lookup table. The first column of the highlighted range
must contain the independent variable (the Short Name)
Engineering Computation: An Introduction Using MATLAB and Excel
VLOOKUP EXAMPLE
• The third argument contains the column number
containing the desired property. Remember that the
column containing the short name is column number 1.
Therefore, the long name is in column number 2.
Engineering Computation: An Introduction Using MATLAB and Excel
VLOOKUP EXAMPLE
• Result:
• By locking the cell
addresses, this
formula can be
copied to other
cells…
Engineering Computation: An Introduction Using MATLAB and Excel
VLOOKUP EXAMPLE
• Change only the column number to correspond to
the property desired:
Yield Strength is in
the 5th column
Engineering Computation: An Introduction Using MATLAB and Excel
VLOOKUP EXAMPLE
• Note that the names are not case-sensitive:
Engineering Computation: An Introduction Using MATLAB and Excel
VLOOKUP EXAMPLE
• In these cases, note that an inexact match still often returns a value:
Engineering Computation: An Introduction Using MATLAB and Excel
VLOOKUP EXAMPLE
• Often, we want to require an exact match
• To do this, set the optional fourth argument is added:
– TRUE = find approximate match
– FALSE = require exact match
Engineering Computation: An Introduction Using MATLAB and Excel
A Note About Lookup Tables
• Lookup tables will not interpolate values!
• When looking up a numerical value, if an exact
match is not found (and the “TRUE” option allows
for an approximate match), then the value
searched for is rounded down to the next
tabulated value.
• We will illustrate this in the following example
Engineering Computation: An Introduction Using MATLAB and Excel
Population Example
• The population data of a town
is given in the table
• We want to use a lookup table
to report the population for any
year entered
Engineering Computation: An Introduction Using MATLAB and Excel
Lookup Table Solution
• Note that using the VLOOKUP function (with the
TRUE/FALSE option left off or set to TRUE)
returns the population for the next year in the
table lower than the input value:
Engineering Computation: An Introduction Using MATLAB and Excel
Linear Interpolation
• We can use IF statements to get a better approximation
using linear interpolation
• With linear interpolation, we assume that our data varies
linearly between data points:
Engineering Computation: An Introduction Using MATLAB and Excel
Formula for Linear Interpolation
• Point x falls between x1 and x2. The y-values
corresponding to x1 and x2 are given (y1 and y2,
respectively). Find the y value corresponding to x.
y2
y
y1
x1
x
x2
Engineering Computation: An Introduction Using MATLAB and Excel
Formula for Linear Interpolation
• Similar Triangles:
y2
y2
y
y
y1
y1
x1
x
x2
x1
x
x2
Engineering Computation: An Introduction Using MATLAB and Excel
Formula for Linear Interpolation
Engineering Computation: An Introduction Using MATLAB and Excel
Population Example
y2 = 4086
y
y1 = 3012
x1 = 1950
x = x2 = 1960
1956
Engineering Computation: An Introduction Using MATLAB and Excel
Interpolation Added to Excel Table
• For each population interval, construct an IF
statement to see if the input year falls within the
interval range…
Engineering Computation: An Introduction Using MATLAB and Excel
Interpolation Added to Excel Table
• If the input year is within the interval, then the
interpolation formula should be used…
Do if condition is true
Engineering Computation: An Introduction Using MATLAB and Excel
Interpolation Added to Excel Table
• If the input year is not within the interval, then a space is
entered into the cell to make it blank.
• Lock the input cell address (A2) to allow this formula to be
copied
Engineering Computation: An Introduction Using MATLAB and Excel
Interpolation Added to Excel Table
• Copy the formula
• The only cell with a value
displayed is the one
corresponding to the
interval that contains the
input year
• Note: entering a space is the
other cells is preferable to
entering a zero, since some
tables will have both positive
and negative values.
Engineering Computation: An Introduction Using MATLAB and Excel
Interpolation Added to Excel Table
• The population is
reported by taking
the maximum value
(the only numerical
value) from the range
of calculated values
Engineering Computation: An Introduction Using MATLAB and Excel
Interpolation Added to Excel Table
• If desired, the population formula can be modified to
report an error if the input value does not fall within the
range of the table
Engineering Computation: An Introduction Using MATLAB and Excel
Check Several Values
1970 falls into 2 ranges, but both
give the same value
Engineering Computation: An Introduction Using MATLAB and Excel
Check Several Values
Engineering Computation: An Introduction Using MATLAB and Excel
Conclusions
• IF statements add a great deal of flexibility to Excel
spreadsheets – allow control over how
calculations are performed
• Lookup tables are convenient for accessing data
that is stored in table form. Important to
remember that lookup tables do not interpolate
between values
• Linear interpolation formulas can be added to
tables in order to provide a better estimate of
values between tabulated points
Engineering Computation: An Introduction Using MATLAB and Excel
Download