22
Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction

Embed Size (px)

Citation preview

Page 1: Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction

Chapter 1Computing Tools

Data Representation,Accuracy and Precision

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 2: Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction

Independent and Dependent Variables

• Independent Variable: Value is free to take on any assigned value

• Dependent Variable: Value is determined based on the value(s) of the independent variable(s)

• What are the independent and dependent variables here?

Engineering Computation: An Introduction Using MATLAB and Excel

Page 3: Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction

Independent and Dependent Variables (cont.)

• We usually assume that variables on the left side of the equal sign are dependent, and variables on the right side are independent

• However, we can rearrange the variables so that they switch sides:

• To determine whether the variables are dependent or independent, we must consider the problem statement

Engineering Computation: An Introduction Using MATLAB and Excel

Page 4: Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction

Independent and Dependent Variables (cont.)

• Let’s consider the equation for the height of the cannon ball:

• When we first looked at this problem, we set g, v, and θ to be constant values

• We varied time, and calculated height for each time value

• Therefore, t was the independent variable and h was the dependent variable

Engineering Computation: An Introduction Using MATLAB and Excel

Page 5: Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction

Functions

• A function is a mathematical operation that returns a single value for a given input value or set of values

• In our example, we can calculate a single value of height for each value of time that we choose

• Therefore, we say that height is a function of time• We often show this by including the independent

variable(s) in parentheses after the function name:

Engineering Computation: An Introduction Using MATLAB and Excel

Page 6: Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction

Functions (cont.)

• In our in-class exercise, we were asked to find the angle θ that maximized the distance traveled x

• To solve this problem, we considered x to be a function of time and launch angle:

Engineering Computation: An Introduction Using MATLAB and Excel

Page 7: Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction

Scalars and Arrays

• A scalar is a single value that can be represented on a numerical scale

• In our cannon example, we calculated a value of height for each value of time

• In a spreadsheet, each cell is contains a scalar value. This is an advantage of spreadsheets; the storage of results is automatic

Engineering Computation: An Introduction Using MATLAB and Excel

Page 8: Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction

Scalars and Arrays (cont.)

• In a spreadsheet, we do not need to name every variable: the cell address (row/column) defines the storage location

• In MATLAB and other programming languages, the variables must be named and storage of results must be planned

• Example: In the cannon problem, we can call time “t”. If t is a scalar, then the value of t is overwritten every time we complete a calculation loop

Engineering Computation: An Introduction Using MATLAB and Excel

Page 9: Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction

Scalars and Arrays (cont.)

• If t is a scalar, then the intermediate results will be lost, and we cannot examine or plot these results when the algorithm is complete

• However, if t is an array, then these results will be stored

• An array is a variable that has multiple values• An array’s name is followed by an index (or

indices, for multi-dimensional arrays) in parentheses

Engineering Computation: An Introduction Using MATLAB and Excel

Page 10: Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction

Indices of Arrays

• Think of the index value as an address• For example, suppose that we store values for time

at 0.1 second intervals.

• If we say that t(5) = 0.4, then we mean that the 5th value in the array of time values is equal to 0.4

• Important: Indices must be positive integers, and must start with one

Engineering Computation: An Introduction Using MATLAB and Excel

Page 11: Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction

Common Mistakes Using Indices with MATLAB

• Using an index of zero:

• Using an index not an integer: setting t(0.1) equal to a value results in the same error as above

Engineering Computation: An Introduction Using MATLAB and Excel

Page 12: Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction

Common Mistakes Using Indices with MATLAB

• Progressing indices by values other than 1. For example, if we take a temperature reading every 10 seconds, and set the first value as T(10), then zeros are filled in for the first nine values of T:

(No error is reported, but this result is probably not what is desired)

• All of these errors result from confusion between independent variables and indices. Thinking of indices as addresses helps avoid these errors

Engineering Computation: An Introduction Using MATLAB and Excel

Page 13: Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction

Multi-Dimensional Arrays

• A variable may have more than one index. A two-dimensional array is similar to the data storage of a spreadsheet. Instead of a row number and a column letter, a two-dimensional array has two index numbers

• Examples: t(1,5) R(3,3) temp(1,20)• Efficient way to store data: if both indices range

from 1-10, then the number of values stored is 10 X 10 = 100

Engineering Computation: An Introduction Using MATLAB and Excel

Page 14: Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction

Matrices

• One- and two-dimensional arrays are also called matrices

• Many mathematical operations can be performed with matrices - the name MATLAB comes from “Matrix Laboratory”

• The size of a matrix is defined by the numbers of rows and columns. Example; a 3 X 2 matrix:

Engineering Computation: An Introduction Using MATLAB and Excel

Page 15: Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction

Vectors

• One-dimensional matrices are referred to in MATLAB as “vectors”

• Vectors can be classified as “column vectors” (all values arranged in a single column) or “row vectors” (all values arranged in a single row)

• Caution: in mechanics classes, the term “vector” has a different definition: a vector is defined as a physical quantity which is defined by magnitude and direction

Engineering Computation: An Introduction Using MATLAB and Excel

Page 16: Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction

Accuracy and Precision

• Accuracy and precision are often used interchangeably, but have different definitions:

• Accuracy is a measure of how close our answer is to the actual result. In the cannon example, the assumptions made in the mathematical model affect the accuracy of the results

• Precision is a measure of the exactness of our calculations – important that we remember that our answer can be no more precise than our input values or of intermediate calculation results

Engineering Computation: An Introduction Using MATLAB and Excel

Page 17: Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction

Significant Digits

• In experimental work, where the precision of all of the input values is known, calculation rules involving the number of significant digits are followed

• For quantities with decimal points, the number of significant digits is defined as the number of digits between the first non-zero digit and the last digit.

• Examples: 1214.55 6 significant digits 1214.5513 8 significant digits 0.00012 2 significant digits 10.00012 7 significant digits

Engineering Computation: An Introduction Using MATLAB and Excel

Page 18: Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction

How Many Significant Digits?

• 1000.00108

• -77.324

• 0.0001033

• 3.44 X 108

3• 100

Can’t tell

Engineering Computation: An Introduction Using MATLAB and Excel

Page 19: Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction

Rules for Working with Significant Digits

• Addition and Subtraction: the number of digits to the right of the decimal point in the answer must be equal to the least number of digits to the right of any of the inputs.

• Examples: 6.778 + 3.5 = 10.3 10.0 – 0.0012 = 10.0

• Multiplication and Division: the number of significant digits in the answer must equal the least number of significant digits of the input values.

• Examples: 7.553 X 5.52 = 41.7 1.0 / 4.5567 = 0.90

Engineering Computation: An Introduction Using MATLAB and Excel

Page 20: Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction

Engineering Calculations

• In engineering problems, the precision of the input values is usually unknown

• Example: In the cannon problem, the precisions of the values of 35 degrees and 10 m/s are not given

• Good rule of thumb: report results to 3 significant digits (some texts recommend 4 significant digits if the first significant digit is a “1”)

• Remember that for the final answer to be precise to this number of digits, intermediate calculations should be carried out to more digits

Engineering Computation: An Introduction Using MATLAB and Excel

Page 21: Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction

Reporting Answers

• Never report your answer to a ridiculous number of digits! If would sound wrong is you said it verbally; it is equally wrong if you report it in writing

Engineering Computation: An Introduction Using MATLAB and Excel

The answer is 5.32678329546

centimeters

The answer is 5.32678329546

centimeters

FOUL! Excessive Use

of digits

FOUL! Excessive Use

of digits

Page 22: Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction

Reporting Answers

• The computer performs calculations to a large number of digits, but that doesn’t mean the answer is that precise

• How many digits do Excel and MATLAB carry through in calculations? Approximately 15 for most operations

Engineering Computation: An Introduction Using MATLAB and Excel