250 likes | 366 Views
Scientific Computing Winter 2012 Chapter 2: Invoking a Computation. Computer Science 121. 2.1 Expressions and Commands. Anything you type into the Matlab interpreter ( >> prompt) is a statement .
E N D
Scientific Computing Winter 2012 Chapter 2: Invoking a Computation Computer Science 121
2.1 Expressions and Commands • Anything you type into the Matlab interpreter (>> prompt) is a statement. • An expression is a statement that provides all the information needed to invoke (perform) a computation.
2.1 Expressions and Commands • The interpreter responds by evaluating the expression • Simplest example is a constant value: >> 4 ans = 4
2.1 Expressions 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 • Why have both?
2.1 Expressions 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)
2.1 Expressions and Commands • Good programmers don't • bathe • 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.
2.2 Changing 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 assigningvalues to variables: >> altitude = 13000 >> temperature = 98.7 >> area = pi* radius^2
2.2 Changing State: Assignment • More generally: name expression >> ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ = ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ • Variable name must start with a letter or underscore (_), and can then contain letters, numbers, and underscores: a radius buffalo66 cost_of_living
2.2 Changing State: Assignment • This notation differs from arithmetic, where it makes no sense to say, for example, x = x + 1 • Book claims “There is no possible number n for which n = n + 1 : is this true?
2.2 Changing State: Assignment >> Inf + 1 ans = Inf
2.2 Changing State: Assignment • In addition to state, variables help us to • write general formulas: >> c = sqrt(a^2 + b^2) • avoid repeating computationof 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.
2.2 Changing State: Assignment • E.g., gravitational force F of moon at location (x, y, z) w.r.t. center of earth: all contain
2.2 Changing State: Assignment all contain • So... >> pos_to_force = -G*m*M/((x^2 + y^2 + z^2)^(3/2)); >> Fx = x*pos_to_force; >> Fy = y*pos_to_force; >> Fz = z*pos_to_force;
Example: Fibonacci Numbers (1202 AD) • Assumptions • Rabbits reproduce one month after birth • Each mother produces one son and one daughter • How many rabbits at the end of each month?
Fibonacci Numbers Build thee more stately mansions, O my soul,As the swift seasons roll!Leave thy low-vaulted past!Let each new temple, nobler than the last,Shut thee from heaven with a dome more vast,Till thou at length art free,Leaving thine outgrown shell by life's unresting sea! – Oliver Wendell Holmes
Fibonacci Numbers: State >> previous = 0; >> final = 1; >> new = final + previous new = 1 >> previous = final; % Repeat these lines >> final = new; % by using the >> new = final + previous % ↑key new = 2
Variables: Style • Book advocates “mixed case” (costOfLiving), but underscore style (cost_of_living) is easier to read. • Use all-upper-case for constant values: TEMP_NORMAL = 98.6 • Names should be mnemonic (cost_of_living), but some short names (x, i) can be translated as-is from formulas to Matlab. • Conventions • i, j, k, m, n, : integers • x, y, : reals (decimal numbers)
2.4 Parsing • Need to break up program text into discrete symbols (tokens): foo=a+sqrt(32.1*b) foo = a + sqrt ( 32.1 * b ) • Book calls this parsing; but, strictly speaking, tokenizing (a.k.a. scanning) is only first step of parsing – parsing means turning program into a form that computer can understand.
2.4 Parsing: Rules • Blank (white) space (including tabs) delimits (separates) tokens, but is otherwise ignored sqrt ( 2 ) sqrt ( 2 ) sqrt ( 2 ) • A variable name must always be a single token (unlike file names in Windows!)
2.4 Parsing: Rules • Don't need whitespace between certain “atomic” tokens and other tokens: foo=a+3*(b+4) • A number is a single token 15 1.5 .15 -15.0 1.5e-3 % 1.5 * 10^-3 • A sequence of characters (incl. whitespace) inside single quotes, is a token, called a string: 'this is one token'
Help! • All Matlab operators have built-in help: >> 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: >> help levy_lab1 Simon Levy, CSCI 121, Jan. 04,2005 Lab 1: Polynomial Curve Fitting
What if you can’t remember the exact name? >>lookforcircle 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 . . .