Download presentation
Presentation is loading. Please wait.
Published byDwayne Flynn Modified over 9 years ago
1
Interpolating Solutions to IVPs Douglas Wilhelm Harder, M.Math. LEL Department 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 t out and y out, how do we approximate the solution at a point t where t out,k < t < t out,k + 1 We will look at: –Interpolation, and –Dormand Prince 2 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 3 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 t out,k ≈ y out,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? t out,k < t < t out,k + 1 4 Interpolating Solutions to IVPs
5
Introduction We will deal with one 1 st -order and one 2 nd -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 5 Interpolating Solutions to IVPs
6
Approximating Solutions We now know that t k < t < t k + 1 and we know that y(t k ) ≈ u k and y(t k + 1 ) ≈ u k + 1 How do we approximate y(t) ? 6 Interpolating Solutions to IVPs
7
Linear Interpolation As an initial idea, we could interpolate the two points (t k, y k ) and (t k + 1, y k + 1 ) with a straight line... 7 Interpolating Solutions to IVPs
8
Linear Interpolation We can define a separate linear polynomial between each consecutive pair of points from (t 1, y 1 ) to (t n, y n ) 8 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.' ); 9 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 [t k, t k + 1 ] –In this case, it would be a piecewise linear polynomial 10 Interpolating Solutions to IVPs
11
Linear Interpolation This, however, would be very unsatisfying—we know the solution is both continuous and differentiable 11 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.... 12 Interpolating Solutions to IVPs
13
Cubic Interpolation Recall that we have more information—we always have exact values or approximations of y (1) (t k ) and y (1) (t k + 1 ) If it is a 1 st -order ODE, we can calculate y (1) (t k ) = f(t k, y k ) and y (1) (t k + 1 ) = f(t k + 1, y k + 1 ) If it is a 2 nd - or higher-order ODE, the approximation of the derivatives is in the second row of the output 13 Interpolating Solutions to IVPs
14
Cubic Interpolation We can find an interpolating cubic polynomial p(t) that: –Matches the values p(t k ) = y k and p(t k + 1 ) = y k + 1 –Matches the derivatives p (1) (t k ) = f(t k, y k ) and p (1) (t k + 1 ) = f(t k + 1, y k + 1 ) The general form of a cubic polynomial is at 3 + bt 2 + ct + d and its derivative is 3at 2 + 2bt + c + d 14 Interpolating Solutions to IVPs
15
Solutions to 1 st -order IVPs This creates a system of equations: p(t k ) = y k at k 3 + bt k 2 + ct k + d = y k p (1) (t k ) = f(t k, y k ) 3at k 2 + bt k + c = f(t k, y k ) p(t k + 1 ) = y k + 1 at k + 1 3 + bt k + 1 2 + ct k + 1 + d = y k + 1 p (1) (t k + 1 ) = f(t k + 1, y k + 1 ) 3at k + 1 2 + bt k + 1 + c = f(t k + 1, y k + 1 ) 15 Interpolating Solutions to IVPs
16
Solutions to 1 st -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))]; 16 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, t k + 1 – t k ] interpolating the points (0, y k ) and (t k + 1 – t k, y k ) 17 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 18 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 y k, The adjacent polynomials must have the same derivative at t k, and Adjoining polynomials must have the same second derivative at t k. The result is an interpolating polynomial that has a twice- differentiable value but may not match the derivatives at the points 19 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 on ts = 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' ) 20 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 derivatives hold on plot( 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' ) 21 Interpolating Solutions to IVPs
22
Solutions to 2 nd -order IVPs What if you have both derivative and second-derivative information that is easily accessible: –Find an interpolating a quintic polynomial 22 Interpolating Solutions to IVPs
23
Solutions to 2 nd -order IVPs You can also find interpolating heptic polynomial and we begin to see a pattern: 23 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 24 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' ); 25 Interpolating Solutions to IVPs
26
The dpinterp Function What happens if we do not want a 7 th degree polynomial? –Can we use less information to make a interpolating polynomial? –We can provide a 4 th argument—if the user provides this argument m, it will uses a polynomial of 2m – 1 26 Interpolating Solutions to IVPs
27
The dpinterp Function We now have a dpinterp function that gives maximum choice to the user: 27 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 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' ); 28 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 ); 29 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 1 st -order IVPs –We determined that we could get even more accurate approximations for an N th -order IVP –The clamped polynomials produce a better result than splines 30 Interpolating Solutions to IVPs
31
References [1]Glyn James, Modern Engineering Mathematics, 4 th Ed., Prentice Hall, 2007, p.778. [2]Glyn James, Advanced Modern Engineering Mathematics, 4 th 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. 31 Interpolating Solutions to IVPs
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.