This HTML version of the book is provided as a convenience, but some math equations are not translated correctly. The PDF version is more reliable. Chapter 2 Variables and Values2.1 A glorified calculatorAt heart, MATLAB is a glorified calculator. When you start MATLAB you will see a window entitled MATLAB that contains smaller windows entitled Current Directory, Command History, and Command Window. The Command Window runs the MATLAB interpreter, which allows you to type MATLAB commands, then executes them and prints the result. Initially, the Command Window contains a welcome message with information about the version of MATLAB you are running, followed by a chevron: >> which is the MATLAB prompt; that is, this symbol prompts you to enter a command. The simplest kind of command is a mathematical expression, which is made up of operands (like numbers, for example) and operators (like the plus sign, +). If you type an expression and then press Enter (or Return), MATLAB evaluates the expression and prints the result. >> 2 + 1 ans = 3 Just to be clear: in the example above, MATLAB printed >>; I typed 2 + 1 and then hit Enter, and MATLAB printed ans = 3. And when I say “printed,” I really mean “displayed on the screen,” which might be confusing, but it’s the way people talk. An expression can contain any number of operators and operands. You don’t have to put spaces between them; some people do and some people don’t. >> 1+2+3+4+5+6+7+8+9 ans = 45 Speaking of spaces, you might have noticed that MATLAB puts a blank line between ans = and the result. In my examples I will leave it out to save room. The other arithmetic operators are pretty much what you would expect. Subtraction is denoted by a minus sign, ; multiplication by an asterisk, *; division by a forward slash, /. >> 2*3  4/5 ans = 5.2000 Another common operator is exponentiation, which uses the >> 2^16 ans = 65536 The order of operations is what you would expect from basic algebra: exponentiation happens before multiplication and division, and multiplication and division happen before addition and subtraction. If you want to override the order of operations, you can use parentheses. >> 2 * (34) / 5 ans = 0.4000 When I added the parentheses I also changed the spacing to make the grouping of operands clearer to a human reader. This is the first of many style guidelines I will recommend for making your programs easier to read. Style doesn’t change what the program does; the MATLAB interpreter doesn’t check for style. But human readers do, and the most important human who will read your code is you. And that brings us to the First Theorem of Debugging: Readable code is debuggable code. It is worth spending time to make your code pretty; it will save you time debugging! 2.2 Math functionsMATLAB knows how to compute pretty much every math function you’ve heard of. It knows all the trigonometric functions; here’s how you use them: >> sin(1) ans = 0.8415 This command is an example of a function call. The name of the function is sin, which is the usual abbreviation for the trigonometric sine. The value in parentheses is called the argument. The trig functions sin, cos, tan—among many others—work in radians.^{1} Some functions take more than one argument, in which case they are separated by commas. For example, atan2 computes the inverse tangent, which is the angle in radians between the positive xaxis and the point with the given y and x coordinates. >> atan2(1,1) ans = 0.7854 If that bit of trigonometry isn’t familiar to you, don’t worry about it. It’s just an example of a function with multiple arguments. MATLAB also provides exponential functions, like exp, which computes e raised to the given power. So exp(1) is just e. >> exp(1) ans = 2.7183 The inverse of exp is log, which computes the logarithm base e: >> log(exp(3)) ans = 3 This example also demonstrates that function calls can be nested; that is, you can use the result from one function as an argument for another. More generally, you can use a function call as an operand in an expression. >> sqrt(sin(0.5)^2 + cos(0.5)^2) ans = 1 As you probably guessed, sqrt computes the square root. There are lots of other math functions, but this is not meant to be a reference manual. To learn about other functions, you should read the documentation. 2.3 DocumentationMATLAB comes with two forms of online documentation, help and doc. The help command works from the Command Window; just type help followed by the name of a command. >> help sin sin Sine of argument in radians. sin(X) is the sine of the elements of X. See also asin, sind. Reference page for sin Other functions named sin MATLAB functions are case sensitive, if you type the sin function using capital letters in MATLAB, you get an error: >> SIN(1) Undefined function 'SIN' for input arguments of type 'double'. Did you mean: >> sin(1) Another problem is that the help page uses vocabulary you don’t know yet. For example, “the elements of X” won’t make sense until we get to vectors and matrices a few chapters from now. The doc pages are usually better. If you type doc sin, a browser appears with more detailed information about the function, including examples of how to use it. The examples often use vectors and arrays, so they may not make sense yet, but you can get a preview of what’s coming. 2.4 VariablesOne of the features that makes MATLAB more powerful than a calculator is the ability to give a name to a value. A named value is called a variable. MATLAB comes with a few predefined variables. For example, the name pi refers to the mathematical quantity π, which is approximately >> pi ans = 3.1416 And if you do anything with complex numbers, you might find it convenient that both i and j are predefined as the square root of −1. You can use a variable name anywhere you can use a number; for example, as an operand in an expression: >> pi * 3^2 ans = 28.2743 or as an argument to a function: >> sin(pi/2) ans = 1 >> exp(i * pi) ans = 1.0000 + 0.0000i As the second example shows, many MATLAB functions work with complex numbers. This example demonstrates Euler’s Equality: e^{i π} = −1. Whenever you evaluate an expression, MATLAB assigns the result to a variable named ans. You can use ans in a subsequent calculation as shorthand for “the value of the previous expression”. >> 3^2 + 4^2 ans = 25 >> sqrt(ans) ans = 5 But keep in mind that the value of ans changes every time you evaluate an expression. 2.5 Assignment statementsYou can create your own variables, and give them values, with an assignment statement. The assignment operator is the equals sign, =. >> x = 6 * 7 x = 42 This example creates a new variable named x and assigns it the value of the expression 6 * 7. MATLAB responds with the variable name and the computed value. In every assignment statement, the left side has to be a legal variable name. The right side can be any expression, including function calls. Almost any sequence of lower and upper case letters is a legal variable name. Some punctuation is also legal, but the underscore, _, is the only commonlyused nonletter. Numbers are fine, but not at the beginning. Spaces are not allowed. Variable names are “case sensitive”, so x and X are different variables. >> fibonacci0 = 1; >> LENGTH = 10; >> first_name = 'allen' first_name = allen The first two examples demonstrate the use of the semicolon, which suppresses the output from a command. In this case MATLAB creates the variables and assigns them values, but displays nothing. The third example demonstrates that not everything in MATLAB is a number. A sequence of characters in single quotes is a string. Although i, j, and pi are predefined, you are free to reassign them. It is common to use i and j for other purposes^{2} , but it is probably not a good idea to change the value of pi! 2.6 Why variables?The most common reasons to use variables are
2.7 ErrorsIt’s early, but now would be a good time to start making errors. Whenever you learn a new feature, you should try to make as many errors as possible, as soon as possible. When you make deliberate errors, you get to see what the error messages look like. Later, when you make accidental errors, you will know what the messages mean. A common error for beginning programmers is leaving out the * for multiplication. >> area = pi r^2 area = pi r^2  Error: Unexpected MATLAB expression. The error message indicates that, after seeing the operand pi, MATLAB was “expecting” to see an operator, like *. Instead, it got a variable name, which is the “unexpected expression” indicated by the vertical line,  (which is called a “pipe”). Another common error is to leave out the parentheses around the arguments of a function. For example, in math notation, it is common to write something like sinπ, but not in MATLAB. >> sin pi Undefined function 'sin' for input arguments of type 'char'. The problem is that when you leave out the parentheses, MATLAB treats the argument as a string (rather than as an expression). In this case the sin function generates a reasonable error message, but in other cases the results can be baffling. For example, what do you think is going on here? >> abs pi ans = 112 105 There is a reason for this “feature”, but rather than get into that now, let me suggest that you should always put parentheses around arguments. This example also demonstrates the Second Theorem of Debugging: The only thing worse than getting an error message is not getting an error message. Beginning programmers hate error messages and do everything they can to make them go away. Experienced programmers know that error messages are your friend. They can be hard to understand, and even misleading, but it is worth making some effort to understand them. Here’s another common rookie error. If you were translating the following mathematical expression into MATLAB:
You might be tempted to write something like this: 1 / 2 * sqrt(pi) But that would be wrong. So very wrong. It should instead be 1 / (2 * sqrt(pi)) or 1 / 2 / sqrt(pi) 2.8 Floatingpoint arithmeticIn mathematics, there are several kinds of numbers: integer, real, rational, irrational, imaginary, complex, etc. MATLAB uses only one kind of number by default: floatingpoint. You might have noticed that MATLAB expresses values in decimal notation. So, for example, the rational number 1/3 is represented by the floatingpoint value >> 1/3 ans = 0.3333 which is only approximately correct. It’s not quite as bad as it seems; MATLAB uses more digits than it shows by default. You can change the format to see the other digits. >> format long >> 1/3 ans = 0.33333333333333 Internally, MATLAB uses the IEEE doubleprecision floatingpoint format, which provides about 15 significant digits of precision (in base 10). Leading and trailing zeros don’t count as “significant” digits, so MATLAB can represent large and small numbers with the same precision. Very large and very small values are displayed in scientific notation. >> factorial(100) ans = 9.332621544394410e+157 The e in this notation is not the transcendental number known as e; it is just an abbreviation for “exponent”. So this means that 100! is approximately 9.33 × 10^{157}. The exact solution is a 158digit integer, but we only know the first 16 digits. You can enter numbers using the same notation. >> speed_of_light = 3.0e8 speed_of_light = 300000000 Although MATLAB can handle large numbers, there is a limit. The predefined variables realmax and realmin contain the largest and smallest number magnitudes that MATLAB can handle.^{3} >> realmax ans = 1.797693134862316e+308 >> realmin ans = 2.225073858507201e308 If the result of a computation is too big, MATLAB “rounds up” to infinity. >> factorial(170) ans = 7.257415615307994e+306 >> factorial(171) ans = Inf Division by zero also returns Inf. >> 1/0 ans = Inf Some older versions of MATLAB give a warning when dividing by zero. A warning is like an error message without teeth; the computation is allowed to continue. Allowing Inf to propagate through a computation doesn’t always do what you expect, but if you are careful with how you use it, Inf can be quite useful. For operations that are truly undefined, MATLAB returns NaN, which stands for “not a number”. >> 0/0 ans = NaN 2.9 CommentsAlong with the commands that make up a program, it is useful to include comments that provide additional information about the program. The percent symbol % separates the comments from the code. >> speed_of_light = 3.0e8 % meters per second speed_of_light = 300000000 The comment runs from the percent symbol to the end of the line. In this case it specifies the units of the value. In an ideal world, MATLAB would keep track of units and propagate them through the computation, but for now that burden falls on the programmer. Comments have no effect on the execution of the program. They are there for human readers. Good comments make programs more readable, but bad comments are useless or (even worse) misleading. Avoid comments that are redundant with the code: >> x = 5 % assign the value 5 to x Good comments provide additional information that is not in the code, like units in the example above, or the meaning of a variable: >> p = 0 % position from the origin in meters >> v = 100 % velocity in meters / second >> a = 9.8 % acceleration of gravity in meters / second^2 If you use longer variable names, you might not need explanatory comments, but there is a tradeoff: longer code can become harder to read. Also, if you are translating from math that uses short variable names, it can be useful to make your program consistent with your math. 2.10 Glossary
2.11 Exercises9pt9pt9pt Exercise 1
Write a MATLAB expression that evaluates the
following math expression. You can assume that the variables
mu, sigma, and x already exist.
Note: you can’t use Greek letters in MATLAB; when translating math expressions with Greek letters, it is common to write out the name of the letter (assuming you know it).

Are you using one of our books in a class?We'd like to know about it. Please consider filling out this short survey. 