80 likes | 241 Views
QMCS 130: Today’s Class. Matlab “Matrix Laboratory” Chapter 1 – matrices and algorithms Chapter 2 – syntax Chapter 3 – types, plotting. Matlab is ‘normal software’. It’s a lot more like Excel than C You type in the computation and it ‘happens’ No ‘compilation’ step
E N D
QMCS 130: Today’s Class • Matlab “Matrix Laboratory” • Chapter 1 – matrices and algorithms • Chapter 2 – syntax • Chapter 3 – types, plotting R. Smith - University of St Thomas - Minnesota
Matlab is ‘normal software’ • It’s a lot more like Excel than C • You type in the computation and it ‘happens’ • No ‘compilation’ step • You can’t write operating systems with Matlab • Chapter 1 of text • A lot about scales, measures, algorithms • Matrices often have 2 rows: a scale and a measurement • A 2D image matrix has elements representing pixels (dots) • 3D image matrix has elements for level of primary colors • Algorithm: a set of directions for a computational task, described in terms of simpler operations R. Smith - University of St Thomas - Minnesota
Syntax things • Variable names – much like C • Letters, digits, underscores • Both ‘functional’ and ‘infix’ notation • Infix notation is the usual + - / *, more or less • Prefix noation is “function” notation • Ex: plus(2,2) instead of 2+2 • Uparrow for exponentiation • Hypotenuse: sqrt(a^2 + b^2) • Or: sqrt(plus(power(a,2),power(b,2))) • Assignment statements, just like C • Fibonacci numbers: • prev = 0; final = 1; new = prev + final; • Prev = final; final = new; new = prev+final; R. Smith - University of St Thomas - Minnesota
Chapter 3: Types • Most basic things are floating point numbers • 3 / 2 -> 1.5000, not 1 • Text = strings, marked by single quotes (‘) • Df = ‘dog food’; • Integers show up in some special cases • Images often have integers to give levels (but not always) R. Smith - University of St Thomas - Minnesota
Vectors, Collections • Vector = sequence of numbers • Collection brackets: “[“ and “]” • Example • time = [0 1 2 3.5 7]; • temp = [93.5 90.6 87.7 83.9 76.6]; • Including vs omitting the semicolon • Calculations on the vectors • Time in seconds: time .* 60 • Fahrenheit conversion: 32 + temp .* 9 ./ 5 • Other examples: x + 1; x .^2; sqrt(x); log(x) • Combining 2 vectors: time + temp; time ./ temp … • Vector operations: • length, size, sum, prod, cumsum, cumprod R. Smith - University of St Thomas - Minnesota
Plotting • plot(time, temp) • plot(time, temp, ‘-o’) • Shows data points R. Smith - University of St Thomas - Minnesota
Plotting built in functions • Colon operator • Creates a vector with preset values • Like ‘fill’ operations in Excel • colon(start, incr, end) • Start = starting value • Incr = increment • End = ending value • Example • Hunds = colon(0, 0.01, 4) • Tenths = colon(0, 0.1, 20) • Try plotting: sin, cos, etc. R. Smith - University of St Thomas - Minnesota
Creative Commons License This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA. R. Smith - University of St Thomas - Minnesota