Docsity

Convergence Order of Approximate Solutions to Differential Equations using Various Methods, Study notes of Calculus

Toggle sidebar
bg1
Math 128A Computer Lab 8
Ordinary Differential Equation Solvers
Adrian Down
April 21, 2006
Part 1
Discussion
Motivation
In this lab, our goal is to approximate a piecewise-defined differential equa-
tion,
˙y=f(t, y(t)) f(y) = (y y < e
e−2(y−e)y≥e(1)
We are supposed to create an approximate solution yon the interval t∈[0,2]
with steps size husing three methods: Forward Euler (FE), Modified Euler
(ME), and Runge-Kutta (RK).
Exact solution
I first find the analytic solution to (1), which I call x. The solution for x < e
is simple,
˙x=x,x0= 1
⇒x=et
The solution for x≥eis not immediately obvious, so I follow the usual
procedure of finding a homogeneous and particular solution. I then form the
general solution as a linear combination of these two solutions.
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Convergence Order of Approximate Solutions to Differential Equations using Various Methods and more Study notes Calculus in PDF only on Docsity!

Math 128A Computer Lab 8

Ordinary Differential Equation Solvers

Adrian Down

April 21, 2006

Part 1

Discussion

Motivation

In this lab, our goal is to approximate a piecewise-defined differential equa- tion,

y˙ = f (t, y(t)) f (y) =

y y < e e − 2(y − e) y ≥ e

We are supposed to create an approximate solution y on the interval t ∈ [0, 2] with steps size h using three methods: Forward Euler (FE), Modified Euler (ME), and Runge-Kutta (RK).

Exact solution

I first find the analytic solution to (1), which I call x. The solution for x < e is simple,

x˙ = x, x 0 = 1 ⇒ x = et

The solution for x ≥ e is not immediately obvious, so I follow the usual procedure of finding a homogeneous and particular solution. I then form the general solution as a linear combination of these two solutions.

Finding the homogeneous solution, neglecting initial conditions for the time being,

x˙ = − 2 x ⇒ x = e−^2 t

By inspection, a particular solution is x = 32 e,

x˙ = e − 2

e − e

= e − 2

e

The general solution is a linear combination of the particular and homoge- neous solutions,

x =

e + ae−^2 t

where a is a constant determined by the initial conditions. The change in the definition of f occurs when the functional form of x for t < 1 reaches x = e, hence at x = et^ = e ⇒ t = 1. In order for the solution to be continuous on t ∈ [0, 2], I choose x(1) for the solution defined on t ∈ [1, 2] such that x(1) = e,

e = x(1) =

e + ae−^2

⇒ −

e = ae−^2

⇒ a = −

e^3

Thus the solution for t ∈ [1, 2] is,

x =

e −

e^3 −^2 t

The solution over the entire interval in t is,

x(t) =

et^0 ≤ t < 1 3 2 e^ −^

1 2 e

3 − 2 t (^1) ≤ t ≤ 2

Using the definition of N = (^1) h ,

yn =

N

)n

I now consider this approximate solution after n = N steps. I treat the N in the exponent by writing the quantity in parentheses as the exponential of a logarithm,

yN =

N

)N

= eN^ log(1+^

1 N )

To find the leading-order dependence in (^) N^1 = h, I use some Taylor expansions,

log (1 + ζ) = ζ −

ζ^2 + O(ζ^3 )

⇒ yN = eN^ (^

1 N −^ 1 2 N^2 +O(^ 1 N^3 ))

= e^1 −^

21 N +O(^1

N^2 ) = e · e−^ 1 2 N (^) · eO(^ 1 N^2 ) eζ^ = 1 + ζ + O

ζ^2

⇒ yN = e

2 N

+ O

N^2

1 + O

N^2

= e −

e 2 N

+ O

N^2

I compare with the exact solution to find the error,

yN − x(1) =

e −

e 2 N

+ O

N^2

− e

e 2 N

+ O

N^2

e 2

h + O(h^2 )

Hence the FE method is first-order in h at the point t = 1. I now consider the order of convergence of the FE method at the point t = 2. First, I find the value of the iterative approximation for y ≥ e,

yn+1 = yn + hf (yn) = yn + h (e − 2(yn − e)) = yn (1 − 2 h) + 3he

This first-order difference equation is similar to a first-order differential equation, and so I use the same techniques as before: I first find a particular solution and then solve the homogeneous case. By inspection, the same particular solution yn = 32 e satisfies this recursion relation,

yn+1 =

e (1 − 2 h) + 3he

=

e − 3 he + 3he =

e

I now consider the homogeneous case,

yn+1 = yn(1 − 2 h)

I will treat the initial conditions by allowing them to determine the value of the coefficient in the linear combination in the solution to the recurrence relation. Thus I neglect the initial conditions for the moment to obtain a solution to the homogeneous difference equation,

yn = (1 − 2 h)n

The general solution to the difference equation is,

yn =

e + a (1 − 2 h)n

where a is a constant to be determined by the initial conditions. As in the case of the exact solution to the differential equation, I choose the constant a so that the approximate solution is continuous at the point t = 1, which corresponds to the value of the approximation after N steps,

yN =

e + a(1 − 2 h)N^ = (1 + h)N

⇒ a = (1 − 2 h)−N

[

(1 + h)N^ −

e

]

The approximate solution for 1 ≤ t ≤ 2 is thus,

yn =

e +

[

(1 + h)N^ −

e

]

(1 − 2 h)n−N

⇒ y 2 N =

e +

[

e −

e 2 N

+ O

N^2

e

]

×

[

e−^2 −

e−^2 2 N

+ O

N^2

)]

e +

[

e −

e 2 N

] [

e−^2 −

e−^2 2 N

]

+ O

N^2

e −

2 e

4 Ne

2 Ne

+ O

N^2

e −

2 e

4 Ne

+ O

N^2

I compare with the exact solution to find the error,

y 2 N − x(2) =

e −

2 e

4 Ne

+ O

N^2

e −

2 e

4 Ne

+ O

N^2

4 e

h + O(h^2 )

Hence the error of the FE method is also first-order in h at t = 2, as claimed above.

ME method I now use exactly the same method to determine the order of convergence of the ME method at t = 1 and t = 2. Consider the previous four pages a warmup. I first the approximation on the interval t ∈ [0, 1), where f takes the more simple form f (y) = y. The iteration scheme of the ME method is,

yn+1 = yn + hf

yn +

h 2

f (yn)

= yn + hf

yn

h 2

= yn + h

yn

h 2

= yn

1 + h +

h^2 2

Taking the initial condition y 0 = 1,

yn =

1 + h +

h^2 2

)n

Using the definition of N = (^1) h ,

yn =

N

2 N^2

)n

I now consider this approximate solution after n = N steps. I treat the N in the exponent by writing the quantity in parentheses as the exponential of a logarithm,

yN =

N

2 N^2

)N

= eN^ log(1+^

1 N +^ 1 2 N^2 )

To find the leading-order dependence in (^) N^1 = h, I use some Taylor expansions. I expand in the small parameter

N +^

1 2 N 2

yN = e N

“ ( (^) N^1 +^2 N^12 )−^12 ( (^) N^1 −^2 N^12 )^2 +O( (^) N^13 )

”

= eN^ (^

1 N + 1 2 N^2 −

1 2 N^2 +O(^

1 N^3 ))

= e1+O(^

1 N^2 ) = e · eO(^ 1 N^2 )

= e

1 + O

N^2

= e + O

N^2

I compare with the exact solution to find the error,

yN − x(1) =

e + O

N^2

− e

= O

N^2

= O(h^2 )

Hence the ME method is second-order in h at the point t = 1.

The general solution to the difference equation is,

yn =

e + a

1 − 2 h + 2h^2

)n

where a is a constant to be determined by the initial conditions. As in the case of the exact solution to the differential equation, I choose the constant a so that the approximate solution is continuous at the point t = 1, which corresponds to the value of the approximation after N steps,

yN =

e + a(1 − 2 h + 2h^2 )N^ =

1 + h +

h^2 2

)N

⇒ a =

1 − 2 h + 2h^2

)−N

[(

1 + h +

h^2 2

)N

e

]

The approximate solution for 1 ≤ t ≤ 2 is thus,

yn =

e +

[(

1 + h +

h^2 2

)N

e

]

1 − 2 h + 2h^2

)n−N

I now evaluate this approximate solution at n = 2N, which corresponds to the point t = 2. As before, I treat factors of N in exponents by writing terms as exponentials of logarithms,

yn =

e +

[(

1 + h +

h^2 2

)N

e

]

1 − 2 h + 2h^2

)N

= yn =

e +

[(

N

2 N^2

)N

e

] (

N

N^2

)N

N

2 N^2

)N

= eN^ log(1+^ N^1 +^1 2 N^2 )

= eN

“ ( (^) N^1 +^2 N^12 )−^12 ( (^) N^1 +^2 N^12 ) (^2) +O ( (^) N^13 )

”

= eN^ (^ N^1 +  2 N^1 ^2 − 

2 N^1 ^2 +O(^ N^13 ))

= e1+O(^

1 N^2 ) = e · eO(^ 1 N^2 )

= e

1 + O

N^2

= e + O

N^2

N

N^2

)N

= eN^ log(^1 −^ N^2 +^2 N^2 )

= e N

“ (−^ N^2 +^ N^22 )−^12 (−^ N^2 +^ N^22 )^2 +O( (^) N^13 )

”

= e N

“ − (^) N^2 + N^22 −^12 N^42 +O( (^) N^13 )

”

= e−2+O(^

1 N^2 ) = e−^2 · eO(^ 1 N^2 )

= e−^2

1 + O

N^2

= e−^2 + O

N^2

⇒ y 2 N =

e +

[

e + O

N^2

e

] [

e−^2 + O

N^2

)]

e +

e · e−^2 + O

N^2

e −

2 e

+ O

N^2

I compare with the exact solution to find the error,

y 2 N − x(2) =

e −

2 e

+ O

N^2

e −

2 e

= O

N^2

= O(h^2 )

Hence the error of the ME method is also second-order in h at t = 2, as claimed above.

RK method Presumably similar calculations would bear out the fact that the Runge-Kutta method has the same order of convergence at the points t = 1 and t = 2.

Results

See figures 1, 2, 3 and tables 1 and 2.

(^00) 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2

1

2

3

4

5

t

y

exact solution k = 1 k = 2 k = 3

Figure 2: Modified Euler approximation to piecewise exponential function on the interval [0, 2] with step width 2−k.

% Computes and returns log_2 of ratio of successive error terms to % demonstrate order of convergence of each method at t = 1 and t = 2

x = cell(kmax + 1, 1); % cell array of x mesh points. x{1} is exact solution and x{k+1} is 2^-(k+1) mesh

y = cell(kmax + 1, 3); % cell array of values of the approximate solution. y{1,1} is exact solution % y{k,j} is 2^-(k+1) mesh. j = 1 is FE, j = 2 is ME, and j = 3 is RK

a = 0; % left end-point of approximation interval b = 2; % right end-point of approximation interval y0 = exactsoln(a); % exact soln and approximation have same initial value

(^00) 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2

1

2

3

4

5

t

y

exact solution k = 1 k = 2 k = 3

Figure 3: Runge-Kutta approximation to piecewise exponential function on the interval [0, 2] with step width 2−k.

h = 2^(-max(kmax,4)); % compute step size for exact solution display n = (b - a)/h + 1; % compute number of steps for exact solution display x{1} = linspace(a,b,n); % x mesh for exact solution display y{1,1} = zeros(1,n); for k = 1:n y{1,1}(k) = exactsoln(x{1}(k)); % evaluate exact solution for display end ymid = y{1,1}((n - 1) / 2 + 1); % get exact solution value at t = 1 yb = y{1,1}(n); % get exact solution value at t = 2 err = zeros(kmax+1,3,2); % error of approximate solution, approx-exact logerr = zeros(kmax,3,2); % logarithmic ratio of errors to show order of convergen for k = 2:kmax+1 % for each mesh size 2^(k+1); skip k = 1, which holds exact solut h = 2^(-(k-1)); % compute mesh size

k Forward Euler Modified Euler Runge-Kutta 1 1. 7226654648782 2. 4934785422619 4. 4967181273865 2 1. 1581995838007 2. 2332658101484 4. 2120942973796 3 1. 0632997590587 2. 1059833275346 4. 096072712617 4 1. 0287535016959 2. 0501439257581 4. 0454087472413 5 1. 0137395935223 2. 0243651720178 4. 022030898966 6 1. 0067195571321 2. 0120077747583 4. 0108420279655 7 1. 003323255551 2. 0059604850313 4. 0054953115112 8 1. 0016526212283 2. 0029694303973 4. 0026447958462 9 1. 0008240741926 2. 0014820355115 3. 9831564898366 10 1. 0004114798005 2. 0007403556689 7. 129283016945 11 1. 0002056011255 2. 0003695020821 0

Table 2: Logarithmic ratio of error terms log (^2) ǫk^ ǫk+1 evaluated at t = 2, showing

order of convergence of each approximation method at this point. Orders of convergence appear to be 1, 2, and 4, respectively. The ratio of error terms for the Runge-Kutta method with k ≥ 10 begins to degrade due to the numerical precision of MatLab.

figure; hold on; plot(x{1},y{1,1},’b-’); plot(x{2},y{2,k},’r--’); plot(x{3},y{3,k},’m--’); plot(x{4},y{4,k},’g--’); xlabel(’t’); ylabel(’y’); axis([0 2 0 5]); legend(’exact solution’,’k = 1’,’k = 2’,’k = 3’); end

% ----------------------------------------------------------------------------- % Created by Adrian Down, University of California, Berkeley. % Created for Prof. John Neu’s numerical analysis class, Math 128A, Spring 2006 % ----------------------------------------------------------------------------- % function y = approx(type,y0,a,b,h)

% Approximate the differential equation described in eff.m with initial % value y0 on the interval [a, b] with step size h. ’type’ indicates what % type of approximation scheme to use: 1 = Forward Euler, 2 = Modified % Euler, and 3 = Runge-Kutta. Returns a cell array of values of the % approximate solution at mesh points t = kh. % Function returns a cell array to allow for approximation of % multi-dimensional functions. Convert returned cell array to regular % array before use. n = (b - a)/h + 1; % compute number of steps in the mesh y = cell(1,n); % initialize cell array that will hold values of approximate soluti y{1} = y0; for k = 2:n % at each mesh point, calculate approximation using value at previous y{k} = step(type,y{k-1},h); end

% ----------------------------------------------------------------------------- % Created by Adrian Down, University of California, Berkeley. % Created for Prof. John Neu’s numerical analysis class, Math 128A, Spring 2006 % ----------------------------------------------------------------------------- % function xnext = step(type,xnow,h) % Given value of approximation at one mesh point xnow, compute value of % approximation at next endpoint xnext a distance h from xnow. % ’type’ defines approximation method. See below. switch type % choose approximation method case 1 % Forward Euler xnext = xnow + h * eff(xnow); case 2 % Modified Euler xnext = xnow + h * eff(xnow + h/2 * eff(xnow)); case 3 % Runge-Kutta F1 = eff(xnow); F2 = eff(xnow + h/2 * F1); F3 = eff(xnow + h/2 * F2); F4 = eff(xnow + h * F3); xnext = xnow + h/6 * (F1 + 2F2 + 2F3 + F4); end

% -----------------------------------------------------------------------------

equation governing planetary motion,

X¨ = − X

|X|^3

where X = (x(t)y(t)) is a position vector. Before solving this equation, I employed our usual strategy of converting this second order differential equation to a system of first order differential equations by defining a new variable,

V ≡ X˙

⇒ V˙ = −

X

|X|^3

The system of differential equations relating V and X can be written in matrix form,

( X˙ V^ ˙

− (^) r^13

X

V

where r ≡ |X|. The vectors X and V each themselves contain two spatial coordinates. It is convenient to express all four of these coordinates explicitly,

   

x ˙ y ˙ v ˙x v ˙y

− (^) r^13 0 0 0 − (^) r^13 0

x y vx vy

where r ≡ |X| =

x^2 + y^2. I find that the approximate solution to the differential equation varies greatly depending on the initial velocity of the planet. In all cases, the initial position of the planet is taken to be on the ˆx axis at (1, 0),and the initial velocity is taken to be purely in the ˆy direction. In the case that the initial velocity has magnitude 1, I find that the orbit is circular. In the case that the initial velocity has magnitude .5, I find that the orbit becomes an oval, and is no longer centered about the center of force at (0, 0). I also find that the approximate solution becomes quickly divergent; I believe that this behavior is due to the rapid change of the orbit at the horizontal maxima

of the ellipse. Taking the magnitude of the initial velocity to be .25 fields similar results, although in this case, the resulting orbit is further displaced from the center of force and has a smaller semi-minor axis. The approximate solution is highly divergent in this case, and I had to take a very small step size to obtain a periodic elliptical orbit.

Results

See figure 4.

−1 −0.5 0 0.5 1

−0.

−0.

−0.

−0.

0

v0x= v0x=. v0x=.

Figure 4: Planetary orbits with varying values of initial velocity in the y direction.

Code