Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 1 Computing Tools Variables, Scalars, and Arrays Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Similar presentations


Presentation on theme: "Chapter 1 Computing Tools Variables, Scalars, and Arrays Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display."— Presentation transcript:

1 Chapter 1 Computing Tools Variables, Scalars, and Arrays Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

2 Variables To solve the projectile problem, data was needed (projectile velocity, angle, time, height, and so on) In programs, data is stored in variables In Excel, each cell is a variable that can hold data In Matlab, the variables are given names, usually a name that reminds you what is in the variable (t for time, h for height) Engineering Computation: An Introduction Using MATLAB and Excel

3 Scalars and Arrays Variables can be a scalar or an array A scalar is a single value that can be represented on a numerical scale 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

4 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

5 Scalars and Arrays (cont.) In the projectile problem, the time and height were calculated multiple times If we keep t and h in a scalar, each new calculation replaces the last one An array is a variable that has multiple values We can use arrays for data that is changing, if we want to keep all of its 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

6 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 5 th 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

7 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

8 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

9 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

10 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

11 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


Download ppt "Chapter 1 Computing Tools Variables, Scalars, and Arrays Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display."

Similar presentations


Ads by Google