INTRODUCTION TO MATLAB (Part I)

Introduction

Matlab is a computer application that simplifies many complex mathematical tasks.

The name Matlab stands for "matrix laboratory".

Matlab has many functions and "Toolboxes" to help in various applications.

Starting Matlab

Initial Matlab prompt:

The main window of Matlab look likes as follows

There are two more windows. One is called editing window and the other is called figure window.

Editing window

Figure window

Basic operation of Matlab

Matlab can be used like calculator

Note that the calculation is always returned in the default variable “ans” which is the answer to the last calculation done. Values can also be assigned to variable names with

the equal sign, and these variables may also be used in calculations as seen in the examples below.

Please do not use the names of Matlab commands and functions as the variable names! Matlab can confuse it.

If you type pi=1., then pi will return as 1. instead of 3.14.

MATRIX SETUP & MANIPULATION

The real power of MATLABis its ability to deal seamlessly with matrices. The basic operators you saw above work for matrices and vectors as well as seen in the example below.

The example above illustrates the assembly of a row and column vector which are then multiplied together. This example introduces the transpose operator which is an apostrophe (‘). The transpose operator simply interchanges the rows and columns of a matrix. It is easy to type matrices into MATLABas rows, and the transpose operator will help you align data as columns. Please note that MATLABadheres to the standard rules of matrix algebra and will not let you violate them by multiplying a row vector by a row vector as see below.

Also note that the individual elements of a vector are matrix are assembled between square brackets and may be separated by spaces (as seen earlier) or by commas as seen above. To assemble a matrix you may use a semicolon to separate individual rows as seen below.

Matrices can also be assembled using colons which work like loops. The syntax is:

MATRIX=[start:increment:finish]

where MATRIX above is a vector containing a sequence of values starting from “start” and counting up the “finish” with a step size of “increment” as seen in the example below. Note that if the increment value is omitted an increment of 1 is assumed.

There are many special functions used to quickly assemble matrices some of these are listed in the table below.

The syntax of each of these functions is discussed under their headings in the “help” command (discussed below under MATLAB ADMINISTRATION). Matrices can also be assembled from other matrices using commas or semicolons as seen below.

We know that the linear system of equations above can be solved by multiplying the vector of right hand side values (b) by the inverse of the coefficient matrix (A) as seen below.

Note that in the above example simply by typing a variable its contents are displayed. It is not necessary to take the inverse of a coefficient matrix to find its solution. A simple application of Gauss-Jordan elimination on the augmented matrix is more efficient that taking the matrix inverse. MATLAB uses the metaphor of dividing by the coefficient matrix (as one would do in a scalar equation) to solve this. The following syntax is used to solve a linear system depending on how it is written.

for Ax=b use the backward slash: x=A\b

for xA=b use the forward slash: x=b/A

since our linear system is written as in the former of the two examples we can use the backward slash.

By default MATLAB does math in matrix notation, to change to element by element place a period in front of the operator. For example a^2 will carryout a*a while a.^2 will square every element of matrix a.

More complicated matrix manipulations can be carried out using the colon operator. When accessing parts of a matrix the matrix name is followed by closed parentheses where the first index is the row and the next index separated by comma is the column. In addition to the lists as seen above the colon by itself refers to all rows or columns in a matrix. For example taking matrix A below we can make various manipulations.

References

[1] Pete Ludovice, “Basic Matlab Engineering”, School of Chemical Engineering, Georgia Tech, 2001

[2] Matlab, The Language of Technical Computing, Version 6.5.0.180913a Release 13