lecture5b - Introduction to Computer Programming with...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Computer Programming with MATLAB CEE/MAE M20 Lecture 5b Numerical Interpolation and Differentiation CEE/MAE M20 Numerical Interpolation Suppose we’re given as discrete set of data 2 x 0 0.5 1 1.5 2 2.5 y -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 Could be, for example Measurement data Evaluations of a function Entries in a table CEE/MAE M20 We’d like to ‘fill in the gaps’ between the discrete points. There are many ways to do this. 3 CEE/MAE M20 We’d like to ‘fill in the gaps’ between the discrete points. There are many ways to do this. For example, linear interpolation 4 x 0 0.5 1 1.5 2 2.5 y -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 CEE/MAE M20 We’d like to ‘fill in the gaps’ between the discrete points. There are many ways to do this. For example, shape-preserving cubic interpolation 5 x 0 0.5 1 1.5 2 2.5 y -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 Slopes are determined such that the function values do not overshoot the data values, at least locally. The resulting interpolant has second-order continuity. CEE/MAE M20 We’d like to ‘fill in the gaps’ between the discrete points. There are many ways to do this. For example, cubic spline interpolation 6 x 0 0.5 1 1.5 2 2.5 y -2 -1.5 -1 -0.5 0 0.5 1 1.5 2
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
CEE/MAE M20 Each of these uses piecewise polynomials between neighboring points. This is the most common interpolation approach. Here, we’ll discuss two approaches and implement one of them Linear interpolation Shape-preserving piecewise cubic Hermite interpolation (‘PCHIP’) Another approach would be to find a single polynomial that passes through all of the points (a curve fit ). A curve fit is generally a bad approach , especially when there are more than 2 or 3 points to fit. 7 CEE/MAE M20 Linear interpolation You probably already know how to do this 8 In the interval we form a linear polynomial P ( x ) = y k + y k +1 - y k x k +1 - x k ( x - x k ) x k x < x k +1 This ensures that P ( x k ) = y k P ( x k +1 ) = y k +1 The points x 1 , x 2 , … x k , …, x n are called the nodes y k y k+1 y k-1 x k x k+1 x k-1 y k+2 x k+2 P(x) x CEE/MAE M20 Let’s define some quantities that will make this a bit tidier 9 y k y k+1 y k-1 x k x k+1 x k-1 y k+2 x k+2 x δ k Divided difference δ k = y k +1 - y k x k +1 - x k h k Interval length h k = x k +1 - x k Also the slope of the line Local variable s s = x - x k P ( x ) = y k + δ k s So we can write our linear polynomial as P(x) CEE/MAE M20 We wish to construct a linear interpolation function, LinInterp Here is the specification: Given an array of points x with an associated array of data values y , linearly interpolate at a point xi to obtain the value yi of the polynomial at xi 10 x y xi yi LinInterp CEE/MAE M20 The pseudocode for LinInterp 11 Check that x and y are the same length Check that xi falls inside the range of x Find the value of k such that x(k) xi < x(k+1) Compute s = xi x(k) Compute h = x(k+1) x(k) Compute del = (y(k+1) y(k))/h Compute yi = y(k) + del*s CEE/MAE M20 The pseudocode for LinInterp 12 Set k = 1 Set n = length(x) while k < n 1 and xi
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern