Skip to main content
Engineering LibreTexts

1.2: A Glorified Calculator

  • Page ID
    85847
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    MATLAB is a programming language with features that support modeling and simulation. It has a lot of features, so it can seem overwhelming, but at heart MATLAB is a glorified calculator. When you start MATLAB you should see a window entitled MATLAB that contains smaller windows entitled Current Folder, Command Window, and Workspace. In Octave, Current Folder is called File Browser.

    The Interpreter

    The Command Window runs the interpreter, which allows you to enter commands; once entered, the interpreter executes the command and prints the result. Initially, the Command Window contains a welcome message with information about the version of the software you’re running, followed by a prompt, which looks like this:

    >>

    The >> symbol prompts you to enter a command. The simplest kind of command is a mathematical expression, like 2 + 1. If you type an expression and then press (or ), MATLAB evaluates the expression and prints the result.

    >> 2 + 1
    ans = 3

    Just to be clear: in this example, MATLAB displayed >>; I typed 2 + 1 and then hit , and MATLAB displayed ans = 3.

    In this expression, the plus sign is an operator and the numbers 2 and 1 are operands. 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. Here’s an example with no spaces:

    >> 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’ll 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 is designated by an asterisk (*), division is denoted by a forward slash (/).

    >> 2*3 - 4/5
    ans = 5.2000

    Another common operator is exponentiation, which uses the ^ symbol, sometimes called “caret” or “hat.” So, 2 raised to the 16th power is

    >> 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 * (3-4) / 5
    ans = -0.4000

    When I added the parentheses, I removed some spaces 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’s worth spending time to make your code pretty; it will save you time !

    Math Functions

    MATLAB knows how to compute pretty much every math function you’ve heard of. For example, 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, and tan—among many others—work in radians, so the argument in the example is interpreted as 1 radian. MATLAB also provides trig functions that work in degrees: sind, cosd, and tand.

    Some functions take more than one argument, in which case the arguments are separated by commas. For example, atan2 computes the inverse tangent, which is the angle in radians between the positive x-axis and the point with the given x- and y-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 .

    >> 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 isn’t meant to be a reference manual. To learn about other functions, you should read the documentation.


    This page titled 1.2: A Glorified Calculator is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Allen B. Downey (Green Tea Press) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.