Presentation is loading. Please wait.

Presentation is loading. Please wait.

MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7.

Similar presentations


Presentation on theme: "MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7."— Presentation transcript:

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

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

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

4 Week 1 Basics

5 Ok – Cast your minds back We are first faced with a text prompt:

6 Basic Math We can create our own variables and apply basic algebraic operations on these variables. Basic operations include addition (+), multiplication (*), subtraction (-), division (/ or \).

7 a = 1 b = a/2 c = b*3 Do the math and: b = a/2 = ½ c = b*3 = 1.5

8 Basic Arrays (Matrices) We can create one- or two-dimensional variables of arbitrary size. We can then apply basic linear algebra operations on these.

9 Example 2 A is a matrix with 3 rows and 2 columns.

10 Matrix Addition in Matlab

11 Matrix Subtraction in Matlab

12 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

13 Example 4 (matrix multiplication)

14 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

15 Example of Function of Vector

16 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 into polar coordinates. We can create a text file with the following text. It can be called like any built in function.

17 Custom Built Function For Matlab Make sure you use cd in Matlab to change to the directory containing myfunc.m the arguments to myfunc could also have been matrices – leading to two matrices being output.

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

19 Loops in Matlab One variant of Matlab loop syntax is: for var=start:end commands; end

20 Example of a Matlab Loop Say I want to add the numbers from 1 to 10, without using the Matlab intrinsic sum.

21 Week 2 Plotting Finite precision effects

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

23 Example 5 (figure created by Matlab)

24 Adding Titles, Captions, Labels, Multiple Plots

25

26 subplot If you wish to create a figure with two sub-figures you can use the subplot function: subplot(1,2,1) requests 1 row of figures 2 columns of figures 1 st figure

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

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

29 A Convergent Binary Representation of Any Number Between 0 and 1 a similar representation in base 10: Volunteer ?

30 Finite Binary Approximations of a Real Number We can easily show that T N is bounded as: (think of Zeno’s paradox)

31 Monster #1 Consider: What should its behavior be as: Use subplots for the following 2 plots 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

32 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 region where 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

33 Monster #2 Consider: What should its behavior be as:

34 As x increases past ~=36 we see that f drops to 0 !! Monster #2 cont (finite precision effects from large*(1+small) )

35 Limit of

36 Consider: What should its behavior be as: Monster #4

37 Monster 4 cont Behavior as delta  0 : or if you are feeling lazy use the definition of derivative, and remember: d(sin(x))/dx = cos(x)

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

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

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

41 Monster 4 cont ( parameter differentiation, delta=1e-15) When we make the delta around about machine precision we see O(1) errors !. Bad

42 Approximate Explanation of Monster #4 1) Taylor’s thm: 2) Round off errors 3) Round off in computation of f and x+delta 4) Put this together:

43 i.e. for or equivalently approximation error decreases as delta decreases in size. BUT for round off dominates!.

44 Week 3 & 4 We covered taking approximate derivatives in Matlab and manipulating images as matrices.

45 Week 5 Approximation of the solution to ordinary differential equations. Adams-Bashforth schemes. Runge-Kutta time integrators.

46 Ordinary Differential Equation Example: 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

47 Forward Euler Numerical Scheme Numerical scheme: Discrete scheme: where:

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

49 Application: Newtonian Motion

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

51 Numerical Scheme For m=1 to FinalTime/dt For n=1 to number of objects End For n=1 to number of objects End

52 AB Schemes Essentially we use interpolation and a Newton-Cotes quadrature formula to formulate:

53 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):

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

55 Week 6 Colliding disks project

56 Week 7 Matrix inverse and what can go wrong. Solving lower and upper triangular systems. LU factorization and partial pivoting.

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

58 Examples If what is If A is an NxN matrix how can we calculate its inverse ?

59 Example cont The inverse of A can be calculated as: Now let’s see how well this exact solution works in Matlab:

60 Example cont With this formulation of the product of A and its inverse only satisfies the definition to 6 decimal places for delta=0.001


Download ppt "MA/CS 375 Fall 2002 Lecture Summary Week 1  Week 7."

Similar presentations


Ads by Google