Uploaded by epic smurf

Week 9 Lecture 1

advertisement
ENGG1003 Week 9 Lecture 1
MATLAB












MATLAB is an interpreted programming language designed for quickly preforming numerical
analysis
It is sometime criticized for not being a “legitimate” programming language
o Engineers use it to solve complex numerical problems quickly, then throw away the
code
o The code is written quickly it does not execute quickly (compared to a compiled
language like in c)
Arithmetic is fast, flow control is very slow
o Problems need vectorisation
Interpreted
o A language is compiled when the entire source code listing gets converted into
binary executable in one step
o An interpreted language is read and executed line by line
 The language interpreter is running the whole time your code is running
Interpreted languages are slower, but have the advantages of
o Being more forgiving of mistakes
o Having more advanced memory management
 Variables do not need to be declared
 Arrays automatically grow and shrink as needed
o Allowing code snippets to easily be executed in isolation
 You can run single lines instantly by typing the into a command prompt
 Code does not need to be written to a source file although it is a good idea
to use one
MATLAB is “weakly typed”
o There are no strict data types
o By default, almost everything is a complex valued array of type double
Arithmetic mostly follows rules of linear algebra
o Somewhat beyond this course. we do not cover matrix multiplication
o Many language behaviours will make more sense after you have studied linear
algebra
o The fact that everything is an array makes for some possibly confusing rules
MATLAB has “high level” features like plotting
o it is more of a “calculator engine” than a programming language
MATLAB is (very expensive) commercial software
o Python is far mor popular in industry (mostly because its free)
The standard license for companies is 1260$ per year, per computer
Octave is a cost-free MATLAB like interpreter (everything in this course should run without
any modification, it is acceptable to use for the whole course including assessments
Tends to load faster than MATLAB


MATLAB may be “weak typed” but the following classifications are useful
o A scalar is a single number
o A vector is a row or column of numbers
 Like a 1d array in c
o A matrix is rectangular array of numbers
 Like a 2d array in c
o MATLAB also supports arrays of matrices
 Like a 3d array in c
Arithmetic operations have different behaviours with different arguments, especially mixed
(e.g. what does scalar plus vector give)
Variable Assignment






When allocating a constant to a variable we have a few basic methods
o Scalar
 X=5
o Row vector
 X = [1 2 3 4]
o Column vector
 X = [1;2;3;4]
o Matrix
 X = [1 2 3 4;5 6 7 8;9 10 11 12]
help (then whatever function name)
o gives you the documentation for that function
o MATLAB has a built-in manual
o Or use doc (then whatever function name)
 This will give you more detailed documentation as well as examples
For scalar data MATLAB supports all basic arithmetic operators just like c
o It also supports exponent with ^
o Ex 10^5 = 100000
For vector and matrix data addition and subtraction is element wise
o The arguments should be the same size
 Or at least one dimension must match but this is beyond this course and is
only supported on newer versions. Also really sloppy
Multiplication and division are completely different beast, its beyond this course basically
To do element wise operations put a . in front of the operator (ex, “. ^”)
Vectorisation

Vectorisation is the process of arranging a large numerical computing task so that it can be
broken up into sub tasks which can be executed simultaneously
Download