1 / 25

Computer Science 121

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 .

tamika
Download Presentation

Computer Science 121

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Scientific Computing Winter 2012 Chapter 2: Invoking a Computation Computer Science 121

  2. 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.

  3. 2.1 Expressions and Commands • The interpreter responds by evaluating the expression • Simplest example is a constant value: >> 4 ans = 4

  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?

  5. 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)

  6. 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.

  7. 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

  8. 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

  9. 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?

  10. 2.2 Changing State: Assignment >> Inf + 1 ans = Inf

  11. 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.

  12. 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

  13. 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;

  14. 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?

  15. Fibonacci Numbers

  16. Fibonacci Numbers

  17. 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

  18. Fibonacci Numbers

  19. 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

  20. 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)

  21. 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.

  22. 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!)

  23. 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'

  24. 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

  25. 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 . . .

More Related