Top Banner
Computer Science 121 Scientific Computing Winter 2014 Chapter 2: Invoking a Computation
25

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

Jan 12, 2016

Download

Documents

Preston Lamb
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Computer Science 121 Scientific Computing Winter 2014 Chapter 2: Invoking a Computation.

Computer Science 121

Scientific Computing

Winter 2014

Chapter 2: Invoking a Computation

Page 2: Computer Science 121 Scientific Computing Winter 2014 Chapter 2: Invoking a Computation.

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.

Page 3: Computer Science 121 Scientific Computing Winter 2014 Chapter 2: Invoking a Computation.

2.1 Expressions and Commands

● The interpreter responds by evaluating the expression

● Simplest example is a constant value:

>> 4

ans = 4

Page 4: Computer Science 121 Scientific Computing Winter 2014 Chapter 2: Invoking a Computation.

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?

Page 5: Computer Science 121 Scientific Computing Winter 2014 Chapter 2: Invoking a Computation.

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)

Page 6: Computer Science 121 Scientific Computing Winter 2014 Chapter 2: Invoking a Computation.

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

1 + 2 * 3 / 4

1 + ((2 * 3) / 4)

● So use parens if there's a reasonable

chance of ambiguity

• Programs should be written primarily to be read and understood by people.... – D. Knuth.

Page 7: Computer Science 121 Scientific Computing Winter 2014 Chapter 2: Invoking a Computation.

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 assigning values to variables:

>> altitude = 13000

>> temperature = 98.7

>> area = pi * radius^2

Page 8: Computer Science 121 Scientific Computing Winter 2014 Chapter 2: Invoking a Computation.

● Variable name must start with a letter or underscore

(_), and can then contain letters, numbers, and

underscores:

2.2 Changing State: Assignment

● More generally:

>> ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

= ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯

name expression

a

radius

cost_of_living

buffalo66

Page 9: Computer Science 121 Scientific Computing Winter 2014 Chapter 2: Invoking a Computation.

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?

Page 10: Computer Science 121 Scientific Computing Winter 2014 Chapter 2: Invoking a Computation.

2.2 Changing State: Assignment

>> Inf + 1

ans = Inf

Page 11: Computer Science 121 Scientific Computing Winter 2014 Chapter 2: Invoking a Computation.

2.2 Changing State: Assignment

●In addition to state, variables help us to

—write general formulas:

>> c = sqrt(a^2 + b^2)

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

Page 12: Computer Science 121 Scientific Computing Winter 2014 Chapter 2: Invoking a Computation.

2.2 Changing State: Assignment

2

3

)( 222 zyx

GmMxFx

● E.g., gravitational force F of moon at location (x, y, z) w.r.t.

center of earth:

all contain

2

3

)( 222 zyx

GmMyFy

2

3

)( 222 zyxGmMzFz

2

3

)( 222 zyxGmM

Page 13: Computer Science 121 Scientific Computing Winter 2014 Chapter 2: Invoking a Computation.

2.2 Changing State: Assignment

● 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;

all contain2

3

)( 222 zyxGmM

Page 14: Computer Science 121 Scientific Computing Winter 2014 Chapter 2: Invoking a Computation.

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?

Page 15: Computer Science 121 Scientific Computing Winter 2014 Chapter 2: Invoking a Computation.

Fibonacci Numbers

Page 16: Computer Science 121 Scientific Computing Winter 2014 Chapter 2: Invoking a Computation.

Fibonacci Numbers

Page 17: Computer Science 121 Scientific Computing Winter 2014 Chapter 2: Invoking a Computation.

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

Page 18: Computer Science 121 Scientific Computing Winter 2014 Chapter 2: Invoking a Computation.

Fibonacci Numbers

Page 19: Computer Science 121 Scientific Computing Winter 2014 Chapter 2: Invoking a Computation.

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

Page 20: Computer Science 121 Scientific Computing Winter 2014 Chapter 2: Invoking a Computation.

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)

Page 21: Computer Science 121 Scientific Computing Winter 2014 Chapter 2: Invoking a Computation.

● Need to break up program text into discrete symbols

(tokens):

foo=a+sqrt(32.1*b)

foo = a + sqrt ( 32.1 * b )

2.4 Parsing

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

Page 22: Computer Science 121 Scientific Computing Winter 2014 Chapter 2: Invoking a Computation.

● Blank (white) space (including tabs) delimits

(separates) tokens, but is otherwise ignored

sqrt ( 2 )

sqrt ( 2 )

sqrt ( 2 )

2.4 Parsing: Rules

● A variable name must always be a single token (unlike

file names in Windows!)

Page 23: Computer Science 121 Scientific Computing Winter 2014 Chapter 2: Invoking a Computation.

● 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.01.5e-3 % 1.5 * 10^-3

2.4 Parsing: Rules

● A sequence of characters (incl. whitespace) inside

single quotes, is a token, called a string:

'this is one token'

Page 24: Computer Science 121 Scientific Computing Winter 2014 Chapter 2: Invoking a Computation.

Help!

● 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

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

Page 25: Computer Science 121 Scientific Computing Winter 2014 Chapter 2: Invoking a Computation.

What if you can’t remember the

exact name?>> 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

. . .