CHAPTER 1: INTRODUCTION INTO MATLAB COMPUTING

 

Lecture Notes 1.1: Computations of variable and arrays (vectors)

 

General Commands:

 

         helpwin, helpdesk: opens the interactive help windows

         help functionname: lists the description of "functionname" in the Command Window

 

help date

 

DATE Current date as date string.

S = DATE returns a string containing the date in dd-mmm-yyyy format.

See also NOW, CLOCK, DATENUM.

 

         dir, ls: lists all files in the directory

         cd c:\ changes directory to "c:\"

         mkdir dirname: creates a new subdirectory "dirname"

         who, whos: lists all initialized variables in the current workspace

         clock: returns the array for [year,month,day,hour,minute,second]

 

clock

fix(clock) % sets the variable clock as the arrray of integers

 

ans =

1.0e+003 *

2.0020 0.0010 0.0040 0.0140 0.0440 0.0245

ans =

2002 1 4 14 44 24

 

         diary on, diary off: records all activities on the Command Window into a file named "diary", it is useful for recording your session for assignments and labs

         diary filename: record activities into file "filename", if the file exists, the new listing is appended

 

Variables:

 

         names and types of variables do not have to be declared

         every object in MATLAB is a complex matrix or its derivative

         scalar variables are initialized as 1x1 matrices of integer, real or complex values

         scalar variables can be later extended into vectors and matrices

         names of variables should not contradict to MATLAB keywords, function names, and command names

 

x = 2 % a numerical variable

abc = 'student' % a string variable

 

x =

2

abc =

student

 

end = 1 % absolutely bad variable

 

Error: Missing operator, comma, or semicolon.

 

% Relatively bad variables:

sin(2) % the value of math.function sin(x) at x = 2

sin = 10 % sin becomes the variable name

sin(2) % sin is no longer associated with the name for math.function sin(x)

 

ans =

0.9093

sin =

10

??? Index exceeds matrix dimensions.

 

% Another example of relatively bad variable:

x = 2 + 3*i % x is a complex number, since i = sqrt(-1)

i = 2 % i is assigned to another number, 2

x = 2 + 3*i % i is not longer associated with the name for sqrt(-1)

 

x =

2.0000 + 3.0000i

i =

2

x =

8

 

% Special numbers and variable names:

eps, pi, inf, NaN, date

 

ans =

2.2204e-016

ans =

3.1416

ans =

Inf

ans =

NaN

ans =

14-Dec-2001

 

         all computations are performed in double precision

         format: switches between different output formats

 

format short; pi % 4 decimal digits

format long; pi % 14 decimal digits

format short e; pi % 4 decimal digits in exponential notations

format long e; pi % 14 decimal digits in exponential notations

format hex; pi % hexagonal representation

 

ans =

3.1416

ans =

3.14159265358979

ans =

3.1416e+000

ans =

3.141592653589793e+000

ans =

400921fb54442d18

         clear variablename: delete value of "variablename"from the current working space

         clear all: clear values of all variables

         clc clear the command window and moves the cursor to the top

 

Arithmetic operations:

         "+", "-", "*", "/" conventional operators for addition, subtraction, multiplication and division

         "\" inverse division (3\1 = 1/3 = 0.33333)

         "^" power operator

         more operators are available for vectors and matrices

         ";" a command is performed but the result is not printed, one line may contain several commands

         "," a separator of different commands, the result is printed for each command

         "" a continuation of a long command into several lines

         "%" a comment sign, the whole line after the comment sign is ignored in computations

 

% Example of a mini-script:

r = 10; % radius of a sphere

vol = 4*pi

...*r^3/3; % computation of volume of a sphere

r,vol

 

r =

10

vol =

12.5664

 

Vectors as one-dimensional arrays:

 

         column-vectors: x = [x1 ; x2 ; ; xN]

         row-vectors: x = [x1, x2, , xN]

 

x = [ 0, 0.5, 1, 1.5, 2], y = [ 0; 0.5; 1; 1.5; 2]

 

x = 0 0.5000 1.0000 1.5000 2.0000

y = 0

0.5000

1.0000

1.5000

2.0000

 

         access to individual elements: x(j), y(k)

x(2), y(5)

 

ans = 0.5000

ans = 2

 

x(6)

??? Index exceeds matrix dimensions.

y(0)

??? Index into matrix is negative or zero.

         resize:

x(8) = 4; x % increase the dimension of x to 8 and assign 4 to x(8)

 

x =

0 0.5000 1.0000 1.5000 2.0000 0 0 4.0000

 

y = y(2:4); y

% reduce the dimension of y to 3 and assign y(2),y(3),y(4) to a new vector

 

y =

0.5000

1.0000

1.5000

 

         transpose: x',y'

z1 = y', z2 = y''

 

z1 =

0.5000 1.0000 1.5000

z2 =

0.5000

1.0000

1.5000

 

         length: computes the number of elements in the vector

         size: computes the dimension of the vector, taking into account its horizontal or vertical structure

length(z1), length(z2), size(z1), size(z2)

 

ans =

3

ans =

3

ans =

1 3

ans =

3                    1

 

         initialization of equally-spaced vectors:

 

 

x1 = 0 : 0.1 : 0.25 % the last element is not reached

x2 = 0 : 5 % default step size = 1

x3 = 5 : -1 : 0 % the step size can be negative

x4 = 0 : -1 : 1 % non-valid operation produces empty vector

length(x4) % the empty vector x4 has a zero length

 

x1 =

0 0.1000 0.2000

x2 =

0 1 2 3 4 5

x3 =

5 4 3 2 1 0

x4 =

Empty matrix: 1-by-0

ans =

0

 

 

y(1:3:12) = 1; y(2:3:12) = 2; y(3:3:12) = 3; y

 

y =

1 2 3 1 2 3 1 2 3 1 2 3

 

x1 = linspace(0,0.25,5)

% the built-in function never mismatches the last element

x4 = linspace(0,-1,10) % the vectors are always non-empty

h1 = x1(2) - x1(1)

% the step size can be easily computed (need not to be given)

h4 = x4(2) - x4(1)

 

x1 =

0 0.0625 0.1250 0.1875 0.2500

x4 =

0 -0.1111 -0.2222 -0.3333 -0.4444 -0.5556 -0.6667 -0.7778 -0.8889 -1.0000

h1 =

0.0625

h4 =

-0.1111

 

         vector operations:

 

 

x = [ 0, 1, 2 ]; y = [ 0.1, 0, 0.1]; z = [ 10, 20 ];

x + y

x - y

 

ans =

0.1000 1.0000 2.1000

ans =

-0.1000 1.0000 1.9000

 

x + z

 

??? Error using ==> +

Matrix dimensions must agree.

 

0.2 + x

0.2 * x

 

ans =

0.2000 1.2000 2.2000

ans =

0 0.2000 0.4000

 

x.*y % main advantage of MATLAB (matrix laboratory)

x./y % no loops for accessing individual elements are required

x.\y % inverse division, the same as y./x

 

ans =

0 0 0.2000

Warning: Divide by zero.

ans =

0 Inf 20

Warning: Divide by zero.

ans =

Inf 0 0.0500

 

x.*z

 

??? Error using ==> .*

Matrix dimensions must agree.

 

x.^3, y.^(0.1)

x.^y % the same as x(k)^(y(k)) for any k

3.^x % the same as 3^(x(k)), the output is a vector of the same structure as x

 

ans =

0 1 8

ans =

0.7943 0 0.7943

ans =

0 1.0000 1.0718

ans =

1 3 9

         appending vectors

w = [ x, y, z]

w = [x'; y'; z']

 

w =

0 1.0000 2.0000 0.1000 0 0.1000 10.0000 20.0000

w =

0

1.0000

2.0000

0.1000

0

0.1000

10.0000

20.0000

         deleting elements of vectors

x(2) = []; x

w(3:6) = []; w

 

x =

0 2

w =

0

1

10

20

 

Strings as vectors of characters

str1 = 'Dmitry10';

str1(1:6) % the first three characters are displayed

length(str1) % the length of character vector "str1"

str2 = 'Pelinovsky20';

str3 = [str1, str2] % concatanetion of two string vectors

str4 = [str1(1:6),' ',str2(1:10)]

 

ans =

Dmitry

ans =

8

str3 =

Dmitry10Pelinovsky20

str4 =

Dmitry Pelinovsky