ma/cs 375 fall 2002 lecture summary week 1 week 7

60
MA/CS 375 Fall 2002 Lecture Summary Week 1 Week 7

Upload: preston-haynes

Post on 13-Dec-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

MA/CS 375

Fall 2002

Lecture

Summary Week 1 Week 7

Page 2: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Theoretical Exercise

• Say we wish to design a self-playing computer game like asteroids.

• The player controls a rocket.

• There are enemy rockets who can shoot torpedoes and who can ram the player.

• There are moving asteroids of fairly arbitrary shape.

• Let’s work through the details involved……

Page 3: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Review

• We are going to do a fast review from basics to conclusion.

Page 4: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Week 1

• Basics

Page 5: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Ok – Cast your minds back

We are first faced with a text prompt:

Page 6: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Basic Math

We can create our own variables and apply basicalgebraic operations on these variables.

Basic operations include addition (+), multiplication (*), subtraction (-), division (/ or \).

Page 7: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

a = 1

b = a/2

c = b*3

Do the math and:

b = a/2 = ½

c = b*3 = 1.5

Page 8: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Basic Arrays (Matrices)

• We can create one- or two-dimensional variables of arbitrary size.

• We can then apply basic linear algebra operations on these.

Page 9: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Example 2

A is a matrix with 3 rows and 2 columns.

2.1 3.23

4.12 1.893

7.1 5

A

Page 10: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Matrix Addition in Matlab

2 1

3 2

4 2

4 1

A

B

C A B

Page 11: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Matrix Subtraction in Matlab

2 1

3 2

4 2

4 1

A

B

C A B

Page 12: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Matrix Multiplication

• There is a specific definition of matrix multiplication.

• In index notation:

• i.e. for the (i,j) of the result matrix C we take the i’th row of A and multiply it, entry wise, with the j’th column of B

1

NcolsA

ij ik kjk

C A B

C A B

Page 13: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Example 4 (matrix multiplication)

2 1

3 2

4 2

4 1

A

B

C A B

Page 14: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Functions in Matlab

• Matlab has a number of built-in functions:– cos, sin, tan, acos, asin, atan– cosh, sinh, tanh, acosh, asinh, atanh– exp, log, sqrt

• They all take matrices as arguments and return matrices of the same dimensions.• e.g. cos([1 2 3])

• For other functions type: > help matlab\elfun

Page 15: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Example of Function of Vector

Page 16: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Custom-Made Matlab Functions

function [ radius, theta] = myfunc( x, y)

% this is a comment, just like // in C++ % now create and evaluate theta (in radians) theta = atan2(y,x); % now create and evaluate radius radius = sqrt( x.^2 + y.^2);

function end

Say we wish to create a function that turns Cartesian coordinates intopolar coordinates. We can create a text file with the following text. Itcan be called like any built in function.

Page 17: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Custom Built Function For Matlab

• Make sure you use cdin Matlab to change to the directory containing myfunc.m

• the arguments to myfunccould also have been matrices – leading to two matrices being output.

Page 18: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Matlab as Programming Language

• We can actually treat Matlab as a coding language.

• It allows script and/or functions.

• Loops are allowed, but since Matlab is an interpreted language, their use can lead to slow code.

Page 19: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Loops in Matlab

One variant of Matlab loop syntax is:

for var=start:end commands; end

Page 20: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Example of a Matlab Loop

• Say I want to add the numbers from 1 to 10, without using the Matlab intrinsic sum.

Page 21: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Week 2

• Plotting

• Finite precision effects

Page 22: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Plotting

• Recall we can create a function of a vector.

• Then plot the vector as one ordinate and the function of the vector as the other ordinate.

Page 23: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Example 5(figure created by Matlab)

Page 24: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Adding Titles, Captions, Labels, Multiple Plots

Page 25: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7
Page 26: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

subplot• If you wish to create a figure with two

sub-figures you can use the subplot function:

• subplot(1,2,1) requests1 row of figures2 columns of figures1st figure

Page 27: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

subplot(1,2,1) subplot(1,2,2)

Page 28: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Starting Numerics

• We next considered some limitations inherent in fixed, finite-precision representation of floating point numbers.

Page 29: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

A Convergent Binary Representation of Any Number

Between 0 and 1

1

1 where m 1 or 0

2

n

n nnn

m m

a similar representation in base 10:

1

1 where 0,1,2,3,4,5,6,7,8 or 9

10

n

n nnn

d d d

Volunteer ?

Page 30: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Finite Binary Approximations of a Real Number

N1

1 +T where 1 or 0

2

n N

n nnn

m m m

We can easily show that TN is bounded as:(think of Zeno’s paradox)

1

2N NT

Page 31: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Monster #1• Consider:

• What should its behavior be as:

• Use subplots for the following 2 plots

• Plot this function at 1000 points in:

• Plot this function at 1000 points in:

• Label everything nicely, include your name in the title.

• In a text box explain what is going on, print it out and hand it in

1 1xf x

x

0x

1,1x

4 ,4

Page 32: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

when we zoom in we see that the large+small operation is introducing order eps errors which we then divide with eps to get O(1) errors !.

Each stripe is a regionwhere 1+ x is a constant(think about the gaps between

numbers in finite precision)

Then we divide by x and the stripes look like hyperbola.

The formula looks like (c-1)/x with a new c for each stripe.

Monster #1 1 1xf x

x

0x

Page 33: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Monster #2

• Consider:

• What should its behavior be as:

log(1 )x xf x e e

x

Page 34: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

As x increases past ~=36 we see that f drops to 0 !!

Monster #2 cont(finite precision effects from large*(1+small) )

Page 35: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Limit of log(1 )x xf x e e

log 1lim lim log 1 lim

1lim by l'Hopital's rule

1lim 1

1

x

x xxx x x

x

x

xx

xx

ef x e e

e

eee

e

Page 36: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

• Consider:

• What should its behavior be as:

sin sincos

x xg x x

0

Monster #4

Page 37: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Monster 4 cont

0 0

0

sin sin sin cos cos( )sin sinlim lim

sin cos 1 cos( )sinlim

cos

x x x x x

x x

x

Behavior as delta 0 :

or if you are feeling lazy use the definition of derivative, and remember: d(sin(x))/dx = cos(x)

Page 38: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Monster 4 cont(parameter differentiation, delta=1e-4)

OK

Page 39: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Monster 4 cont (parameter differentiation, delta=1e-7)

OK

Page 40: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Monster 4 cont (parameter differentiation, delta=1e-12)

Worse

Page 41: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Monster 4 cont (parameter differentiation, delta=1e-15)

When we make the delta around about machine precision we see O(1) errors !.

Bad

Page 42: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Approximate Explanation of Monster #4

2

2

'

ˆˆ ˆ

( )

ˆ ˆ ˆ ( )

( )

ˆ ˆˆ ˆ

'

f x f x f x O

y x

x O

f y f y O

f x O

f y f x f y f x O

f x O O

1) Taylor’s thm:

2) Round off errors

3) Round off in computation of f and x+delta

4) Put this together:

Page 43: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

2ˆ ˆˆ ˆ '

ˆ ˆˆ ˆ

f y f x f x O O

f y f xO O

i.e. for or equivalently approximation error decreases as delta decreases in size.

BUT for round off dominates!.

810

810

Page 44: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Week 3 & 4

• We covered taking approximate derivatives in Matlab and manipulating images as matrices.

Page 45: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Week 5

• Approximation of the solution to ordinary differential equations.

• Adams-Bashforth schemes.

• Runge-Kutta time integrators.

Page 46: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Ordinary Differential Equation

• Example:

0

?

u a

duu

dt

u T

• t is a variable for time • u is a function dependent on t• given u at t = 0 • given that for all t the slope of us is –u• what is the value of u at t=T

Page 47: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Forward Euler Numerical Scheme

• Numerical scheme:

• Discrete scheme:

1n nnu uu

t

1 1n nu t u where: approximate solution at t=n tnu

Page 48: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Summary of dt Stability

• 0 < dt <1 stable and convergent since as dt 0 the solution approached the actual solution.

• 1 <= dt < 2 bounded but not cool.

• 2 <= dt exponentially growing, unstable and definitely not cool.

Page 49: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Application: Newtonian Motion

Page 50: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

N-Body Newtonian Gravitation Simulation

• Goal: to find out where all the objects are after a time T

• We need to specify the initial velocity and positions of the objects.

• Next we need a numerical scheme to advance the equations in time.

• Can use forward Euler…. as a first approach.

Page 51: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Numerical Scheme

13

1,2

1

m mi Ni n im m

n nm m

i i n i n

m m mn n n

M Gdt

dt

x - x

v vx x

x x v

For m=1 to FinalTime/dt For n=1 to number of objects

End For n=1 to number of objects

EndEnd

Page 52: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

AB Schemes Essentially we use

interpolation and a Newton-Cotes quadrature formula to formulate:

1 1

1 1 2

1

1

1 2 3

1

Linear:

Quadratic:

3

2 2Cubic:

23 16 5

12 12 12Quartic:

55 59 37 9

24 24 24 24Quintic:

1901 2774

720 7

n n n n

n n n n n

n n n n n n

n n n

n n n

t ty y f f

t t ty y f f f

t t t ty y f f f

y y t f

f

t ty y f

1 2 3 42616 1274 251

20 720 720 720n n n nt t tf f f f

Page 53: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Runge-Kutta Schemes• See van Loan for derivation of RK2 and

RK4.

• I prefer the following (simple) scheme due to Jameson, Schmidt and Turkel (1981):

n

1

y

for : 1:1

y=y

n

n

y

k s

tf y

kend

y y

Page 54: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Runge-Kutta Schemes• Beware, it only works when f is a function

of y and not t

• here s is the order of the scheme.

n

1

y

for : 1:1

y=y

n

n

y

k s

tf y

kend

y y

Page 55: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Week 6

• Colliding disks project

Page 56: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Week 7

• Matrix inverse and what can go wrong.

• Solving lower and upper triangular systems.

• LU factorization and partial pivoting.

Page 57: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Recall

• Given a matrix M then if it exists the inverse of a matrix M-1 is that matrix which satisfies:

1 0 0

0 1 0

0 0 1

1 1M M MM

Page 58: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Examples

• If what is

• If what is

• If A is an NxN matrix how can we calculate its inverse ?

2 4

1 3

A 1A

1 2 3 4

3 1 3 2

5 2 1 4

2 4 1 3

A 1A

Page 59: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Example cont

• The inverse of A can be calculated as:

• Now let’s see how well this exact solution works in Matlab:

1

11

A2

1

3 2

1 1

1 1

A

Page 60: MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7

Example cont1

11

A

21

3 2

1 1

1 1

A

With this formulation of the product of A andits inverse only satisfiesthe definition to 6 decimalplaces for delta=0.001

1A