Chapter 03

# Chapter 03 - Chapter 3 Matrices, Operators, and Functions...

This preview shows pages 1–3. Sign up to view the full content.

Chapter 3 Matrices, Operators, and Functions The basic unit with which we work in Matlab is the matrix (plural is “matrices”). We add them, subtract them, multiply them, and perform many other operations on them. In this chapter, we introduce the basic idea of the matrix. We show how Matlab allows you to define matrices and to operate on them. In addition to providing simple operations on matrices, such as addition or subtraction, which are stated using the usual plus sign and minus sign of mathematics, Matlab provides hundreds of other operations that are carried out by calling built-in functions, such as sqrt(x) , which is a function that calculates the square root. 3.1 Matrices The name "MATLAB" stands for "Matrix Laboratory", which suggests (correctly) that Matlab is designed to deal with matrices. A matrix is a 2-dimensional rectangular arrangement of numbers, such as this “2x3”, matrix, which has 2-rows and 3-columns: 1 2 3 3.3 3.1416 -4 A matrix is a useful form for dealing with sets of two or more equations involving sets of two or more variables, a situation that arises repeatedly in all branches of science and engineering. Scientists and engineers also work with scalars (single numbers). Surprisingly perhaps, Matlab treats a scalar as a 1x1 matrix! In fact even the x in sqrt(x) above is treated in Matlab as if it were a 1x1 matrix! To see the size of a matrix in Matlab, you can use the built-in function called size : >> size(x) ans = 1 1 A function in mathematics is any operation that produces a result that depends only on its input. In Matlab, as in most other programming languages (C, C++, Java, Fortran), a function is any operation that is invoked by giving its name. The major distinction between these two definitions is that a mathematical function will always produce the same output for a given input, whereas that is not necessarily true for a function provided by a programming language. Input to a function is given as a list of values separated by commas inside a pair of parentheses that follows the name. Each such value is called an argument (in both mathematics and programming). Here the size function was given only one argument, x , (so no commas are needed), and it produced as its result two numbers—1 and 1. The first 1 represents the number of rows, or height, of the matrix x

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
and the second 1 is its number of columns, or width. It is possible to make larger matrices by using brackets and semicolons ( ; ). As another example, let’s create in Matlab the matrix that we gave at the beginning of the chapter, assign it to the variable X , and then call size using X as an argument, as follows: >> X = [1 2 3; 3.4 pi -4] X = 1.0000 2.0000 3.0000 3.4000 3.1416 -4.0000 >> size(X) ans = 2 3 Note that the matrix is printed in row-major order , meaning that all the elements on one row are printed before the elements in the next row. Note also that when the semicolon occurs inside square brackets it means "end of row" (instead of suppressing printing). Hitting the
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 04/08/2008 for the course CS 103 taught by Professor Fitzpatrick during the Spring '07 term at Vanderbilt.

### Page1 / 32

Chapter 03 - Chapter 3 Matrices, Operators, and Functions...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online