HP 100 * MATLAB

Download Report

Transcript HP 100 * MATLAB

MATLAB FUNDAMENTALS:
INTRO TO LINEAR ALGEBRA
NUMERICAL METHODS
HP 101 – MATLAB
Wednesday, 11/5/2014
www.clarkson.edu/class/honorsmatlab
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
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
 Quick example…

(No dot!)
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.
Dot Product



1
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(θ)
B
A B=0
A B=2
B
A
2
1
2
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




Cross Product



A measure of the orthogonality “perpendicularness” of two vectors
Opposite of a dot product
A x B = ABsin(θ)
a = [ 1 2 0 ];
b = [ 3 4 0 ];
1
B
c = cross(a,b);
AxB=2
c = [ 0 0 -2 ]
A
AxB=0
B
2
1
2
Cross products are used a lot in
statics, dynamics, and fluids
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?”
Solving Systems Linear Equations

Suppose:
3x + 2y – z = 10
-x + 3y + 2z = 5
x - y - z = -1

We can say…. A*b = c
A =
Sizes:
3
2
-1
-1
3
2
1
-1
-1
3X3
x
b =
y
10
c =
5
z
-1
3X1
3X1
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) :


You can also view the solution in matrix form:


b = A\c
b = rref[A,c]
Notes:



No dot before the multiplier
Matrix inner dimensions must agree
Gaussian Elimination is more efficient than inv function
Matrix Functions

After you have a Dif EQ or a Linear Algebra
course, you can use MATLAB to:
 Multiply
or divide matrices together
 Raise a matrix to a power
 Find determinants
 Create identity Matrices
 Special matrices such as:
 pascal
a*b a/b
a^2
det( )
eye( )
( ) magic ( ) rosser ( )
 help(gallery)
for list of all special matrices
Numerical Methods
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!
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.
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.
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:
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!

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)
 x=0:0.1:3; y=x.^2; 0.1*trapz(y)
 x=0:0.01:3; y=x.^2; 0.01*trapz(y)
ans = 9.5
ans = 9.05
ans = 9.005
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 (a
secant) approaches
the tangent line as
the distance between
the points gets small.
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….
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.
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 above
equation until the
following is satisfied:
Epsilon is referred to as the error bound. It should
be determined by the function you are solving.
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:
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.