Interpolating Solutions to IVPs Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,

Slides:



Advertisements
Similar presentations
Splines and Piecewise Interpolation
Advertisements

ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Interpolation A method of constructing a function that crosses through a discrete set of known data points. .
Differentiation and Richardson Extrapolation
CS 445/645 Fall 2001 Hermite and Bézier Splines. Specifying Curves Control Points –A set of points that influence the curve’s shape Knots –Control points.
Spline Functions – An Elegant View of Interpolation Bruce Cohen David Sklar
MATH 685/ CSI 700/ OR 682 Lecture Notes
Chapter 18 Interpolation The Islamic University of Gaza
1 Chapter 4 Interpolation and Approximation Lagrange Interpolation The basic interpolation problem can be posed in one of two ways: The basic interpolation.
1 Curve-Fitting Spline Interpolation. 2 Curve Fitting Regression Linear Regression Polynomial Regression Multiple Linear Regression Non-linear Regression.
Topics in Applied Mathematics Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada.
Chapter 3, Interpolation and Extrapolation. Interpolation & Extrapolation (x i,y i ) Find an analytic function f(x) that passes through given N points.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Curve-Fitting Interpolation
1cs426-winter-2008 Notes  Ian Mitchell is running a MATLAB tutorial, Tuesday January 15, 5pm-7pm, DMP 110 We won’t be directly using MATLAB in this course,
王俊鑫 (Chun-Hsin Wang) 中華大學 資訊工程系 Fall 2002 Chap 2 Numerical Methods for First-Order Differential Equations.
ECIV 301 Programming & Graphics Numerical Methods for Engineers REVIEW II.
Boundary-value Problems and Finite-difference Equations Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Heat-conduction/Diffusion Equation Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Fin500J Topic 7Fall 2010 Olin Business School 1 Fin500J Mathematical Foundations in Finance Topic 7: Numerical Methods for Solving Ordinary Differential.
Tort Law: Negligence Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada.
MATH 212 NE 217 Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada Copyright © 2011.
The Wave Equation Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
The Shooting Method for Boundary-value Problems
CMPS1371 Introduction to Computing for Engineers NUMERICAL METHODS.
MATH 212 NE 217 Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada Copyright © 2011.
CS 376 Introduction to Computer Graphics 04 / 23 / 2007 Instructor: Michael Eckmann.
Proof by Induction.
The Crank-Nicolson Method and Insulated Boundaries
MATH 212 NE 217 Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada Copyright © 2011.
Numerical Computation
Review of Interpolation. A method of constructing a function that crosses through a discrete set of known data points.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
MATH 212 NE 217 Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada Copyright © 2011.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Computers in Civil Engineering 53:081 Spring 2003 Lecture #15 Spline Interpolation.
Lecture 16 - Approximation Methods CVEN 302 July 15, 2002.
The Wave Equation Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada.
Introduction to Numerical Analysis I MATH/CMPSC 455 Splines.
Basic Theory (for curve 02). 1.3 Parametric Curves  The main aim of computer graphics is to display an arbitrary surface so that it looks real.  The.
MATH 212 NE 217 Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada Copyright © 2011.
Recap Cubic Spline Interpolation Multidimensional Interpolation Curve Fitting Linear Regression Polynomial Regression The Polyval Function The Interactive.
Interpolation Search Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada.
Mohiuddin Ahmad SUNG-BONG JANG Interpolation II (8.4 SPLINE INTERPOLATION) (8.5 MATLAB’s INTERPOLATION Functions)
L5 – Curves in GIS NGEN06 & TEK230: Algorithms in Geographical Information Systems by: Sadegh Jamali (source: Lecture notes in GIS, Lars Harrie) 1 L5-
Cubic Spline Interpolation. Cubic Splines attempt to solve the problem of the smoothness of a graph as well as reduce error. Polynomial interpolation.
Parametric Curves CS 318 Interactive Computer Graphics John C. Hart.
MATH 212 NE 217 Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada Copyright © 2011.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
1 Lecture 8 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Chapters 5 and 6: Numerical Integration Code development trapezoid rule Simpson’s rule Gauss quadrature Laguerre quadrature Analysis changing the variable.
Interpolation - Introduction
NUMERICAL DIFFERENTIATION Forward Difference Formula
Curve-Fitting Spline Interpolation
Interpolation Methods
Chapter 3, Interpolation and Extrapolation
Chapter 15 Curve Fitting : Splines
Boundary-value problems and Finite-difference equations
Interpolation Methods
Splines There are cases where polynomial interpolation is bad
Chapter 3, Interpolation and Extrapolation
Presentation transcript:

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 © 2012 by Douglas Wilhelm Harder. Some rights reserved.

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

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

Introduction Suppose that we found an approximation to an initial- value problem: [t_out, y_out] = [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 Interpolating Solutions to IVPs

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

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

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

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

Linear Interpolation This is no different than connecting the dots with straight lines... [t6c, y6c] = [0, 3], [0, 1]', 1, 1e-1 ); plot( t6c, y6c(1,:), '-b.' ); 9 Interpolating Solutions to IVPs

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

Linear Interpolation This, however, would be very unsatisfying—we know the solution is both continuous and differentiable 11 Interpolating Solutions to IVPs

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

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

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

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 bt k ct k d = y k + 1 p (1) (t k + 1 ) = f(t k + 1, y k + 1 ) 3at k bt k c = f(t k + 1, y k + 1 ) 15 Interpolating Solutions to IVPs

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

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

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 = [ ; ; 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

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

Matching Derivatives or Splines? For our example, we have: [t, y] = [0, 2], 0', 0.1, 1e-5 ) pp = dpinterp( t, ); 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

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

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

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

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 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 i) = factorial( i - 1 ); M(m i, 1:(end 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

The dpinterp Function This piecewise quintic is more accurate than a cubic spline: [t, y] = [0, 2], [0 1]', 0.1, 1e-2 ); pp = dpinterp( t, ); 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

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

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 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 i) = factorial( i - 1 ); M(m + i, 1:(end 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

The dpinterp Function Even now, a cubic polynomial is better than a spline: [t, y] = [0, 2], [0 1]', 0.1, 1e-2 ); pp = dpinterp( t, 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

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, 3 );pp = dpinterp( t, 2 ); 29 Interpolating Solutions to IVPs

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

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 Interpolating Solutions to IVPs