Monday, 07/28/03

 

Minimization Using Derivatives

 

Assume we want to minimize f(x) and it has a unique minimum at p, a  P b and we start the search at P.

If f ‘(P0) > 0 then P is at the left of P0.

 

P0

 

 

If f ‘(P0) < 0 then P is at the right of P0.

Image2

P0

 

 

If the derivative of f(x) is available, then we can use any of the methods we have covered to solve non-linear equation to solve f ‘(x) = 0. (You can use regular-falsi, bisection, Newton Raphson, secant, etc).

 

Example:

f(x) = sin(x)    f ‘(x) = cos(x)

Find minimum in the interval [-¾π,0].

Use bisection for f ‘(x) = 0

 

A

f ‘(a)

b

f ‘(b)

c

f ‘(c)

-2.356

-0.7071

0

1

-1/781

.3827

-2.356

-.7071

-1.1781

.3827

-1.7672

-.1951

-1.7672

-1.951

-1.1781

.3827

-1.4727

.0980

 

 

 

 

1.5708

0

 

 

Finding Minimum for Multiple Variables

 

Steepest Descent of 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)) = "f(x) = ( df(X)/dx1, df(X)/dx2, …, df(X)/dxN)

 

From the concept of gradient we know that the gradient vector points to the direction of the greatest of f(X). 

 

Since we are looking for the minimum, we will use -"f(x), that points in the direction of greatest decrease.

Basic gradient method:

-         Start at point P0 and move along the line in the direction -"f(x) on small increment.

-         That is going to take you closer to the minimum

P1 = P0 - "f(P0)h        h is a small increment

     

Pk = Pk-1 - "f(Pk-1)h

 

Example

f(x) = x2 + 1

f ‘(x) = 2x                              

 

"f(x) = 2xi                                                            xk = xk-1 – 2xk-2 h

Let h = 0.1                                        "f(x) = 2(-1)i

 

0 = -1

x1 = -1 - 2(-1)0.1 = -1 + .2 = -0.8

x2 = -.8 – 2(-.8)(.1) = -.8 + .16 = -.64

x3 = -.64 – 2(-.64)(.1) = -.64 + .128 = -.512

 

   0

 

The gradient method does not guarantee a global minimum. The minimum obtain is the closest in the path from the starting point.

A method used to find a global minimum is called “simulated annealing” that “shakes” the curve from a high intensity to a lower intensity until the method arrives to the global minimum. “Simulate annealing” simulated cooking down of a metal or crystalline structure.

 

    

Tuesday, 07/29/03

 

Numerical Solution of Differential Equations

 

Introduction

Example

     y’ = ky

     dy/dt = ky        òdy/y =  òk dt

                                   ln y = kt + k2

                                                    eln y = ek2  ekt

                                   y     = k3 ekt

Very often differential equations do not have an analytic solution, so they have to be approximated using numerical method.

 

Euler’s Method

 

Let [a,b] be the interval which we want to find the solution of y’ = f(t,y), which y(a) = y0. We will find a set of points:

     (t0, y0), (t1, y1), …, (tk,yk) that are used to approximate y(t) ≈ y(t,k)

 

 

Also tk = a + kh      for k = 0,1,2,…M

We want to solve y’ = f(t,y) over [t0, t1, …, tm] with y(t0) = 0.

 

Using Taylor’s expansion to approximate y(t):

      y(t) = y(t0) + y’(t0)(t-t0) + y”(c1)(t-t0)2/2

                                                y”(c1)(t-t0)2/2 error, a < c1 < b

 

We use this equation to obtain y(t1):

      y(t) = y(t0)  +y’(t0)(t1-t0) + y”(c1)(t-t0)2/2

       y1          y0               y0

 

If the step size is small enough, we can neglect the second order error.

     y1 = y0 + hy’(t0)

     y1 = y0 + hf(t0,y0)

In general:

     yk+1 = yk + h      

     yk+1 = yk + hf(tk,yk)     for k=0,1,…M-1

 

Example

y’ = t2 – y            y(0) = 1      h = .2

f(t,y), so f(t,y) = y’

 

t1 = 0 + 0.2 = 0.2

y1 = y+ hf(t0,y0) = 1 + .2(02-1) = .8

 

t2 = .2 + .2 = .4

y2 = .8 + .2(.22 - .8) = .648

 

t3  = .4 + .2 = .6

y3 = .648 + .2(.42 - .648) = .5504

 

t4 = .6 + .2 = .8

y4 = .5504 + .2(.62 - .5504) = .5123

 

Analytical solution is: y(t) = -e-t + t2 – 2t + 2

y(.4) = -e-.4 + (.4)2 – 2(.4) + 2

        = .6897  (Euler: .648)

y(.8) = -e-.8 + (.8)2 – 2(.8) + 2

       = .5407   (Euler: .5123)

 

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]

     òtot1y’(t) dt =  òtot1f(t,y(t)) dt

    y(t1) – y(t0) =  òtot1f(t,y(t)) dt

 

We use numerical integration to approximate the integral in the right side. Use trapezoidal rule

    y(t1) – y(t0) = h/2 (f(t0,y(t0)) + f(t1,y(t1)))

then we have

    y(t1) = y(t0) + h/2 (f(t0,y(t0)) + f(t1,y(t1)))

 

Observe that we still need to know f(t1,y(t1)), that 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) + hf(t0,y(t0))

So we have:

  y(t1) = y(t0) + + h/2 (f(t0,y(t0)) + f(t1,y(t0)+hf(t0,y(t0)))

  y1 = y(t1)    y0  = y(t0)

  y1 = y0 + + h/2 (f(t0,y(t0)) + f(t1,y0+hf(t0,y0)))

                                                      P0

 

In general:

  Pk+1 = yk + hf(tk,yk)

  Yk+1 = yk + h/2 (f(tk,yk) + f(ty,Pk+1))

Euler approximation is used as a prediction and the integral approximation is used as correction.

 

Example

f(y,t) = y’ = t2 – y     y(0) = 1     h = .2

k = 0     y0 = 1    t0 = 0

k = 1     t1 = 0 + .2 = .2

             P1 = y0 + hf(t0,y0)

                 = y0 + h(t02 – y0)

                 = 1 + .2(02 – 1) = .8

             y1 = y0 + h/2 ((t02 – y0) + (t12 +  P1))

                 = 1 + .2/2 ((02 – 1) + ((.2)2 + .8))

                 = .8240

k = 2     t2 = .4

             P2 = .8240 + .2(.22 – 0.8240) = 0.6672

             y2 = .8240 + .2/2 ((.22 – 0.8240) + .42 - .6672)

                  = .6944

k = 3    t3 = .6

           P3  = .6949 + .2(.42 - .6949) = .5879

           y3 = .6949 + .2/2 ((.42 - .6949) + (.62 - .5879))

                = .6186

k = 4   t4 = .8

           P4 = .6186 + .2(.62 - .6186) = .3669

           y4 = .6186 + .2/2 ((.62 - .6186) + (.82 - .5669))

                = .6001

 

 

k

tk

yk(Euler)

yk(Heun)

yk(exact)

2

.4

.648

.6949

.6897

4

.8

.5123

.6001

.5907

 

 

Wednesday, 07/30/03

 

Numerical Solution of Differential Equations

 

Taylor Series Method

 

Using the Taylor expansion to approximate the solution:

    y(t0+h) = y(t0) + hy’(t0) + h2y”(t0)/2 + … + hNyN(t0)/N! + O(hN+1)

We want to solve

    y’ = f(y,t)        y(t0) = a

From the Taylor expansion we can find the successive points.

    yk+1 = yk + hyk’ + h2yk”/2 + h3y”’/3! … + hNyN/N!

We still need to compute yk”, yk”’ … etc using y’ = f(y,t).

 

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          y(0) = 1         h = .2

           Use N = 3

yk+1 = yk + hyk’ + h2y”(t0)/2 + … + h3y’’’(t0)/6

y’ = t2 – y

y” = 2t – y’

y”’ = 2 – y”

 

k=0   t=0    y0 = 1

                   y0’ = 02 – 1 = -1

                   y0” = 2(0) – (-1) = 1

                   y0”’ = 2 - 1 = 1

 

k=1   t=.2    y1 = 1 + .2(.1) + (.2)2(1)/2 + (.2)3(1)/6 = .821333

                   y1’ = (.2)2 – .821333 = -.781333

                   y1” = 2(.2) – (-.781333) = 1.181333

                   y1”’ = 2 – 1.181333 = .818667

 

k=2   t=.4   y2 = .821333 + .2(-.781333) + (.2)2(1.181333)/2 + (.2)3(.818667)/6 = .689785

                   y2’ = (.4)2 – .689785 = -.524785

                   y2” = 2(.4) – (-.524785) = 1.329785

                   y2”’ = 2 – 1.329785 = .670215

 

k=3   t=.6   y3 = .689785 + .2(-.524785) + (.2)2(1.329785)/2 + (.2)3(.670215)/6 = .611317

                   y3’ = (.6)2 – .611317 = -.251317

                   y3” = 2(.6) – (-.251317) = 1.451317

                   y3”’ = 2 – 1.451317 = .548633

 

k=4   t=.8   y4 = .611317 + .2(-.251317) + (.2)2(1.451317)/2 + (.2)3(.5486333)/6=.590812

         y(.8) =          

            exact: .5907

            Euler: .5123

            Heun: .6001

 

To reduce approximation error, increase the order N of the expansion or reduce the value of h. I t is more effective to increase N.

     Error = O(hN+1)

 

 

Runge-Kutta  method of order 4.

-         Taylor method gives a good approximation, but it requires the derivative of the function.

-         The Runge-Kutta method of order 4 simulates the accuracy of the Taylor series method using an order N=4 but it does not require the computation of derivatives.

yk+1 = yk + h(f1 + f2 + f3 + f4)/6

where:

f1 = f(tk,yk)

f2 = f(tk + h/2, yk + h/2 f1)

f3 = f(tk + h/2, yk + h/2 f2)

f4 = f(tk + h, yk + h f3)

 

Example

y’ = f(t,y) = t2-y           y(0) = 1            h = .2

 

t0=0            y0 = 1

k=0            f1 = 02 -1 = -1

                  f2 = f(0 + .2/2, 1+.2/2(-1))

                      = f(.1,.9) = .12 - .9 = -.89   

                  f3 = f(0 + .2/2, 1+.2/2(-.89))

                      = f(.1,.911) = .12 - .911 = -.901

                  f4 = f(0 + .2, 1+.2(-.901))

                      = f(.2,.8198) = .22 - .8198 =- .7798

 

t1=.2           y1 = (1 + .2( -1 + 2(-.89) + 2 (-.901) + (-.7798))/6   

k=1                = .821273

                  f1 = .22 -.821273 = -.781273

                  f2 = f(.2 + .2/2, 0.821273+.2/2(-.781273))

                      = f(.3,.743186) = .32 - .743146 = -.653146   

                  f3 = f(.2 + .2/2, .821273+.2/2(-.653146))

                      = f(.3,.755958) = .32 - .755958 = -.665958

                  f4 = f(.2 + .2, .821273+.2(-.665958))

                      = f(.4,.688081) = .42 - .688081 = -.528081

 

t1=.4           y2 = (.821273 + .2( -.781273 + 2(-.653146) + 2 (-.665958) + (-.528001))/6   

k=1                = .689688

 

exact:   y(.4) = .6847

Taylor: y(.4) = .689783

 

Thursday, 07/31/03

 

System of Differential Equations

 

Assume we have

    dx/dt = f(t,x,y)                    with  x(t0) = x0

    dy/dt = g(t,x,y)                             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 of a system of differential equations

x’ = x + 2y        x(0) = 6

y’ = 3x + 2y      y(0) = 4

solution:

x(t) = 4e4t + 2e-t

y(t) = 6e4t – 2e-t

You can verify that this is the solution by derivating and substituting.

 

Numerical Solution

 

Euler’s Method

We can extend Euler’s method of a single differential equations to a system of equation.

Dx/dt = f(t,x,y)

Dy/dt = g(t,x,y)

 

Dx = f(t,x,y)dt

Dy = g(t,x,y)dt

Approximation       dxk = xk+1 - xk

                               dyk = yk+1 - yk

                               dtk = tk+1 - tk

 

xk+1xk = f(tk,xk,yk) (tk+1-tk)

yk+1yk = g(tk,xk,yk) (tk+1-tk)

 

So we have that

xk+1 = xk + f(tk,xk,yk) h                   x(t0) = x0

yk+1 = yk + g(tk,xk,yk) h                  y(t0) = y0

 

The Euler’s method does not give good accuracy because it approximates the Taylor expansion only to the first derivative.

 

Runge-kutta for systems of differential equations

Rk can be extended to systems of linear equations.

The formulas are: (Rk order 4)

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 involved higher derivatives x”(t), y”(t).

For example:

   mx”(t) + cx’(t) + kx(t) = g(t)

The higher order differential equations can be transformed to a system of differential equations of first order.

Use the substitution:

  y(t) = x’(t)

The system

  mx”(t) + cx’(t) + kx(t) = g(t)

obtain x”(t)

  x”(t) = (g(t) – cx’(t) – kx(t)) / m

substituting x”(t)

  y’(t) = (g(t) – cy(t) – kx(t)) / m    --------- 1

This is the first differential equation of first order in the system.

The second equation is

x’(t) = y(t)  -------------  2

 

Example

 

4x” + 3x’ + 5x = 2          with x(0) = 1

x” = (2 – 3x’ – 5x) / 4             x’(0) = 3

 

Let y = x’ and substituting in x”

y’ = (2 – 3y – 5x)/4       x(0) = 1, y(0) = 3

x’ = y

 

 

Friday, 08/01/03

 

CS 314 Final Review

 

Curve Fitting

- Least squares line

- Least squares for non-linear equations

- Transformation for data linearization

- Polynomial fitting

- Splines

   + The 5 properties(I … V)

   + Proof

   + How to use the formula to get the spline coefficient

   + End-point constraints

 

 Numerical Differentiation

-         Limit of difference quotient

-         Central difference formula of order O(n2)

-         Central difference formula of order O(n4)

 

Numerical Integration

-         Trapezoidal rule

-         Simpson’s rule

 

Numerical Optimization

-         Local/global minimum/maximum point

-         Properties of minimum/maximum point

-         The golden ratio method

-         Minimization using derivatives

-         Steepest Descent of Gradient method

 

Solution of Differential Equations

-         Euler’s method

-         Heun’s method

-         Taylor Series method

-         Runge-Kutta method of order 4

-         System of Differential equations

            + Euler’s method

            + Runge-kutta of order 4

-         Higher order differential equation

 

 

Final

  20% first half, 80% second half

 

Study:

Class notes, book, homework, do exercises in book

 

You may bring:

1 page formula

calculator

 

Final exam: Friday, Aug 8th

                    10:20-12:20

                    wthr 104