+ All Categories
Home > Documents > Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf ·...

Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf ·...

Date post: 28-May-2020
Category:
Upload: others
View: 4 times
Download: 0 times
Share this document with a friend
46
Week #5 : Non-linear solvers: Secant, fzero; Interpolation Goals: Further non-linear equation solvers: secant and fzero. Interpolation with polynomials, splines and pchip.
Transcript
Page 1: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Week #5 : Non-linear solvers: Secant, fzero; Interpolation

Goals:

• Further non-linear equation solvers: secant and fzero.

• Interpolation with polynomials, splines and pchip.

Page 2: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Solving Non-Linear Equations - Secant - 1

Secant Method

Bisection isn’t that sophisticated: it always picks the middle point between twoothers. What if you know one value is closer to zero? What if you had otherinformation?Concept: we know more than just the sign of our answers (too far, too close inthe landing model): we also know how close each point is to f (x) = zeroWe can use that information to speed up our search.

• Pretend that f (x) is linear, through the starting points (a, f (a)), and (b, f (b)).

• Connect those dots with a line to get an estimate of f ′(x).

• Find c, the root of the line defined by the two points, and include that in ournew updated a and b.

Page 3: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Solving Non-Linear Equations - Secant - 2

Graphical ExampleWe want to find the x values where the graph below crosses y = 0.

• Suppose we start at initial guess of a = 4.5, b = 4.8.

• Draw a line through the pair of points, and follow it to its root, x = c.

• Replace a with b, and b with the new point c.

• Repeat the last two steps until a and b are almost the same, or don’t changebetween iterations.

Page 4: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Solving Non-Linear Equations - Secant - 3

Page 5: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Solving Non-Linear Equations - Secant - 4

Computational Algorithm

The previous graph was the graph of

f (x) = ex/2 − x− 10, which came from the equation ex/2 = x + 10

Exercise: Use an anonymous function to define this function in MATLAB.

Page 6: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Solving Non-Linear Equations - Secant - 5

Question: How do we calculate the root of the line between x = a and x = b?

Page 7: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Solving Non-Linear Equations - Secant - 6

Exercise: Implement the secant algorithm for root-finding as a function inthe file named secant.m

Input: f (x), a, b, tol

Exercise: Test the secant method on the equation

ex/2 = x + 10

Exercise: Test the secant method on the ballistics problem from earlier in thisclass.

Page 8: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Solving Non-Linear Equations - Secant - 7

Comments

The secant method is more sophisticated than the bisection method. It will gen-erally converge faster than bisection to the correct solution.

• The starting values of x = a and x = b must be close to the root (seeassignment).

• For high-precision answers, the secant method gets much more precise answersmore quickly than bisection.

• Both bisection and secant are vastly superior to using loops to find high-accuracy approximate solutions.

Page 9: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Solving Non-Linear Equations - fzero - 1

Other Non-Linear Solvers

We have seen the following ways to identify solutions to non-linear equations.

Loops Bisection Secant method

Exercise: Rank them by increasing difficulty of coding, and by increasing speedof convergence.

Looking at how we went from Bisection to Secant methods, using more information,it should not be a surprise that more complex methods exist that will converge evenmore quickly.

Page 10: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Solving Non-Linear Equations - fzero - 2

For anyone having to write their own quick-and-dirty equation solver, bisection orsecant are usually sufficient. However, more complicated (but often more reliable)techniques exist and are implemented in programs like MATLAB.Exercise: Look up the help on fzero. What does it do, and what algorithmdoes it use?

Exercise: Modify our script for secant to use fzero instead.

Page 11: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Interpolation Application - 1

Interpolation Application

At the beginning of the course, we considered the design of a roller-coaster. Inthat design, the track will be supported by a set of fixed supports. The track itself,defined by a mathematical curve, must pass smoothly and precisely throughthe location of each support.

Key idea: interpolation defines a curve or function through a set of points.

Page 12: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Interpolation Application - 2

Other Applications

Design Analysis Data

Design Analysis Data

Page 13: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Interpolation Application - 3

Terminology

Interpolation functions are defined first and foremost by the user-provided controlpoints, nodes, or knots. For a function of one variable, where the interpolation isa function y = f (x), the control points are a list of (xi, yi) pairs.

Page 14: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Interpolation Application - 4

Interpolation schemes

• Given the same control points, nodes, or knots, many interpolations are possi-ble.

Page 15: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Interpolation Application - 5

Choosing an interpolation

Since there are an infinite number of ways to interpolate a set of n points, ‘good’interpolation will be defined by application. Typically, interpolation functionsy = f (x) should be

• Strict interpolants

– yi = f (xi) for all control points, meaning the curve goes through all controlpoints

• Continuous

• Continuous derivative

• Smooth (2nd derivative is continuous)

Interpolants are not curve-fitting or least-squares fitting to data; those are linesnear a set of points, while interpolants must pass through the given points.

Page 16: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Interpolation Application - 6

Families of Interpolants

Based on families of functions we know, what might make a good general-purposeinterpolating function?

Page 17: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Our First Interpolant - 1

Our First Interpolant

Example: Consider the points (-1, 1), (0, 0), and (1, 2). What degree polynomialcould pass through these points uniquely?

Exercise: Start a new script called W5 1.m. Have it create the vectors for x andy, above, in MATLAB.

Exercise: Look up the command polyfit, and use it to find a polynomial thatfits through the given points.

Exercise: Look up the command polyval, and use it to generate a plot ofboth the points and interpolating polynomial on the same graph. This will verifywhether you found the correct polynomial.

Page 18: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Our First Interpolant - 2

Second Example

Exercise: Fit and plot a polynomial to the data in W5 2.m. Give the formulafor the polynomial found.

x = [0, 1, 2, 3, 4, 5];

y = [0, 1, 1.5, 1.75, 1.88, 1.94];

Page 19: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Our First Interpolant - 3

Extrapolation

Exercise: For the polynomial fit we just found, extend the graph out to x = 10.Comment on the shape.

Page 20: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Our First Interpolant - 4

Let’s add two more points, to show the graph flattening out:

x = [0, 1, 2, 3, 4, 5, 10, 20];

y = [0, 1, 1.5, 1.75, 1.88, 1.94, 2, 2];

Sketch what you would want as an interpolant for these points.

Exercise: Compute and plot the polynomial interpolant for these points. Com-ment on the shape.

Page 21: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Our First Interpolant - 5

Based on what we know about polynomial functions, explain why you see theeffects you saw for

• extrapolation

• unsatisfactory interpolation

Page 22: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Piecewise Interpolation - 1

Piecewise Interpolation

The “polynomial wiggle” and lack of convergence in extrapolation come from fittinga single polynomial through all the points. So why not fit different functionsto subsets of the input points?Sketch a piece-wise linear interpolation of the points below.

Comment on the linear interpolation, indicating benefits and shortcomings.

Page 23: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Piecewise Interpolation - 2

Splines In MATLAB

W5 3.m contains the same data as W5 2.m, but it uses a spline interpolant insteadof a polynomial. Here is the interpolation code:

xx = linspace(0, 20);

pp = spline(x, y);

yy = ppval(pp, xx);

Exercise: Run W5 3.m to see how a spline interpolant fits through the givenpoints. Comment on the fit and shape of the curve.

Page 24: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Piecewise Interpolation - 3

Piecewise Cubic Interpolation

So what are the “splines” the MATLAB is creating? They are like the piecewiselinear interpolants we sketched earlier, except they use cubic polynomials on eachinterval.Polynomials of degree 3 (cubics) capture many of our desired properties for inter-polation curves. They are/have

• Continuous

• Continuous derivatives

• Continuous second derivatives

Page 25: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Piecewise Interpolation - 4

Cubic Splines: The Math

To understand how MATLAB actually defines the cubic splines, it will help tointroduce some notation.On each interval i, between points xi and xi+1, we fit a cubic polynomial: Pi(x) =aix

3 + bix2 + cix + di.

We require the following, given the control points, (xi, yi),

• Pi(xi) = yi and Pi(xi+1) = yi+1

– cubic on one interval must go through the data point at each end.

• P ′i (xi) = P ′i+1(xi)

– derivative must remain continuous at data points.

• P ′′i (xi) = P ′′i+1(xi)

– second derivative must remain continuous at data points.

Page 26: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Piecewise Interpolation - 5

The following graphs illustrate what each level of continuity (values, derivs,second derivs) would look like.

Page 27: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Computing Spline Coefficients - 1

Computing Spline Coefficients

How do those constraints lead to defining the necessary cubic functions? Like thetruss problems we saw earlier, each constraint is really an equation. Even better,these equations are linear in each cubic’s coefficients: any computer softwaresystem can easily and quickly be programmed to construct a spline from a set ofcontrol points.

Page 28: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Computing Spline Coefficients - 2

Spline End Behaviour

We saw earlier that we could further refine the shape of the spline by selecting theslopes at the end. This is because, to uniquely define a spline, the user needs toprovide both the set of (xi, yi) points, and two additional pieces of information (orassumptions). Typically, these last two constraints define the end behaviour ofthe splines (what happens at the first and last control points). The most appro-priate choice will depend on the application area.Standard options are

• Known derivative: P ′1(x1) = v1, P′n−1(xn) = vn

• Natural spline: P ′′1 (x1) = P ′′n−1(xn) = 0

• “Not a knot”: P ′′′1 (x2) = P ′′′n−1(xn−1) = 0

Page 29: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Computing Spline Coefficients - 3

MATLAB - Specifying End Behaviour

How can we select the desired end behaviour in MATLAB?Exercise: Modify W5 3.m so that the spline interpolation line reads

pp = spline(x, [1 y 0]);

What changed in the plot of the interpolant?

Modify the values with the y vector. What properties do those values define in theinterpolating function?

Page 30: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Computing Spline Coefficients - 4

Splines Summary

• Splines are the mostly widely used robust interpolation approach. “If in doubt,use splines”.

• One (relatively minor) problem with splines is they are not monotone; theycan have some ‘wiggle’ like polynomial interpolation.

•We’ll see an alternative to splines shortly.

Page 31: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Splines in MATLAB - 1

Interpolation Review

• Interpolation is the generation of a function that goes exactly through a setof n user-provided control points, (x1, y1), (x2, y2), . . . , (xn, yn).

•We saw last class that for a set of n points, a unique degree n− 1 polynomialcan be found that interpolates the points.

• Fitting one single polynomial through a set of points produced some undesirableproperties, so we next looked at piece-wise interpolating options, specificallysplines.

Page 32: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Splines in MATLAB - 2

Splines

Based on desired smoothness properties, we defined spline interpolants, whichare piecewise cubic polynomials.How would you write out such a function by hand?

Without any restrictions, there are many possibilities for a cubic polynomial oneach interval. To narrow our choice, we make the spline interpolant unique byrequiring

• Continuity at the control points.

• Continuous derivatives at the control points.

• Continuous second derivatives at the control points.

• User-selected properties at the end points (e.g. end slopes).

Page 33: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Splines in MATLAB - 3

Splines in MATLAB

Exercise: starting with the script W5 4.m, use MATLAB to generate the splineinterpolant

• through (0, 0), (2, 0.2), (4, 2), and (6, 1.5), and

• with end slopes P ′(0) = 0.5, and P ′(6) = −1

Page 34: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Splines in MATLAB - 4

Splines in MATLAB (cont.)

Somehow, the spline command has created a sequence of n−1 cubic polynomialsthat, connected together over the appropriate intervals between the points, makea smooth function.On the i-th interval, the function will be of the form

Pi(x) = ai(x− xi)3 + bi(x− xi)

2 + ci(x− xi) + di

The cubic polynomial coefficients ai, bi, ci and di are called the spline coefficients.If you know the spline coefficients and the control points, you have the (piecewise)function that defines the overall spline interpolant, P (x).

Page 35: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Splines in MATLAB - 5

To get the spline coefficients in MATLAB for this example, use

>> spline_info = spline(x, [0.5 y -1])

spline_info =

form: ’pp’

breaks: [0 2 4 6]

coefs: [3x4 double]

pieces: 3

order: 4

dim: 1

Page 36: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Splines in MATLAB - 6

Unusually, spline_info is not a matrix; it is a MATLAB data structure. Thesyntax to look into the coefs part of the spline_info is

>> spline_info.coefs

ans =

0.1925 -0.5850 0.5000 0

-0.1775 0.5700 0.4700 0.2000

0.0300 -0.4950 0.6200 2.0000

>> spline_info.breaks

ans =

0 2 4 6

Interpret the matrix of coefficients displayed.

Page 37: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Splines in MATLAB - 7

Graphically

Page 38: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Splines in MATLAB - 8

Using Splines

There are two ways spline interpolants are typically used.1) Simple plotting of the interpolant.

• If you aren’t using the interpolant for any computational reason, but just wanta plot of the graph, MATLAB makes that possible using just two lines.

xx = linspace(-0.5, 6.5, 1000);

yy = spline(x, [0.5 y -1], xx);

plot(x, y, ’.’);

hold on;

plot(xx, yy);

Page 39: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Splines in MATLAB - 9

2) Finding the spline coefficients, then use the implied function to estimate inte-grals, derivatives, or any other value of interest.

• You want to be able to evaluate the function at your own points, or performother mathematical operations on it.

% store spline coefficients, control points

spline_info = spline(x, [0.5 y -1]);

% Evaluate spline at x=0.5,1.0 and 1.5

xx = [0.5, 1.0, 1.5];

yy = ppval(spline_info, xx)

yy =

0.1278 0.1075 0.0834

Refer to spline, ppval in MATLAB help for more examples.

Page 40: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Monotone Interpolants - 1

Hermitian Interpolation

Despite all of their good qualities, sometimes splines do not provide the right shapefor an interpolant in an application. To allow more user control, we next consideranother piecewise cubic interpolant.If we relax the spline requirement that P ′′i (xi) = P ′′i+1(xi), we can define more gen-eral piecewise cubic interpolations. The problem with doing that is immediately wehave fewer constraints (more freedom), but then more user information is required(more user work).Hermite cubic polynomials are piecewise cubics that are defined uniquely by theuser providing

• control points (xi, yi) (or “knots”),

• values of the derivative/slope at the control points, and

• end behaviour.

Being able to set the slope at each control point gives the user much more controlon the final curve shape than splines.

Page 41: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Monotone Interpolants - 2

Monotone Piecewise Cubics

Problem: as a user, we rarely want to define all the derivatives at the controlpoints: that’s usually too much freedom! (In general, we don’t know the derivativeat the control points, so what values would we pick?)To address this problem, Fritsch and Carlson developed a Hermitian polynomialinterpolant which:

• estimates ‘reasonable’ derivatives at each control point.

• has a zero derivative if the control point is a local (data) max or min.

These two things guarantee that the interpolation will be locally monotone. Thepchip function in MATLAB generates monotone Hermitian interpolants.

Page 42: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Monotone Interpolants - 3

Monotone Interpolation in MATLAB

From W5_5.m:

x = [0 1 2 3 4 5];

y = [0 0 3 4 5 5];

xx = linspace(0, 5);

% Plot the control points

plot(x, y, ’.’, ’MarkerSize’, 25);

hold all

% Plot the spline interpolant with dashed lines

yy = spline(x, y, xx);

plot(xx, yy, ’-.’);

% Plot the pchip/monotone interpolant with solid lines

yy = pchip(x, y, xx);

plot(xx, yy);

hold off;

legend(’data’, ’spline’, ’pchip’, ’location’, ’northwest’);

Page 43: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Monotone Interpolants - 4

Page 44: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Monotone Interpolants - 5

Splines vs PCHIP Comparison

• Splines are smoother than pchip interpolants (pchip has discontinuous secondderivatives at the data points).

• Splines generally fit underlying smooth functions better than pchip.

• pchip will guarantee that if your data is monotone increasing or decreasing, theinterpolation function will be too.

• Splines are much more commonly used “in the wild”.

• Choice ultimately depends on application.

Page 45: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Interpolation Examples - 1

Interpolation Examples

We will now compare spline and pchip interpolants on a few different datasets, andpractice use the relation MATLAB resources for piecewise functions to estimateinterpolated values.Exercise: Run W5_6.m, and add and plot both a spline and pchip interpolantto the given data points.Compare the graphs of the interpolants.

Compare the y value at x = 4.75 for both predictions.

Page 46: Week #5 : Non-linear solvers: Secant, fzero; Interpolation ...math272/Notes/Week05/notes05.pdf · Solving Non-Linear Equations - fzero - 1 Other Non-Linear Solvers We have seen the

Interpolation Examples - 2

Exercise: Open W5_7.m, which contains national census total population figuresfor Canada. Add code so it plots both the pchip and spline interpolants.Use the interpolants to estimate the population of Canada in August of 1914 (startof World War I).

How could you estimate the rate of population growth in 1914, based on theinterpolated graph?


Recommended