MIT6 094IAP10 Lec03
MIT6 094IAP10 Lec03
MIT6 094IAP10 Lec03
094
Danilo epanovi
IAP 2008
Homework 2 Recap
How long did it take?
Using min with matrices:
a=[3 7 5;1 9 10; 30 -1 2];
b=min(a); % returns the min of each column
m=min(b); % returns min of entire a matrix
m=min(min(a)); % same as above
m=min(a(:)); % makes a a vector, then gets min
Common mistake:
[m,n]=find(min(a)); % think about what happens
How to make and run a function: save the file, then call it
from the command window like any other function. No need
to 'compile' or make it official in any other way
Outline
(1)
(2)
(3)
(4)
(5)
Linear Algebra
Polynomials
Optimization
Differentiation/Integration
Differential Equations
Matrix Decompositions
MATLAB has built-in matrix decomposition methods
The most common ones are
[V,D]=eig(X)
Eigenvalue decomposition
[U,S,V]=svd(X)
Singular value decomposition
[Q,R]=qr(X)
QR decomposition
x + 4 y = 34
3 x + y = 2
System 2:
2x 2 y = 4
x + y = 3
3x + 4 y = 2
x + 4 y = 34
3 x + y = 2
System 2:
2x 2 y = 4
x + y = 3
3x + 4 y = 2
x1=A\b;
gives least squares solution
error=abs(A*x1-b)
Outline
(1)
(2)
(3)
(4)
(5)
Linear Algebra
Polynomials
Optimization
Differentiation/Integration
Differential Equations
Polynomials
Many functions can be well described by a high-order
polynomial
MATLAB represents a polynomials by a vector of coefficients
if vector P describes a polynomial
ax3+bx2+cx+d
P(1)
P(2)
P(3)
P(4)
Polynomial Operations
P is a vector of length N+1 describing an N-th order polynomial
To get the roots of a polynomial
r=roots(P)
r is a vector of length N
Polynomial Fitting
MATLAB makes it very easy to fit polynomials to data
Given data vectors X=[-1 0 2] and Y=[0 -1 3]
p2=polyfit(X,Y,2);
finds the best second order polynomial that fits the points
(-1,0),(0,-1), and (2,3)
see help polyfit for more information
y = x2
for x=-4:0.1:4.
Outline
(1)
(2)
(3)
(4)
(5)
Linear Algebra
Polynomials
Optimization
Differentiation/Integration
Differential Equations
Minimizing a Function
fminbnd: minimizing a function over a bounded interval
x=fminbnd('myfun',-1,2);
myfun takes a scalar input and returns a scalar output
myfun(x) will be the minimum of myfun for -1x 2
Anonymous Functions
You do not have to make a separate function file
x=fzero(@myfun,1)
What if myfun is really simple?
function to evaluate
x=fminbnd(@(x) (cos(exp(x))+x^2-1),-1,2);
Optimization Toolbox
If you are familiar with optimization methods, use the
optimization toolbox
Useful for larger, more structured optimization problems
Sample functions (see help for more info)
linprog
linear programming using interior point methods
quadprog
quadratic programming solver
fmincon
constrained nonlinear optimization
Exercise: Min-Finding
Find the minimum of the function f ( x ) = cos ( 4 x ) sin (10 x ) e
over the range to . Use fminbnd.
Plot the function on this range to check that this is the
minimum.
Exercise: Min-Finding
Find the minimum of the function f ( x ) = cos ( 4 x ) sin (10 x ) e
over the range to . Use fminbnd.
Plot the function on this range to check that this is the
minimum.
Make the following function:
function y=myFun(x)
y=cos(4*x).*sin(10*x).*exp(-abs(x));
Find the minimum in the command window:
x0=fminbnd('myFun',-pi,pi);
Plot to check if it's right
figure; x=-pi:.01:pi; plot(x,myFun(x));
Outline
(1)
(2)
(3)
(4)
(5)
Linear Algebra
Polynomials
Optimization
Differentiation/Integration
Differential Equations
Numerical Differentiation
1
-0.2
-0.4
-0.6
-0.8
-1
100
200
300
400
500
600
first difference of mat along the 2nd dimension, dm=[2 2;4 -2]
see help for more details
The opposite of diff is the cumulative sum cumsum
2D gradient
[dx,dy]=gradient(mat);
700
Numerical Integration
MATLAB contains common integration methods
Adaptive Simpson's quadrature (input is a function)
q=quad('myFun',0,10);
q is the integral of the function myFun from 0 to 10
q2=quad(@(x) sin(x)*x,0,pi)
q2 is the integral of sin(x)*x from 0 to pi
z2=trapz(x,sqrt(exp(x))./x)
x
z2 is the integral of e x from 0 to pi
Outline
(1)
(2)
(3)
(4)
(5)
Linear Algebra
Polynomials
Optimization
Differentiation/Integration
Differential Equations
ode45
High order (Runge-Kutta) solver. High accuracy and
reasonable speed. Most commonly used.
ode15s
Stiff ODE solver (Gear's algorithm), use when the diff eq's
have time constants that vary by orders of magnitude
Initial conditions
Time range
Inputs:
ODE function name (or anonymous function). This function
takes inputs (t,y), and returns dy/dt
Time interval: 2-element vector specifying initial and final
time
Initial conditions: column vector with an initial condition for
each ODE. This is the first input to the ODE function
Outputs:
t contains the time points
y contains the corresponding values of the integrated
variables.
ODE Function
The ODE function must return the value of the derivative at
a given time and function value
Example: chemical reaction
10
Two equations
dA
= 10 A + 50 B
dt
dB
= 10 A 50 B
dt
B
50
ODE file:
y has [A;B]
dydt has
[dA/dt;dB/dt]
plot(t,y(:,1),'k','LineWidth',1.5);
hold on;
plot(t,y(:,2),'r','LineWidth',1.5);
legend('A','B');
xlabel('Time (s)');
ylabel('Amount of chemical (g)');
title('Chem reaction');
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
0.05
0.1
0.15
0.2
0.25
0.3
Time (s)
0.35
0.4
0.45
0.5
&& +
g
sin ( ) = 0
L
&& =
g
sin ( )
L
let & =
& =
g
sin ( )
L
v
x=
v
dx &
=
dt &
Courtesy of The MathWorks, Inc. Used with permission.
plot(t,x(:,1));
hold on;
plot(t,x(:,2),'r');
legend('Position','Velocity');
8
Position
Velocity
Position in terms of
angle (rad)
Velocity (m/s)
4
2
0
-2
-4
-6
-8
10
Velocity is greatest
when theta=0
Velocity
2
0
-2
Velocity=0 when
theta is the greatest
-4
-6
-8
-3
-2
-1
0
Position
Exercise: ODE
Use ode45 to solve for y ( t ) on the range t=[0 10], with
initial condition y ( 0 ) = 10 and dy dt = t y 10
Plot the result.
Exercise: ODE
Use ode45 to solve for y ( t ) on the range t=[0 10], with
initial condition y ( 0 ) = 10 and dy dt = t y 10
Plot the result.
Make the following function
function dydt=odefun(t,y)
dydt=-t*y/10;
Integrate the ODE function and plot the result
[t,y]=ode45(odefun,[0 10],10);
Alternatively, use an anonymous function
[t,y]=ode45(@(t,y) t*y/10,[0 10],10);
Plot the result
plot(t,y);xlabel('Time');ylabel('y(t)');
Exercise: ODE
The integrated function looks like this:
Function y(t), integrated by ode45
10
9
8
y(t)
5
4
3
2
5
Time
10
End of Lecture 3
(1)
(2)
(3)
(4)
(5)
Linear Algebra
Polynomials
Optimization
Differentiation/Integration
Differential Equations
We're almost done!
MIT OpenCourseWare
http://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.