interpolating solutions to ivps douglas wilhelm harder, m.math. lel department of electrical and...

Post on 19-Jan-2016

219 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Interpolating Solutions to IVPs

Douglas Wilhelm Harder, M.Math. LELDepartment of Electrical and Computer Engineering

University of Waterloo

Waterloo, Ontario, Canada

ece.uwaterloo.ca

dwharder@alumni.uwaterloo.ca

© 2012 by Douglas Wilhelm Harder. Some rights reserved.

2

Outline

Given a solution to an IVP in the form of two vectors tout and yout, how do we approximate the solution at a point t where

tout,k < t < tout,k + 1

We will look at:– Interpolation, and– Dormand Prince

Interpolating Solutions to IVPs

3

Outcomes Based Learning Objectives

By the end of this laboratory, you will:– Understand how to use piecewise polynomials to approximate

solutions of an IVP from discrete approximations– Understand how the mkpp and ppval functions in Matlab work

Interpolating Solutions to IVPs

4

Introduction

Suppose that we found an approximation to an initial-value problem:

[t_out, y_out] = dp45( @f, [a, b], y_init, ... );

The output vector gives us the information that tout,k ≈ yout,k

What happens if we want to approximate the solution u(t) at an arbitrary point that may fall between two of these t-values?

tout,k < t < tout,k + 1

Interpolating Solutions to IVPs

5

Introduction

We will deal with one 1st-order and one 2nd-order ODE:function [dy] = f8a( t, y ) dy = (y - 1).^2 .* (t - 1).^2;end

function [y] = y8a_soln( t ) y = (t.^3 - 3*t.^2 + 3*t)./(t.^3 - 3*t.^2 + 3*t + 3);end

function [dw] = f8b( x, w ) dw = [w(2); sin(x) - 4*w(2)*w(1) + 2*x*w(1)^2];end

function y = y8b_soln( t ) c = 5^(1/3); d = c/80; y = 1/c*exp( t/4 ).*(

... airy( 2, d )*airy( d*(1 - 80*t) )

... - airy( d )*airy( 2, d*(1 - 80*t)))/(airy( 3, d )*airy( d )

... - airy( 2, d )*airy( 1, d )

... );end

Interpolating Solutions to IVPs

6

Approximating Solutions

We now know that

tk < t < tk + 1

and we know that

y(tk) ≈ uk and y(tk + 1) ≈ uk + 1

How do we approximate y(t)?

Interpolating Solutions to IVPs

7

Linear Interpolation

As an initial idea, we could interpolate the two points

(tk, yk) and (tk + 1, yk + 1)

with a straight line...

Interpolating Solutions to IVPs

8

Linear Interpolation

We can define a separate linear polynomial between each consecutive pair of points from (t1, y1) to (tn, yn)

Interpolating Solutions to IVPs

9

Linear Interpolation

This is no different than connecting the dots with straight lines...

[t6c, y6c] = dp45( @f6c, [0, 3], [0, 1]', 1, 1e-1 );

plot( t6c, y6c(1,:), '-b.' );

Interpolating Solutions to IVPs

10

Linear Interpolation

Such a structure is said to be a piecewise-defined polynomial– A different polynomial is defined on each line segment [tk, tk + 1]

– In this case, it would be a piecewise linear polynomial

Interpolating Solutions to IVPs

11

Linear Interpolation

This, however, would be very unsatisfying—we know the solution is both continuous and differentiable

Interpolating Solutions to IVPs

12

Linear Interpolation

Straight lines are not good approximations to differentiable functions– We learned how to draw lines in kindergarten—let’s come up

with something better....

Interpolating Solutions to IVPs

13

Cubic Interpolation

Recall that we have more information—we always have exact values or approximations of

y(1)(tk) and y(1)(tk + 1)

If it is a 1st-order ODE, we can calculatey(1)(tk) = f(tk, yk) and y(1)(tk + 1) = f(tk + 1, yk + 1)

If it is a 2nd- or higher-order ODE, the approximation of the derivatives is in the second row of the output

Interpolating Solutions to IVPs

14

Cubic Interpolation

We can find an interpolating cubic polynomial p(t) that:– Matches the values p(tk) = yk and p(tk + 1) = yk + 1

– Matches the derivatives p(1)(tk) = f(tk, yk) and p(1)(tk + 1) = f(tk + 1, yk + 1)

The general form of a cubic polynomial is

at3 + bt2 + ct + d

and its derivative is

3at2 + 2bt + c + d

Interpolating Solutions to IVPs

15

Solutions to 1st-order IVPs

This creates a system of equations: p(tk) = yk atk

3 + btk2 + ctk + d = yk

p(1)(tk) = f(tk, yk) 3atk2 + btk + c = f(tk,

yk)

p(tk + 1) = yk + 1 atk + 13 + btk + 1

2 + ctk + 1 + d = yk + 1

p(1)(tk + 1) = f(tk + 1, yk + 1) 3atk + 12 + btk + 1 + c = f(tk + 1, yk + 1)

3 2

2

3 211 1 1

21 11 1

1

,3 2 1 0

1

,3 2 1 0

kk k k

k kk k

kk k k

k kk k

yat t t

f t ybt t

yct t t

f t ydt t

Interpolating Solutions to IVPs

16

Solutions to 1st-order IVPs

Thus, if the two points that bracket t are at k and k + 1, the code would be

M = [ t(k)^3 t(k)^2 t(k) 1; 3*t(k)^2 2*t(k) 1 0; t(k + 1)^3 t(k + 1)^2 t(k + 1) 1; 3*t(k + 1)^2 2*t(k + 1) 1 0];

p = M \ [y(k); f(t(k), y(k)); y(k+1); f(t(k+1), y(k+1))];

Interpolating Solutions to IVPs

17

The Matlab mkpp Function

The mkpp function returns a piecewise polynomial data structure in Matlab

The arguments are:– A vector t of n break points—the points defining the sub-intervals– An (n – 1) × 4 matrix where each row is the interpolating cubic

polynomial defined on [0, tk + 1 – tk] interpolating the points

(0, yk) and (tk + 1 – tk, yk)

Interpolating Solutions to IVPs

18

The dpinterp Function

Thus, we could define the function dpinterp:function [pp] = dpinterp( t, y, f ) n = length(t) - 1; P = zeros( n, 4 );

for k=1:n dt = t(k + 1) - t(k);

M = [ 0 0 0 1; 0 0 1 0; dt^3 dt^2 dt 1; 3*dt^2 2*dt 1 0];

p = M \ [y(k); f(t(k), y(k)); y(k+1); f(t(k+1), y(k+1))]; P(k, :) = p'; end

pp = mkpp( t, P );end

Interpolating Solutions to IVPs

19

Matching Derivatives or Splines?

An alternate means of interpolating points are cubic splines:– Rather than matching the derivatives at the end points, we

simply state that at each of the interior points:• The adjacent piecewise cubic polynomials must equal yk,

• The adjacent polynomials must have the same derivative at tk, and

• Adjoining polynomials must have the same second derivative at tk.

The result is an interpolating polynomial that has a twice-differentiable value but may not match the derivatives at the points

Interpolating Solutions to IVPs

20

Matching Derivatives or Splines?

For our example, we have:[t, y] = dp45( @f8a, [0, 2], 0', 0.1, 1e-5 )pp = dpinterp( t, y, @f8a ); pps = spline( t, [f8a(t(1), y(1)) y

f8a(t(end),y(end))] );hold onts = 0:0.001:2;plot( t, y, 'bo' ); plot( ts, ppval( pp, ts ), 'b' )plot( ts, ppval( pps, ts ), 'r' ); plot( ts, y8a_soln( ts ), 'k' )

Interpolating Solutions to IVPs

21

Matching Derivatives or Splines?

However, if you plot the errors, we see that our interpolating polynomial has less overall error and matches the derivativeshold onplot( t, y - y8a_soln(t), 'bo' ); plot( ts, ppval( pp, ts ) - y8a_soln(ts), 'b' )plot( t, t*0, 'ko' ); plot( ts, ppval( pp2, ts ) - y8a_soln(ts),

'r' )

Interpolating Solutions to IVPs

22

Solutions to 2nd-order IVPs

What if you have both derivative and second-derivativeinformation that is easily accessible:– Find an interpolating a quintic polynomial

1,1

2,2

35 4 3 2

1, 144 3 2

2, 153 2

1 16

0 0 0 0 0 1

0 0 0 0 1 0

,0 0 0 1 0 0

1

5 4 3 2 1 0

,20 12 6 2 0 0

k

k

k k

kk k k k k

kk k k k

k kk k k

ya

ya

f ta

yt t t t t a

yt t t t a

f tt t t a

y

y

Interpolating Solutions to IVPs

23

Solutions to 2nd-order IVPs

You can also find interpolating heptic polynomial andwe begin to see a pattern:

1

2

3

47 6 5 4 3 2

56 5 4 3 2

65 4 3 2

74 3 2

8

0 0 0 0 0 0 0 1

0 0 0 0 0 0 1 0

0 0 0 0 0 1 0 0

0 0 0 0 1 0 0 0

1

7 6 5 4 3 2 1 0

42 30 20 12 6 2 0 0

210 120 60 24 6 0 0 0

k k k k k k k

k k k k k k

k k k k k

k k k k

a

a

a

a

t t t t t t t a

t t t t t t a

t t t t t a

t t t t a

1,

2,

3,

1, 1

2, 1

3, 1

1 1

,

,

k

k

k

k k

k

k

k

k k

y

y

y

f t

y

y

y

f t

y

y

Interpolating Solutions to IVPs

24

The dpinterp Function

This allows us to define a general dpinterp function:function [pp] = dpinterp( t, y, f ) [m, n] = size( y ); P = zeros( n - 1, 2*m + 2 ); M = zeros( 2*m + 2, 2*m + 2 ); for i = 1:(m + 1) M(i, 2*m + 3 - i) = factorial( i - 1 ); end for k=1:(n - 1) dt = (t(k + 1) - t(k)).^((2*m + 1):-1:0); for i = 1:(m + 1) M(i, 2*m + 3 - i) = factorial( i - 1 ); M(m + 1 + i, 1:(end + 1 - i)) = dt; dt = dt(2:end).*((2*m + 1):-1:i); end dyk = f( t(k), y(:, k)); dyk1 = f(t(k + 1), y(:, k + 1)); P(k, :) = (M \ [y(:, k); dyk(end); y(:, k + 1); dyk1(end)])'; end pp = mkpp( t, P );end

Interpolating Solutions to IVPs

25

The dpinterp Function

This piecewise quintic is more accurate than a cubic spline:[t, y] = dp45( @f8b, [0, 2], [0 1]', 0.1, 1e-2 );pp = dpinterp( t, y, @f8b );dy = f8b(2, y(:, end)); pps = spline( t, [1 y(1,:) dy(1)] );hold on; plot( ts, ppval( pp, ts ) - y8b_soln( ts ), 'r' );plot( ts, ppval( pps, ts ) - y8b_soln( ts ), 'b' );plot( t, y(1,:) - y8b_soln( t ), 'ro' );

Interpolating Solutions to IVPs

26

The dpinterp Function

What happens if we do not want a 7th degree polynomial?– Can we use less information to make a interpolating polynomial?– We can provide a 4th argument—if the user provides this

argument m, it will uses a polynomial of 2m – 1

Interpolating Solutions to IVPs

27

The dpinterp Function

We now have a dpinterp function that gives maximum choice to the user:

Interpolating Solutions to IVPs

function [pp] = dpinterp( t, y, f, m ) [mp, n] = size( y ); if nargin == 3 m = mp + 1; else m = max( min( mp + 1, m ), 1 ); end P = zeros( n - 1, 2*m ); M = zeros( 2*m, 2*m ); for i = 1:m M(i, 2*m + 1 - i) = factorial( i - 1 ); end

for k=1:(n - 1) dt = t(k + 1) - t(k); dt = dt.^((2*m - 1):-1:0); for i = 1:m M(i, 2*m + 1 - i) = factorial( i - 1 ); M(m + i, 1:(end + 1 - i)) = dt; dt = dt(2:end).*((2*m - 1):-1:i); end if m == mp + 1 dyk = f( t(k), y(:, k)); dyk1 = f(t(k + 1), y(:, k + 1)); P(k, :) = (M \ [y(:, k); dyk(end); y(:, k + 1); dyk1(end)])'; else P(k, :) = (M \ [y(1:m, k); y(1:m, k + 1)])'; end end pp = mkpp( t, P );end

28

The dpinterp Function

Even now, a cubic polynomial is better than a spline:[t, y] = dp45( @f8b, [0, 2], [0 1]', 0.1, 1e-2 );pp = dpinterp( t, y, @f8b, 2 );dy = f8b(2, y(:, end)); pps = spline( t, [1 y(1,:) dy(1)] );hold on; plot( ts, ppval( pp, ts ) - y8b_soln( ts ), 'r' );plot( ts, ppval( pps, ts ) - y8b_soln( ts ), 'b' );plot( t, y(1,:) - y8b_soln( t ), 'ro' );

Interpolating Solutions to IVPs

29

The dpinterp Function

As may be noted, quintics are very good approximations but in this case, clamped cubics are also better than cubic splines

pp = dpinterp( t, y, @f8b, 3 ); pp = dpinterp( t, y, @f8b, 2 );

Interpolating Solutions to IVPs

30

Summary

We have looked approximating solutions to IVPs between the points returned by functions such as dp45:– We discussed piecewise-defined polynomials– We found piecewise cubic polynomials for 1st-order IVPs– We determined that we could get even more accurate

approximations for an Nth-order IVP– The clamped polynomials produce a better result than splines

Interpolating Solutions to IVPs

31

References

[1] Glyn James, Modern Engineering Mathematics, 4th Ed., Prentice Hall, 2007, p.778.

[2] Glyn James, Advanced Modern Engineering Mathematics, 4th Ed., Prentice Hall, 2011, p.164.

[3] J.R. Dormand and P. J. Prince, "A family of embedded Runge-Kutta formulae," J. Comp. Appl. Math., Vol. 6, 1980, pp. 19-26.

Interpolating Solutions to IVPs

top related