CS 314 Notes July
26 --- July 30
Monday July 26, 2004
Euler’s
method:
Let [a,b] be the
interval which we want to find the solution of y' = f(t,y), with y(a) = y0.
We will find a set of points (t0,y0), (t1,y1),...,(tK,yK)
that are used to approximate y'(t) ~= y'(tK).
h =
(b-a)/m step size
Also, tk
= a + k*h for k = 0,1,2,...,m
we want to solve y' = f(t,y) over [t0,t1,...,tm]
with y(t0) = 0
Use Taylor
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
is Error
Use equation to
obtain y(t1)
y(t1) =
y(t0) + y'(t0)(t1-t0) + y''(c1)(t1-t0)2/2
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,...,m-1
Example:
|
y' = t2 - y |
y (0) = 1 |
h = .2 |
|
|
||
If you want to get
better precision with Euler’s method, you can make h (step size) smaller.
Also, the larger
the k is, the larger the solution error will be.
We want to solve y '(t) = f(t, y (t)) over [a,
b] with y (t0) = 0. We can integrate y '(t)
over [t0, t1]:
|
t1 |
|
t1 |
|
|
ò |
y '(t)dt = |
ò |
¦(t, y (t))dt = y (t1)
- y (t0) |
|
t0 |
|
t0 |
|
We use numerical integration to approximate the integral in the right side. Use the trapezoidal rule:
y (t1) - y (t0) = (h/2)[f(t0,
y (t0)) +(t1, y (t1))]
Then we have:
y (t1) = y (t0) + (h/2)[f(t0,
y (t0))+(t1, y (t1))]
Observe we still need to know (t1, y (t1)).
That involves y (t1) which is what we want to solve. To
eliminate this circular reference, we use Euler's approximation to obtain 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)
+ h*f(t0, y (t0)))
y1 = y (t1)
y0 = y (t0)
y1 = y0 + (h/2)[¦f(t0, y0) + ¦f(t1,
y0 + h*f(t0, y0))]
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 |
Tuesday July 27, 2004
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 since Error = O(hN+1)
Wednesday July 28, 2004
Class has been
canceled today.
Thursday July 29, 2004
Final exam: Next
Thursday Aug 5 from 1:00 to 3:00, at Univ 303
You can use more
time if you want
A review will be
given on Friday July 30
Hw5 is posted due
on next Tuesday during class
To test the
correctness of a Spline, the following has to be true:
1. Continuity in the function
S0(x0)=y0
S0(x1)=y1
S1(x1)=y1
S1(x2)=y2
S2(x2)=y2
S2(x3)=y3
2. Continuity in 1st derivative
S0’(x1)=S1’(x1)
S1’(x2)=S2’(x2)
3. Continuity in 2nd derivative
S0’’(x1)=S1’’(x1)
S1’’(x2)=S2’’(x2)
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
Iteration0 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= .821273
Iteration1
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
t2=.4 y2
= (.821273 + .2( -.781273 + 2(-.653146) + 2 (-.665958) + (-.528001))/6
= .689688
exact: y(.4) = .6847
Taylor: y(.4) = .689783
Systems of Differential
Equations
Assume
we have equations:
dx/dt = f(t,x,y)
dy/dt = g(t,x,y)
with x(t0) = x0; 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.
Ex 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
- Euler 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 = g(t,x,y) dy
= g(t,x,y)dt
Approximate
dxk = xk+1-xk
dyk = yk+1 - yk
dtk = tk+1 - tk
xk+1-xk =
f(tk,xk,yk)(tk+1-tk)
yk+1-yk = g(tk,xk,yk)(tk+1-tk)
x(t0) = x0 y(t0)
= y0
Therefore
xk+1 = xk + f(tk,xk,yk)*h
yk+1 = yk + g(tk,xk,yk)*h
Bad Accuracy because it approximates Taylor to the 1st Derivative
- Runge-Kutta (Order 4) for a system of differential equations
xk+1 = xk +
h * (f1 + 2f2 + 2f3 + f4) / 6
yk+1 = yk + h * (g1 + 2g2 + 2g3
+ g4) / 6
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 + h*f3, yk
+ h*g3 )
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 + h*f3, yk
+ h*g3 )
Friday July 30, 2004
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
Final Review
10% - First Half
90% - Second Half
Study Materials:
Do hw5
Class Notes
Book
Read Homework & Projects
Exercises in Book
May Bring
1/2 page formulary (one side)
Calculator
Hough Transform
It’s used in Pattern Recognition
Assume you have N facts and you have M hypothesis
Hough Algorithm:
Have accumulator vector with M entries (M hypothesis) and initialize it to 0.
hypothesis_accumulator [i] = 0 for i=1....M
For all facts j=1....N
If facts[j] supports hypothesis k,
increment hypothesis_accumulator[k]
The most likely hypothesis will be the one with the largest hypothesis_accum
most likely hypothesis = maximum <hypothesis_accum>