Chapter 11 Using MATLAB as a Powerful Calculator

MATLAB can do basic arithmetic, including trigonometry and linear algebra. Read through the MATLAB basics and try the examples below to get used to using MATLAB commands.

If you ever need additional help, recall the MathWorks help site: here.

To get started, open MATLAB and make sure you are working in the Command Window: MATLAB

11.1 MATLAB Basics for calculation:

  • We can add (+), subtract(-), multiply (*), and divide(/) any two numbers
  • All of our favorite functions are in MATLAB:
    • The square root function:

      i.e. To calculate \(\sqrt{44944}\), just type sqrt(44944)

    • The exponential function:

      i.e. To calculate \(e^4\), just type exp(4)

    • The natural log function:

      i.e. To calculate \(\ln{(3)}\), just type log(3)

      Note: \(\log_{10}{(3)} = \cfrac{\ln{(3)}}{\ln{(10)}}\)

    • Trig functions:

      i.e. To calculate cos\(\left(2\right)\) where \(2\) is in radians, just type cos(2). To calculate \(\cos{(2)}\) where \(2\) is in degrees, type cosd(2)

    • Powers:

      i.e. To calculate \(8^{1.3}\), just type 8^(1.3)

  • All of our favorite numbers are there too:
    • For \(\pi\), just type pi

    • For \(\sqrt{-1}\), just type i

      Caution: If you assign the variable \(i\) a value, it will overwrite this!

  • Putting a semi-colon (;) at the end of any line in MATLAB will suppress the output

Question 1

  1. Find \(\sqrt{3697929}\)
  2. Note that MATLAB approximates when you are not dealing with a perfect square. Approximate \(\sqrt{134502}\)
  3. Approximate \(ln{(74)}\).
  4. Approximate \(\log_{10}{8.6}\)
  5. Let \(x = 42\). Approximate \(sin{(x)}\) assuming \(x\) is measured in radians.
  6. Let \(x = 42\). Approximate \(sin{(x)}\) assuming \(x\) is measured in degrees.

11.2 Early words of caution:

  • MATLAB is case sensitive! If you define b = 2 and then try to use B, MATLAB will tell you B is not defined.
  • Anytime you define a variable (i.e. x = 4), that is stored for later use. Once MATLAB is closed, all variables are erased. To clear variables without closing MATLAB, you have two options:
    1. Clear an individual variable (i.e. clear x)
    2. Clear all variables (i.e. clear all)
  • To clear the command line at any point (just to tidy up your display), simply type clc
  • When dealing with any matrix or vector of values, operations are interpreted as LINEAR ALGEBRA OPERATIONS (matrix operations)!
    • We can only add/subtract two things that have the same number of elements
    • Multiplication is interpreted as matrix multiplication.
    • If you tell MATLAB to perform an operation that does not make sense in the Linear Algebra setting, it will give you an error message.
    • We will see examples of this in the next section when we begin dealing with vectors and matrices.

11.3 Dealing with vectors/matrices:

A vector is simply a matrix with a single row or a single column – they are defined and interpreted in the same way.

11.3.1 Defining a vector/matrix:

  • Always use square brackets \(\left[\dots\right]\) to define a vector/matrix
  • Elements can be separated by a space or a comma
  • Rows can be separated by a semi-colon or by hitting return without closing the vector/matrix
    • Examples:
      1.   A = [1  2  3; 4  5  6; 7  8  9]
      2.   A = [1,2,3 
          4,5,6   
          7,8,9]
  • To define a column vector, we can define it as a column vector or as the transpose (in MATLAB we use ' to denote transpose) of the row vector.
    • Examples:
      1.   x = [1;2;3;4]
      2.   x = [1
          2
          3
          4]
      3.   x =[1  2  3  4]'
  • To get a (row) vector consisting of a range of numbers, we can specify the endpoints, using a colon
    • Examples:
      1. If not given an increment, MATLAB will assume we want integers.
      v = 1:10
      creates the vector \(v = \left[1,2,3,4,5,6,7,8,9,10\right]\)
      1. We can tell MATLAB an increment to use.
      v = 1:0.1:1.5
      creates the vector \(v = \left[1,1.1,1.2,1.3,1.4,1.5\right]\)

Question 2

  • Create a vector \(v\) containing all of the numbers from 0 to 1, in increments of 0.01.
  • Create another vector \(w\) containing all of the numbers from 3 to 5, in increments of 0.02.
  • Create a third vector \(u\) containing all of the integers from 1 to 100 (i.e. 1,2,3,…,100).

11.3.2 Accessing Elements, Adding, and Subtracting

  • To access an element of a vector/matrix, we use parentheses with the position of the element (starting at 1)
    • Examples:
      1.   x = [1;4;7;5];
          x(2)

        should give us the 2nd element, which is \(4\).

      2.   A = [1  2  3; 4  5  6; 7  8  9]
          A(2,3)

        should give us the \((2,3)\) element, which is \(6\).

  • Operations are defined in the LINEAR ALGEBRA sense:
    • Adding/Subtracting two vectors/matrices:
      • They must have the same dimensions (number/arrangement of elements)
      • Adding/subtracting is done element-wise
      • Examples:
        1. A = [1  2  3; 4  5  6; 7  8  9];
          B = [3  -2  5; -7  5  1; 0  -4  -6];
          A+B

          should give us the matrix \[\begin{bmatrix} 4 & 0 & 8 \\ -3 & 10 & 7 \\ 7 & 4 & 3 \\ \end{bmatrix}\]

        2. A = [1  2  3; 4  5  6; 7  8  9];
          B = [3  -2  5; -7  5  1; 0  -4  -6];
          A-B

          should give us the matrix \[\begin{bmatrix} -2 & 4 & -2 \\ 11 & 0 & 5 \\ 7 & 12 & 15 \\ \end{bmatrix}\]

Question 3

  • Using your vectors from Question 2, create a new vector, \(z\), by adding \(v\) and \(w\). What happens if you try to add \(v\) and \(u\)?
  • Using MATLAB, determine the 22nd entry of \(z\)?
  • Given the following matrices,\(A\) and \(B\), compute \(A+B\) and \(B-A\).

\[A = \begin{bmatrix} -1 & 6 & 0 \\ 13 & 2 & -5 \\ 9 & 1 & -7 \\ \end{bmatrix}\] \[B = \begin{bmatrix} 4 & 0 & 8 \\ -3 & 1 & 6 \\ 7 & -4 & 3 \\ \end{bmatrix}\]

11.3.3 Multiplying two vectors/matrices:

  • This is done in the LINEAR ALGEBRA sense of MATRIX MULTIPLICATION (“middle dimensions must match”)

  • If we want to do multiplication element-wise, we must modify the command (using a period before the operation):

    A*B = matrix multiplication

    A.*B = element-wise multiplication

    This period trick works for any operation and is interpreted as “do this element-wise”

    If we want to square each term in a vector x, just type

    x.^2

    OR we can use a for loop

    for i=1:length(x)
            x(i) = x(i)^2;
    end;

Question 4

  • Using the vector \(z\) from Question 3, create a new vector, \(y\), with each entry equal to the cube of the corresponding entry in \(z\).
  • Using MATLAB, determine the 22nd entry of \(y\)?
  • Using the matrices \(A\) and \(B\) from Question 3, create two new matrices, \(C\) and \(D\).
    \(C\) should be the matrix obtained using matrix multiplication.
    \(D\) should be the matrix obtained using element-wise multiplication.
  • Using MATLAB, determine the \((2,3)\) entry of \(C\) and the \((2,3)\) entry of \(D\).
  • Consider the matrix below, \(F\). Why is it impossible to multiple \(A\) by \(F\) element-wise?
  • Again consider the matrix \(F\). Can you multiply \(A\) times \(F\) using matrix multiplication (in that order)? What about \(F\) times \(A\)?
    You don’t need Linear Algebra - use MATLAB to help answer this question.

\[F = \begin{bmatrix} 2 & -6 \\ 3 & -5 \\ 4 & -4 \\ \end{bmatrix}\]