Class Notes: July 28, 2003 through August 1, 2003
CS 314
Optimization (continued from Friday's lecture)
Minimization using DerivativesAssume we want to minimize f(x) and it has a unique minimum at p. where a <= p <= b and we start the search at p0.
If f'(p0) > 0, p is to the left of p0.
If f'(p0) < 0, p is to the right of p0.
If the derivative of f(x) is available, then we can use any of the methods we've covered to solve non-linear equations to solve f'(x) = 0. (e.g. Regula-falsi, bisection, Newton Raphson, Secant, etc.)
Example:
f(x) = sin(x). Find the minimum in the interval [-3pi/4, 0]. Use
bisection on f'(x).
The answer is -1.5708
a |
f'(a) | b |
f'(b) |
c | f'(c) |
-2.356 | -.7071 | 0 | 1 | -1.1781 | .3827 |
-2.356 | -.7071 | -1.1781 | .3827 | -1.7672 | -.1951 |
-1.7672 | -.1951 | -1.1781 | .3827 | -1.4727 | .0980 |
You could do numerical derivation if the derivative is hard to find. Choose h carefully. Any method to solve f(x) = 0 can be used here.
Finding a Minimum for Multiple Variables
Steepest Descent or Gradient Method
Assume we want to minimize f(x) of n variables where x = (x1,
x2, ..., xn). The gradient,
▼f(x), is a vector function defined as:
grad( f(x) ) = (df/dx1, df/dx2, ..., df/dxn)
From the concept of gradient, we know that the gradient vector points in the
directoin of the greatest increase of f(x). Evaluate at a point to
get the vector which points in the direction of greatest change. Since we
are looking for the minimum, we will use the negative of the gradient which will
point in the direction of greatest decrease.
Basic Gradient Method
Start at point p0 and move along the line in the direction ▼f(x)
a small increment. That is going to take you closer to the minimum.
p1 = p0 - ▼f(p0)*h
h is the small increment
pk = pk-1 - ▼f(pk-1)*h
When we reach p, the value of the gradient will be zero.
Example:
f(x) = x2+1
f'(x) = 2xi, where i is a unit vector in the x-direction
Let h = 0.1, x0=-1
f(x0) |
= 2*-1*i = -2i |
f(x1) | = -1 - 2*-1*0.1 = -0.8 |
f(x2) | = -0.8 - 2*-0.8*0.1 = -0.64 |
f(x3) | = -0.512 |
f(x4) | = -0.4096 |
f(x5) | = -0.32768 |
Numerical Solution of Differential Equations
Introduction
Example of a differential equation:
y' = k*y
dy/dt = k*y
dy = k*y dt
∫dy/y = ∫k dt
ln(y) = kt + k2
y = ekt + k2
y = k3ekt
Very often, differential equations do not have an analytic solution so they have to be approximated using numerical methods.
Euler's Method
Let [a,b] be the interval which we want to find the solution of y' = f(y,t)
with y(a) = y0. We will find a set of points: (t0,y0),
..., (tn,yn) that are used to approximate y(t) = y(tk).
Also, tk= a +k*h (where h is the step size) for k = {0,1,2,...,m}
over [t0, t1, ..., tm] with y(t0) =
0.
Using Taylor's Expansion to approximate y(t) around t0, we have:
y(t) = y(t0) + y'(t0)*(t-t0)
+ y''(c)*(t-t0)2/2
We use this equation to obtain y(t1):
y(t1) = y(t0) +
y'(t0)*(t-t0) + E(t)
If the step size is small enough, we can neglect the second order error.
y1 = y0 +
h*y'(t0)
y1 = y0 + h*f(t0,y0)
In general, tk+1 = tk +h
yk+1 = yk + h*f(tk,yk)
for k = 0, 1, 2, ..., m-1
Example:
y' = t2-y y(0) = 1 h = 0.2
k | tk | yk | Exact Answer` |
0 | 0 | 1 | 1.0000 |
1 | 0.2 | 1 + 0.2(02-1) = 0.8 | 0.8213 |
2 | 0.4 | 0.8 + 0.2(0.22-0.8) = 0.648 | 0.6897 |
3 | 0.6 | 0.648 + 0.2(0.42-0.648) = 0.5504 | 0.6112 |
4 | 0.8 | 0.5504 + 0.2(0.62-0.5504) = 0.5123 | 0.5907 |
Heun's Method
We want to solve y'(t) = f( t, y(t) ) over [a,b] with y(t0) = y0.
We can integrate y'(t) over [t0, t1].
∫ y'(t) dt = ∫ f( t, y(t) ) dt
y(t1) - y(t0) = ∫ f( t, y(t) ) dt
Use numerical integration to approximate the integral in the right side.
Use trapezoidal rule.
y(t1) - y(t0) = (h/2)( f(t0,y0) +
f(t1,y1) )
y(t1) = y(t0) + (h/2)( f(t0,y0) +
f(t1,y1) )
Observe that we still need to know f(t1,y(t1)).
This involves y(t1) that is what we want to solve. To eliminate
this circular reference, we use Euler's Approximation to approximate y(t1)
y(t1) = y(t0) +
h*f(t0,y0)
The new equation is:
y(t1) = y(t0) + (h/2)( f(t0,y0) +
f(t1,y(t0)+h*f(t0,y0) )
using y0 = y(t0) and y1 = y(t1)
y1 = y0 + (h/2)( f(t0,y0)
+ f(t0,y0+h*f(t0,y0) )
pk+1 = yk + h*f(tk,yk)
yk+1 = yk + (h/2)( f(tk,yk)
+ f(tk+1,pk+1) )
Euler's Approximation is used as a predictor and the integral approximation is
used as a correction.
Example: (same equation and initial values as the Euler's example)
k | tk | yk | pk | Exact Answer |
0 | 0 | 1 | 1.0000 | |
1 | 0.2 | 0.824 | 1 + 0.2(02-1) = 0.8 | 0.8123 |
2 | 0.4 | 0.6949 | 0.8240 + 0.2(0.22-0.8240) = 0.6672 | 0.6897 |
3 | 0.6 | 0.6186 | 0.6949 + 0.2(0.42-0.6949) = 0.5879 | 0.6112 |
4 | 0.8 | 0.6001 | 0.6168 + 0.2(0.62-0.6168) = 0.5669 | 0.5907 |
Taylor Series Method
Using the Taylor Series to approximate the solution y(t0+h) = y(t0)
+ h*y'(t0) + (h2/2!)(y''(t0)) + ... + (hn/n!)(y(n)(t0))
We want to solve y' = f(t,y) when y(t0) = a. From the Taylor
Expansion we can find the successive points yk+1 = yk + h*y'k
+ (h2/2!)(y''k) + ... + (hn/n!)(y(n)k).
We still need to compute yk'' and yk''', etc using y' =
f(t,y). The error in the approximation will be O(hn+1).
The higher the order of the Taylor Series, the smaller the error.
Example:
Solve y' = t2-y when y(0) = 1 and h = 0.2. Use n=3.
yk+1 = yk + h*y'k + (h2/2!)(y''k)
+ (h3/3!)(y(3)k)
y' = t2-y
y'' = 2t-y'
y''' = 2 - y''
y1 = 1 + 0.2*-1 + 0.22*1/2 + 0.23*1/6 =
0.821333
y1' = 0.22-0.821333 = -0.781333
y1'' = 2*0.2 - (-0.781333) = 1.181333
y1''' = 2 - 1.181333 = 0.818667
y2 = 0.821333 + 0.2(-0.781333) + 0.22*1.181333/2 + 0.23*0.818667/6
= 0.689785
y2' = 0.42 - 0.689785 = -0.529785
y2'' = 2*0.4 - (-0.529785) = 1.329785
y2''' = 2 - 1.329785 = 0.670215
y3 = 0.689785 + 0.2(-0.529785) + 0.22*1.329875/2 + 0.23*0.548683/6
= 0.611317
y3' = 0.62 - 0.611317 = -0.251317
y3'' = 2*0.6 - (-0.251317) = 1.451317
y3''' = 2 - 1.451317 = 0.548683
y4 = 0.611317 + 0.2(-0.251317) + 0.22*1.451317/2 + 0.23*0.548683/6
= 0.590812
Exact Solution: y(0.8) = 0.5907
Taylor: 0.590812
Heun: 0.6001
Euler: 0.5123
To reduce approximation error, increase the order n of the expansion or reduce the value of h. It is more effective to increase n because error is O(hn+1).
Runge-Kutta Method of Order 4
The Taylor Series Method gives a good approximation, but it
requires the derivatives of the function. The Runge-Kutta Method of Order
4 simulates the accuracy of the Taylor Series Method using an order of n=4 but
it does not require the computation of derivatives.
yk+1 = yk + h*(f1 + 2f2 + 2f3
+ f4)/6 where...
f1 = f(tk, yk)
f2 = f(tk+h/2, yk+f1*h/2)
f3 = f(tk+h/2, yk+f2*h/2)
f4 = f(tk+h, yk+f3*h)
Example (same equation and initial conditions as previous
example):
k = 0
y0 = 1
f1 = 02 - 1 = -1
f2 = f(0.1, 0.9) = 0.12-0.9 = -0.89
f3 = f(0.1, 0.911) = 0.12 - 0.911 = -0.901
f4 = f(0.2, 0.8198) = 0.22 - 0.8198 = -0.7798
k = 1
y1 = 1 + 0.2*(-1 + 2*-0.89 + 2*-0.901 + -0.7798)/6 = 0.821273
f1 = 0.22 - 0.821273 = -0.781273
f2 = f(0.3, 0.743196) = 0.32 - 0.743196 = -0.653416
f3 = f(0.3, 0.755958) = 0.32 - 0.755958 = -0.665958
f4 = f(0.4, 0.688081) = 0.42 - 0.688081 = -0.528081
k = 2
y2 = 0.821273 + 0.2*(-0.781273 + 2*-0.653146 + 2*-0.665958 +
-0.528081)/6 = 0.689688
Systems of Differential Equations
Assume we have dx/dt = f(t,x,y), dy/dt = f(t,x,y), x(t0) = x0, and y(t0) = y0. The solution to this system are functions x(t) and y(t) that when derivated and substituted in the system of equations give equality.
Example:
x' = x + 2y x(0) = 6
y' = 3x + 2y y(0) = 4
Solution:
x(t) = 4e4t + 2e-t
y(t) = 6e4t - 2e-t
Numerical Solution with Euler's Method
We can extend Euler's Method of a single differential equation
to a system of equations.
dx/dt = f(t,x,y) --> dx = f(t,x,y) dt
dy/dt = f(t,x,y) --> dy = f(t,x,y) dt
dxk = xk+1 - xk
dyk = yk+1 - yk
dtk = tk+1 - tk = h
xk+1 - xk = f(tk, xk, yk)(tk+1
- tk)
yk+1 - yk = f(tk, xk, yk)(tk+1
- tk)
xk+1 = xk + f(tk, xk, yk)*h
x(t0) = x0
yk+1 = yk + f(tk, xk, yk)*h
y(t0) = y0
The Euler Method does not give good accuracy because it approximates the Taylor Expansion only to the first derivative.
Runge-Kutta Method
The Runge-Kutta Method can be extended to systems of linear equations. The formulas are as follows:
xk+1 = xk + (h/6)(f1 + 2f2 + 2f3 + f4) yk+1 = yk + (h/6)(g1 + 2g2 + 2g3 + g4)
f1 = f(tk, xk, yk) f2 = f(tk + h/2, xk + (h/2)f1, yk + (h/2)g1) f3 = f(tk + h/2, xk + (h/2)f2, yk + (h/2)g2) f4 = f(tk + h, xk + hf3, yk + hg3) g1 = g(tk, xk, yk) g2 = g(tk + h/2, xk + (h/2)f1, yk + (h/2)g1) g3 = g(tk + h/2, xk + (h/2)f2, yk + (h/2)g2) g4 = g(tk + h, xk + hf3, yk + hg3)
Higher Order Differential Equations
Higher order differential equations involve higher derivatives (e.g. x''(t)
and y''(t)). For example,
Example:
4x'' + 3x' + 5x = 2 x(0) = 1 and x'(0) = 3
x'' = (2 - 3x' - 5x)/4
y' = (2 - 3x' - 5x)/4 and x' = y
Review for the Final Exam
20% first half
80% second half
Study: class notes, book, homework, do exercises from the book
Topics from the Second Half