Title: Computer Science 102
1Computer Science 102
- Into to Computational Modeling
- Spring 2009
- Special Topics Programming in Matlab
2Matlab
- An integrated programming and graphical
environment - Interpreted interactive get answer immediately
- Also supports saving and loading large programs
- Student version available for 100 from
mathworks.com - Free, open-source equivalent Octave
- http//www.gnu.org/software/octave
3Matlab
- Questions
- Why Matlab vs. lower-level (C / Java / Python)
language? - Why Matlab vs. a similar package (Mathematica,
Maple) ?
4Matlab vs. C/Java/Python
- Abstraction We should be able to ignore
low-level details, such as memory management
(required in C) - Expressiveness
- Simple (primitive) operations like add, subtract,
multiply, divide, should work on entire matrices
(tables) at once, without having to consider
individual elements - Common operations like S (sum over elements)
should be built into the language, so we dont
have to code them ourselves.
5Expressiveness
gtgt a rand(3) a 0.7577 0.6555
0.0318 0.7431 0.1712 0.2769 0.3922
0.7060 0.0462 gtgt b rand(3) b
0.0971 0.3171 0.4387 0.8235 0.9502
0.3816 0.6948 0.0344 0.7655 gtgt c a
b c 0.8549 0.9726 0.4706
1.5666 1.1214 0.6585 1.0871 0.7405
0.8117
- Generate two matrices of uniformly distributed
random numbers and add them
6Expressiveness
- Where one matrix is greater than another, set
the first matrix to zero
gtgt a(agtb) 0 a 0 0
0.0318 0.7431 0.1712 0.2769 0.3922
0 0.0462
7Matlab vs. Maple/Mathematica
- Features
- Matlab Numerical (linear algebra)
- Maple, Mathematica Symbolic (calculus)
from http//www.physicsforums.com/archive/topic/
t45208_Matlab_vs_Maple_vs_Mathematica.html
... Matlab is excellent for manipulating data, or
dealing with any sort of matrix
calculations.. From my experience Mathematica is
ideal when it comes to symbolics such as
differentiation and integration. I've seen a few
cases where Mathematica blows Maple out of the
water when one compares the types of integrals
they can evaluate. Maple and Matlab have the
best graphics in my opinion. In both of them you
are allowed to rotate 3D graphics and zoom in on
the spot (2D) in realtime. In Mathematica things
are little more complicated, which often
elicits frustration. With Mathematica, in order
to zoom, you must change the window that you are
plotting with.
8The Matlab Environment
- Integrated Development Environment
- Editor Command Window (Interpreter)
9Expressions and Commands
- Anything you type into the Matlab interpreter (gtgt
prompt) is a statement. - An expression is a statement that provides all
the information needed to invoke (perform) a
computation.
10Expressions and Commands
- The interpreter responds by evaluating the
expression - Simplest example is a constant value
- gtgt 4
- ans 4
11Expressions and Commands
- An expression typically contains
- an operator , , -, /, , sin, cos, sqrt
- one or more inputs (a.k.a. arguments a.k.a.
operands) - Two different styles (same result)
- Functional plus(3, 2)
- Infix 3 2
12Expressions and Commands
- As in arithmetic notation,
- build compound expressions from simple
expressions 3 2 5 / sin(pi/3) - use Order of Operations (PEMDAS) and parentheses
to disambiguate - (3 2) 5 / sin(pi/3)
13Expressions and Commands
- Good programmers don't
- assume that others know order of operations
- want to waste time using o.o.o. to disambiguate
code they're reading
- So use parens if there's a reasonable chance of
ambiguity
1 2 3 / 4
1 ((2 3) / 4)
Programs should be written primarily to be read
and understood by people D. Knuth.
14Changing State Assignment
- Just as a system (health, airplane, particle) can
have a state, so can a computation. - State of computation is contents of computer's
memory (RAM). - We change this state (and hence state of system
we're modeling) by assigning values to variables
gtgt altitude 13000 gtgt temperature 98.7 gtgt area
pi radius2
15Changing State Assignment
name
expression
gtgt
- Variable name must start with a letter or
underscore (_), and can then contain letters,
numbers, and underscores
a
radius
buffalo66
cost_of_living
16Changing State Assignment
- This notation differs from arithmetic, where it
makes no sense to say, for example, x x 1 - In addition to state, variables help us to
- write general formulas
- gtgt c sqrt(a2 b2)
- avoid repeating computation of useful
intermediate values. - We should avoid repeating the same computation
(code), because if there's a bugg in it, we have
to fix it multiple times. - We should avoid repeating the same computation
(code), because if there's a bugg in it, we have
to fix it multiple times.
17Changing State Assignment
- E.g., gravitational force F of moon at location
(x, y, z) w.r.t. center of earth
all contain
18Changing State Assignment
all contain
gtgt pos_to_force -GmM/((x2 y2
z2)(3/2)) gtgt Fx xpos_to_force gtgt Fy
ypos_to_force gtgt Fz zpos_to_force
19Help!
- All Matlab operators have built-in help
- gtgt help cos
- COS Cosine.
- COS(X) is the cosine of the elements of X.
-
- See also ACOS, COSD.
- Unbroken block of comments at top of your own
code gets reported as help
gtgt help simon_lab1 Simon Levy, CSCI 121, Jan.
04,2005 Lab 1 Polynomial Curve Fitting
20What if you cant remember the exact name?
gtgt lookfor circle WRLDTRV Show great circle
flight routes around the globe.
ipexConformalShowCircles Plot packed circles
before/after transformation. CIRCCIRC Find the
intersections of two circles in cartesian
space GC2SC Converts a great circle definition
to a center and radius GCWAYPTS Computes
equally spaced waypoint coordinates along a great
circle GCXGC Computes the intersection points
between two great circles . . .
21Functions The Basis of Computing
- Most of what we do in Matlab (or any language)
involves writing and using functions. - Like a function in math, a Matlab function has
one or more inputs (arguments) and an output
(return value). - Each function is stored in its own document (.m
file) - We can call functions directly from the command
prompt, and functions can call each other.
22Example Hypotenuse of Right Triangle
23Variable Names Are Arbitrary But Should Make Sense
required
function foo bar(baz, moo) foo sqrt(baz2
moo2)
24A Functions Variables Are Private (Hidden)
25Programming to an API
- It is rare to build an entire software project
from scratch. - Instead, we reuse our own or others code.
- So we have to know how to invoke (call) existing
code. - Application Programming Interface specifies how
to do this - Simplest form is what we get when call help on a
function
26Requirement Specification via Stub Functions
- Stub function is like an API for something not
yet written