a (very) short introduction to matlab j.a. marr george mason university school of physics, astronomy...

25
A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences

Upload: daniella-dickerson

Post on 18-Jan-2016

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences

A (VERY) SHORT INTRODUCTIONTO MATLAB

J.A. MARRGeorge Mason University

School of Physics, Astronomy and Computational Sciences

Page 2: A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences

FIRST…WHERE IS MATLAB W/R/TPROGRAMMING LANGUAGES?

• Matlab• LISP family, Python, Java• Fortran, Algol family, C++• C• assembly language• machine code (binary!)

micro codeIncreasing

ease-of-use(fairly subjective)

Increasing program size

(fairly objective)

Page 3: A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences

WHY MATLAB?

“Softer” Reasons . . . • Most scientists and engineers know it• Very widely used in industry, academia and gov• Easy to learn• LOTS of pre-developed application packages• Very good technical support/user community

“Harder” Reasons . . . • Excellent native graphics capabilities• Includes many modern numerical methods• Highly optimized for scientific computing• EXCEPTIONALLY FAST matrix operations• Interfaces well with other languages/packages

Page 4: A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences

• ASSIGNMENT: single variables

• ASSIGNMENT: matrices (or, arrays)

• ITERATION (FOR loops)

WHAT WE’LL STUDY HERE…

MEANS GO TO THE ACCOMPANYING WORKSHEET assignments_practice.m AND READ THE COMMENTS AND CODE IN THE SECTION OF THAT WORKSHEET KEYED TO THE PRESENTATION TOPIC FOUND HERE.

Page 5: A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences

ASSIGNMENT (VARIABLES)

• Think of a box with a label on it. We can put “stuff” inside that box.• Labels on boxes allow us to refer to specific boxes, by name, at a later time, as needed.• These labeled boxes are what we call a “variables”.• Putting “stuff” inside the box is called assigning a value to a variable, or simply, assignment.• In Matlab, we write a variable name (which creates the labeled box), then write an equals sign (=) and finally write the value we want to “put in the box”, or, assign to the variable. Examples:

A = 10a = 5TwelveDigitsOfPi = 3.141592653589

Page 6: A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences

ASSIGNMENT (VARIABLES)

More Examples:MyVariable = 1.349e-03bushels_of_wheat = 2.0constant = -13.0

Rules:• The “=” symbol DOES NOT MEAN EQUALS! It means assignment: Assign the value on the right of the “=” symbol to the variable on the left of the “=” symbol (“equals” is denoted by ==).• To access what’s “in the box”, simply write (or type) the name of the box (i.e., to retrieve the value assigned to a variable, type that variable’s name). So a variable written on the right means, “retrieve the value in this box”.

Page 7: A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences

ASSIGNMENT (VARIABLES)

REMEMBER:• Value on the right of “=” gets stored into variable on the left of “=”:

var1 = 5.0

• Valid assignment (creates var2, assigns it the value contained in var1):

var2 = var1

• Invalid assignment (generates error: var3 not previously declared – holds no value)

var2 = var3

Page 8: A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences

ASSIGNMENT (VARIABLES)

• Rules (cont):• Variables can be used in assignment statements to assign values to other variables: Since typing the name of a variable retrieves the value stored in it, we can assign the value contained in one variable, to another variable:

var1 = 3.0 var2 = var1

• We can do math with variables, too:

var3 = var2 + var1 var4 = var3 var2 var5 = var4 / var1 var6 = var2 ^ var1

Page 9: A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences

ASSIGNMENT (VARIABLES)

• Rules (cont):• In particular, we can “update” a variable (updating by 1 is usually called “incrementing”):

var7 = 1var7 = var7 + 1

• Variables can be only one TYPE at a time. Numbers are principally of two types: integers (3, 0, -3) and floating point numbers, or floats (3.14159). Floats are further divided into single and double precision. Matlab’s floats are double precision by default.

LET MATLAB HANDLE THE DETAILS!

Page 10: A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences

ASSIGNMENT (VARIABLES)

• Rules (cont):• Ending any statement with a semi-colon will cause Matlab to NOT echo the variable and its value back to the screen:

EDU>> var8 = 3.0

var8 =

3

EDU>> var9 = 5.0;

EDU>>

Page 11: A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences

ASSIGNMENT (MATRICES or ARRAYS)

• Think of a MATRIX as a partitioned box: The box itself has a label (variable name) and we can store MULTIPLE things inside the box, each inside its own partition.

• We can manipulate the ENTIRE COLLECTION at once, just by referring to that one label (matrix name).

A ==

Page 12: A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences

• A matrix has DIMENSIONS: In the 2 dimensional case, rows and columns.• Each partition is referred to by both its row number and its column number.•Rule:

• In Matlab, both row and column numbers START AT 1:

(1,1) (1,2) (1,3)

(2,1) (2,2) (2,3)

(3,1) (3,2) (3,3)

(ROW, COLUMN)

A ==

ASSIGNMENT (MATRICES or ARRAYS)

Page 13: A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences

• We can access individual partitions (called “elements”) by writing the matrix name, followed by a left paren “(“, the row number, a comma, the column number, and a right paren “)”:

A ==

A(1,2)

A(1,1)

A(2,3)

A(3,2)

ASSIGNMENT (MATRICES or ARRAYS)

Page 14: A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences

A(1,1) A(1,2) A(1,3)

A(2,1) A(2,2) A(2,3)

A(3,1) A(3,2) A(3,3)

(ROW, COLUMN)

• Rule:The name for an individual matrix element acts just like a variable.

A ==

4 2 0

13 7 12

1 3 -10

==

(ACTUAL VALUES)

Typing “A(1,1)” at the command line, will result in 4Typing “A(2,2)” at the command line, will result in 7Typing “A(3,3)” at the command line, will result in -10

A(1,3)

ASSIGNMENT (MATRICES or ARRAYS)

Page 15: A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences

• Rules:• Each matrix element acts like a variable

(ACTUAL VALUES)

varX = A(1,1) + A(1,2)varX = A(2,2) A(1,2)varX = A(3,1) - A(2,3)varX = A(1,2) ^ A(3,2)

4 2 0

13 7 12

1 3 -10

ASSIGNMENT (MATRICES or ARRAYS)

A(1,1) A(1,2) A(1,3)

A(2,1) A(2,2) A(2,3)

A(3,1) A(3,2) A(3,3)

(ROW, COLUMN)

A == ==

Page 16: A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences

A(1,1) A(1,2) A(1,3)

A(2,1) A(2,2) A(2,3)

A(3,1) A(3,2) A(3,3)

(ROW, COLUMN)

• As a result, you can assign values to specific matrix elements, too:

A == ==

(NEW VALUES)

A(1,1) = 100A(2,2) = 200A(3,3) = 300

100 2 0

13 200 12

1 3 300

ASSIGNMENT (MATRICES or ARRAYS)

Page 17: A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences

• Matrices can be different dimensions. Here’s A 1-D matrix (called a “row vector”):

(ROW, COLUMN)

B == B(1,1) B(1,2) B(1,3) B(1,4) B(1,5)

(ACTUAL VALUES)

B == 10 8 6 4 2

B(1,1) == 10 B(1,3) == 6 B(1,5) == 2

ASSIGNMENT (MATRICES or ARRAYS)

Page 18: A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences

(ROW, COLUMN)

• Another 1-dimensional matrix (called a “column vector”):

C ==

(ACTUAL VALUES)

C ==

C(1,1) == 10

C(3,1) == 6

C(5,1) == 2

C(1,1)

C(2,1)

C(3,1)

C(4,1)

C(5,1)

10

8

6

4

2

ASSIGNMENT (MATRICES or ARRAYS)

Page 19: A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences

• Creating matrices in Matlab:• Method #1: Like variable assignment:• Assign the last element in a 1-D matrix, or the “lower right corner element” in a 2-D matrix to be some value.• Now you have a matrix filled with zeros and whatever you assigned to the last element:

EDU>> D(1,5) = 0 D = 0 0 0 0 0

EDU>> D(3,3) = 10 D = 0 0 0 0 0 0 0 0 10

ASSIGNMENT (MATRICES or ARRAYS)

Page 20: A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences

• Creating matrices in Matlab:• Method #2: Write it out explicitly. Use either spaces or commas to separate values in a row. Use semi-colons to separate rows:

EDU>> D = [1 2 3; 4 5 6; 7 8 9]

D =

1 2 3 4 5 6 7 8 9

EDU>> D = [1, 2, 3; 4, 5, 6; 7, 8, 9]

D =

1 2 3 4 5 6 7 8 9

ASSIGNMENT (MATRICES or ARRAYS)

Page 21: A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences

ITERATION (FOR loops)

• Often times, we’ll want to repeat doing something again, and again, and again, and again…maybe millions of times…or maybe, just enough times to access and change each element of a matrix.

• Computers, of course, are great at doing things over and over again

• This is called ITERATION.

• Iteration is executed with something called a FOR loop

Page 22: A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences

ITERATION (FOR loops)

• Syntax: As shown—always the same. NOTE: The keywords FOR and END come in a pair—never one without the other.

for n = [1:5] n end

• What’s happening:• n first assigned the value 1• n then written to the screen• END is encountered• END sends execution back to the top• Repeat: n = 2, 3, 4, 5• Is n at the end of the list? Then STOP.

“Loop body”(always indented)

SIDE NOTE:n = [1:5] same asn = [1,2,3,4,5]

loop index

Page 23: A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences

ITERATION (FOR loops)

• So, a FOR loop is doing “implicit assignment” to the loop index n: each time “through the loop” (or, one “trip”), the loop index n has a particular value (which can’t be changed by you, only by the FOR loop). After the trip is complete (when execution reaches END), the loop index is reassigned to the next value in the 1-D matrix, from left to right. When the rightmost (last) value is reached, the FOR loop stops executing.

And then statements AFTER the FOR loop (following the END keyword) are executed --that is, the FOR loop “exits”.

Page 24: A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences

ITERATION (FOR loops)

• FOR loop examples: Study carefully, so that you understand what each loop is doing!

for n = [1:5] n^2end

mat(1,5) = 0;for n = [1:5] mat(1,n)=nend

mat2(1,5) = 0;mat3(1,5) = 0;mat4(1,5) = 0;for n = [1:5] mat2(1,n) = n mat3(1,n) = n^2 mat4(1,n) = mat2(1,n) + mat3(1,n)end

var1 = 2.0;var2 = 3.0;mat1(1,5) = 0;for n = [1:5] mat1(1,n) = var1+ nvar2;end

Page 25: A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences

POSTSCRIPT

“The only way to learn a new programming language is by

writing programs in it.”

Brian Kernighan & Dennis Richie“The C Programming Language”