Skip to content Skip to navigation

Connexions

You are here: Home » Content » Basic Mathematical Operations

Navigation

Content Actions

  • Download module PDF
  • Add to ...
    Add the module to:
    • My Favorites
    • A lens
    • An external social bookmarking service
    • My Favorites (What is 'My Favorites'?)
      'My Favorites' is a special kind of lens which you can use to bookmark modules and collections directly in Connexions. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need a Connexions account to use 'My Favorites'.
    • A lens (What is a lens?)

      Definition of a lens

      Lenses

      A lens is a custom view of Connexions content. You can think of it as a fancy kind of list that will let you see Connexions through the eyes of organizations and people you trust.

      What is in a lens?

      Lens makers point to Connexions materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

      Who can create a lens?

      Any individual Connexions member, a community, or a respected organization.

    • External bookmarks
  • E-mail the authors

Recently Viewed

This feature requires Javascript to be enabled.

Basic Mathematical Operations

Module by: Anders Gjendemsjø, Darryl Morrell Based on: Using MATLAB by Anders Gjendemsjø

Summary: This module covers basic mathematical operations in an m-file environment.

Operations and Expressions

An m-file environment has all of the standard arithmetic operations (addition, subtraction, etc.) and functions (sine, cosine, logarithm, etc.). The table lists the most commonly used operations; in this table, x and y are scalars. (A scalar is a single value, as opposed to a vector or matrix which consists of many values.)

Some Common Scalar Mathematical Operations
Operation m-file
x-yxy x-y
x+yxy x+y
xyxy x*y
xyxy x/y
xyxy x^y
exex exp(x)
log10xlog10x log10(x)
lnxlnx log(x)
log2xlog2x log2(x)
cosxx cos(x)
sinxx sin(x)
xx sqrt(x)

Expressions are formed from numbers, variables, and these operations. The operations have different precedences. The ^ operation has the highest precedence; ^ operations are evaluated before any other operations. Multiplication and division have the next highest precedence, and addition and subtraction have the lowest precedence. Precedence is altered by parentheses; expressions within parentheses are evaluated before expressions outside parentheses.

Example 1

The Table below shows several mathematical formulas, the corresponding expressions, and the values that are computed for the expressions.

Example Expressions
formula MATLAB Expression Computed Value
52+42 52 42 5^2+4^2 41
5+42 54 2 (5+4)^2 81
2+34-5 23 45 (2 + 3)/(4 - 5) -5
log10100 log10 100 log10(100) 2
ln42+3 ln 4 23 log(4*(2+3)) 2.9957

Useful Tricks

These tricks are occasionally useful, especially when you begin programming with m-files.

  • A semicolon added at the end of a line suppresses the output.
  • Often it is useful to split input over multiple lines. To split a statement across multiple lines, enter three periods ... at the end of the line to indicate it continues on the next line.

Example 2

Splitting the expression 2+34-5 23 45 over multiple lines.


    (2+3)...
    /(4-5)

Comments, questions, feedback, criticisms?

Send feedback