Introductory Programming Fall 2006 For today, you should have: 1) Read Chapter 2 Today: 1) update continued 2) matrices For next time you should: 1) modify update so it stores successive values of a and b in vectors named A and B, and then plots them 2) prepare for Evaluation 2, which will cover everything up to what we did today. Update ------ Last time we wrote a script that looks like this: anew = a - 0.03 * a + 0.05 * b bnew = b + 0.03 * a - 0.05 * b a = anew b = bnew Today we will gradually improve this code, following these steps: 1) use round() to make sure we are moving integral cars! suggestion: define variables froma and fromb 2) add a loop, so the script runs a number of iterations 3) add plotting some commands we might want: clf % clear the current figure hold on % allow mulitple plots on the same figure plot(x, y, 'ro') % plot a red o at x, y plot(x, y, 'bx') % plot a blue cross at x, y Matrices -------- In linear algebra, 1) a single number all by itself is a scalar. 2) a sequence of numbers is a vector arranged horizontally, it's a row vector; vertical is a column vector 3) a 2-D grid of numbers is a matrix 4) an n dimensional grid of numbers is a tensor, for n>2 In most programming languages, 1) scalars are represented by integers and floating point numbers 2) vectors, matrices and tensors are represented by 1-D, 2-D and n-D arrays In MATLAB, Everything Is a Matrix! 1) a matrix that happens to be 1x1 is treated as a scalar 2) a matrix that is 1 x n is a row vector 3) a matrix that is n x 1 is a column vector 4) a matrix that is n x m is a matrix Creating matrices ----------------- Try the following code: a = 1 b = [1, 2] c = [3, 4] d = [1, 2; 3, 4] % notice the semi-colon! e = [b; c] whos The whos command is similar to who, but it gives the size and type of each value in your workspace. The [] operator creates a new matrix. Inside square brackets: * the comma operator separates elements in a row * the semi-colon operator separates rows What do you think [b, c] does? To be more concise, you can leave out the commas. As usual, let's think of everything we can do wrong with matrices! length and size --------------- What are the length() and size() of a, b, c, d and e? The colon operator ------------------ The colon operator, which we have seen in the context of a for loop, also creates a matrix: x = 1:10 whos What happens if we try: y = 10:1 whos What about: z = [] whos Matrix operations ----------------- Most operators and functions work on matrices w = 0:0.1:2 x = w * pi % multiplication by a scalar does what you expect y = sin(x) % most math functions do what you expect While we are at it, if you have two matrices that are the same length, you can plot one as a function of the other. plot(x,y) Most operations between matrices and scalars do what you expect. Addition and subtraction between matrices does what you expect: a = 1:3 b = 1:3 c = a+b d = a-b It performs the operations "elementwise" Multiplication DOES NOT do what you expect: e = a*b ??? Error using ==> mtimes Inner matrix dimensions must agree. The .* operator performs elementwise multiplication. e = a .* b Same with exponentiation f = a^2 ??? Error using ==> mpower Matrix must be square. f = a.^2 % elementwise exponentiation You can divide a matrix by a scalar. For now, you should not divide by a matrix. Indices ------- (we'll start out talking about vectors, then generalize to matrices.) The elements of a vector are numbered from 1..n, where n is the length of the vector. You can read, or access, an element with the () operator x = 1:10 y = x .^ 2 z = y(10) The expression in paretheses is called an index, because it indicates which element you want. It must be an integer between 1 and n, inclusive. y(0) y(11) What do you think will happen for the following: y(1.0) y(1.000000001) y(1.000000000000000000001) y(i) y('1') Keep that last one in mind!