matlab fundamentals: intro to linear algebra numerical methods hp 101 – matlab wednesday,...

23
MATLAB FUNDAMENTALS: INTRO TO LINEAR ALGEBRA NUMERICAL METHODS HP 101 – MATLAB Wednesday, 11/5/2014 www.clarkson.edu/class/honorsmatlab

Upload: della-hoover

Post on 11-Jan-2016

244 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: MATLAB FUNDAMENTALS: INTRO TO LINEAR ALGEBRA NUMERICAL METHODS HP 101 – MATLAB Wednesday, 11/5/2014

MATLAB FUNDAMENTALS:INTRO TO LINEAR ALGEBRANUMERICAL METHODS

HP 101 – MATLABWednesday, 11/5/2014

www.clarkson.edu/class/honorsmatlab

Page 2: MATLAB FUNDAMENTALS: INTRO TO LINEAR ALGEBRA NUMERICAL METHODS HP 101 – MATLAB Wednesday, 11/5/2014

Quote and Video of The Week “It’s a great party house guys… that’s

why we bought it.” Jon Goss

https://www.youtube.com/watch?v=CwhvJ5B4lYg

https://www.youtube.com/watch?v=NBbHRaNNBuY

Page 3: MATLAB FUNDAMENTALS: INTRO TO LINEAR ALGEBRA NUMERICAL METHODS HP 101 – MATLAB Wednesday, 11/5/2014

Linear (Matrix) Algebra Intro

Math with matrices is a bit different from regular math…

Addition and subtraction are the same. Multiplication (“Row Column Rule”)

Syntax: A*B (No dot!) Quick example…

Division and Powers… well, they don’t really exist in the classical sense. MATLAB can compute them, but we won’t worry about what they mean.

Page 4: MATLAB FUNDAMENTALS: INTRO TO LINEAR ALGEBRA NUMERICAL METHODS HP 101 – MATLAB Wednesday, 11/5/2014

Dot Product

Dot Product is amount of vector A that lies in plane with vector B

Essentially, the sum of the results when you multiply two vectors together element by element

A•B = ABcos(θ)

A

B A B = 0 B A B = 2

1

2 1 2

Page 5: MATLAB FUNDAMENTALS: INTRO TO LINEAR ALGEBRA NUMERICAL METHODS HP 101 – MATLAB Wednesday, 11/5/2014

Dot Product Example

a = [ 1 2 3]; b = [ 4 5 6]; y = a.*b y = [ 4 10 18] dot_product = sum(y) = 32

d_p = dot(a,b) = 32

Vectors must be same length – rows or columns

Page 6: MATLAB FUNDAMENTALS: INTRO TO LINEAR ALGEBRA NUMERICAL METHODS HP 101 – MATLAB Wednesday, 11/5/2014

Cross Product

A measure of the orthogonality “perpendicular-ness” of two vectors

Opposite of a dot product A x B = ABsin(θ)

A

B A x B = 2

B A x B = 0

1

2

1 2

a = [ 1 2 0 ];

b = [ 3 4 0 ];

c = cross(a,b);

c = [ 0 0 -2 ]

Cross products are used a lot in statics, dynamics, and fluids

Page 7: MATLAB FUNDAMENTALS: INTRO TO LINEAR ALGEBRA NUMERICAL METHODS HP 101 – MATLAB Wednesday, 11/5/2014

Systems of Linear Equations

Linear equations are big, nasty, and algebra can be really tricky

Sometimes, you can have tons of equations but actually solving them all by hand would be painful

Examples: Complex Circuits Simple Traffic Flow (or other flows) “Who bought how much candy?”

Page 8: MATLAB FUNDAMENTALS: INTRO TO LINEAR ALGEBRA NUMERICAL METHODS HP 101 – MATLAB Wednesday, 11/5/2014

Suppose:3x + 2y – z = 10

-x + 3y + 2z = 5

x - y - z = -1

We can say…. A*b = c

A = b = c = 10

5

-1

Solving Systems Linear Equations

3 2 -1

-1 3 2

1 -1 -1

x

y

z

Sizes: 3 X 3 3 X 1 3 X 1

Page 9: MATLAB FUNDAMENTALS: INTRO TO LINEAR ALGEBRA NUMERICAL METHODS HP 101 – MATLAB Wednesday, 11/5/2014

Example Continued

A = [3 2 -1; -1 3 2; 1 -1 -1]; c = [10; 5; -1]; b = A^(-1) * c; or b=inv(A)*c Alternatively, can use Gaussian Elimination

(preferred) : b = A\c

You can also view the solution in matrix form: b = rref[A,c]

Notes: No dot before the multiplier Matrix inner dimensions must agree Gaussian Elimination is more efficient than inv

function

Page 10: MATLAB FUNDAMENTALS: INTRO TO LINEAR ALGEBRA NUMERICAL METHODS HP 101 – MATLAB Wednesday, 11/5/2014

Matrix Functions

After you have a Dif EQ or a Linear Algebra course, you can use MATLAB to: Multiply or divide matrices togethera*b a/b

Raise a matrix to a power a^2 Find determinants det( )

Create identity Matrices eye( )

Special matrices such as: pascal ( ) magic ( ) rosser ( )

help(gallery) for list of all special matrices

Page 11: MATLAB FUNDAMENTALS: INTRO TO LINEAR ALGEBRA NUMERICAL METHODS HP 101 – MATLAB Wednesday, 11/5/2014

Numerical Methods

Page 12: MATLAB FUNDAMENTALS: INTRO TO LINEAR ALGEBRA NUMERICAL METHODS HP 101 – MATLAB Wednesday, 11/5/2014

So What are Numerical Methods? A tool for efficiently solving complex

mathematical problems. Discrete methods which repeat

calculations until the correct answer is obtained.

Very complex… we will only touch the surface. For more, you should consider taking the Numerical Methods course in the math department.

Really, really cool!

Page 13: MATLAB FUNDAMENTALS: INTRO TO LINEAR ALGEBRA NUMERICAL METHODS HP 101 – MATLAB Wednesday, 11/5/2014

Discretization

Numerical methods approximate solutions by treating continuous functions as a series of points.

Fortunately, this is how MATLAB works! Your “step” (often a time-step) is very

important. The smaller it is, the more accurate your results will be but the slower your program will run.

Page 14: MATLAB FUNDAMENTALS: INTRO TO LINEAR ALGEBRA NUMERICAL METHODS HP 101 – MATLAB Wednesday, 11/5/2014

Iterative Methods

Numerical methods also require repeating calculations until the correct answer is found.

This can be accomplished in a few easy ways… Loops which use a value or values from a

previous run through the loop. Loops which call a function and use one or

more of its outputs as its inputs in the next loop.

Functions which recursively call themselves, giving information to the next level and passing information back down. These can be hard to conceptualize.

Page 15: MATLAB FUNDAMENTALS: INTRO TO LINEAR ALGEBRA NUMERICAL METHODS HP 101 – MATLAB Wednesday, 11/5/2014

Why are Numerical Methods Useful? First and foremost, they are very fast… You may have noticed that using

symbolics is pretty slow in MATLAB. If you have a large number of calculations to do, this can add up quickly.

Using numerical methods to solve equations, or to find say, the roots of an equation is much faster.

Some equations simply can’t be solved explicitly…

eg:

Page 16: MATLAB FUNDAMENTALS: INTRO TO LINEAR ALGEBRA NUMERICAL METHODS HP 101 – MATLAB Wednesday, 11/5/2014

Numerical Integration

If we think of an integral as the area beneath a curve, we can approximate it in a few ways… By taking two discrete

points, we can calculate the area of the trapezoid formed by them and the axis.

Then we can add up all the trapezoids to get the total area.

If the distance between our points is small enough, the solution is almost exact!

Page 17: MATLAB FUNDAMENTALS: INTRO TO LINEAR ALGEBRA NUMERICAL METHODS HP 101 – MATLAB Wednesday, 11/5/2014

Numerical Integration

That sounds like a bit of a pain… we could do it, but is there an easier way?

Yes! Because the MATLAB developers love you so much, they included the trapz function!

Note that trapz assumes a spacing of 1 between your elements. So we need to multiply the result by the spacing… Lets try some: x=0:1:3; y=x.^2; 1*trapz(y) ans = 9.5 x=0:0.1:3; y=x.^2; 0.1*trapz(y) ans = 9.05 x=0:0.01:3; y=x.^2; 0.01*trapz(y) ans = 9.005

Page 18: MATLAB FUNDAMENTALS: INTRO TO LINEAR ALGEBRA NUMERICAL METHODS HP 101 – MATLAB Wednesday, 11/5/2014

Numerical Differentiation

Differentiation gives the instantaneous slope of a function at a given point. We can come very close to that exact value by looking at a very small piece of the curve and treating it as a straight line…

This straight line (asecant) approaches the tangent line as the distance betweenthe points gets small.

Page 19: MATLAB FUNDAMENTALS: INTRO TO LINEAR ALGEBRA NUMERICAL METHODS HP 101 – MATLAB Wednesday, 11/5/2014

Numerical Differentiation

The formula for numerical differentiation is…

Where f(x) is some function and h is the step.

Lets find the derivative of sine at π/3: y1=sin(pi/3); y2=sin(pi/3+0.01); y_prime=(y2-y1)/0.01 ans = 0.4957 (The exact answer is .5) Using a step of 0.001 instead gives 0.4996….

Page 20: MATLAB FUNDAMENTALS: INTRO TO LINEAR ALGEBRA NUMERICAL METHODS HP 101 – MATLAB Wednesday, 11/5/2014

Newton’s Method

Newton’s Method is useful to find a root or roots of an equation.

It requires knowledge of the function, the derivative of the function (numerical differentiation can come in handy here) and an initial guess of the answer.

It can be a bit picky about the initial guess… the iteration can get stuck “bouncing” or it can converge to a root which is not desired.

Good judgment is required on a case-by-case basis to determine if the method is appropriate and what the initial guess should be.

Page 21: MATLAB FUNDAMENTALS: INTRO TO LINEAR ALGEBRA NUMERICAL METHODS HP 101 – MATLAB Wednesday, 11/5/2014

Newton’s Method

Newton’s Method uses the derivative of at an initial guess function to evaluate a new guess for the root of the function until the root is found.

We iterate the aboveequation until thefollowing is satisfied:

Epsilon is referred to as the error bound. It should be determined by the function you are solving.

Page 22: MATLAB FUNDAMENTALS: INTRO TO LINEAR ALGEBRA NUMERICAL METHODS HP 101 – MATLAB Wednesday, 11/5/2014

Example of Newton’s Method How calculators do division: We want to compute x=1/d without

using a division operator… lets try Newton’s Method!

First, we need to rewrite our equation in a manner that the solution is zero when we find d:

Next, we need the derivative: Now we can plug these into the

Newton’s Method formula:

Page 23: MATLAB FUNDAMENTALS: INTRO TO LINEAR ALGEBRA NUMERICAL METHODS HP 101 – MATLAB Wednesday, 11/5/2014

Continuing the Example…

Please go to the course website and download the skeleton code for the example.

We’ll go through it together. After class a detailed and highly commented version will be posted on the website with the skeleton code.