Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Piecewise cubic Hermite interpolation, 15pts) Union Pacific has decided to reop

ID: 3596610 • Letter: #

Question

(Piecewise cubic Hermite interpolation, 15pts) Union Pacific has decided to reopen the Boise Train Depot for rail travel. They have contracted your computational science consulting company to design a new switching path between two of the rail lines (Track A and B) that enter the Depot. The requirements for the path are that it pass through the points (0,0), (21), and (4,2) (see Figure below). Furthermore, the path should be tangent to the line y = 0 at (0,0), tangent to the line y = 2 at (42), and have a slope of 19/22 at x = 2. Track B 4.2 0,0) TrackA (a) Since you are given both the function and derivative values at each of the node points, you decide to use a piecewise cubic Hermite polynomial to model the switching path. Analytically compute the piecewise polynomial for the intervals [-1,0].10, 2112,41.14,5] (see Section 3.3 of the NCM book). (b) Use the mkpp function in MATLAB to make a piecewise polynomial representation for the train track. (c) Use ppval with the piecewise polynomial function you created from (b) to make a nice smooth plot of the solution from part (a) for Union Pacific. If you did everything correctly, the value of the piecewise polynomial at x = 2.5 should be 985/704·

Explanation / Answer

Interpolating Polynomial:

we all recognize that points decide a directly line. greater exactly any factors inside the aircraft, (x1, y1) and (x2, y2), with x1 = x2, determine a completely unique firstdegreepolynomial in x whose graph passes thru the two points. There are many unique formulas for the polynomial, however all of them cause the identical straight

line graph. This generalizes to extra than two factors. Given n points in the plane, (xk, yk), okay = 1, . . . , n, with wonderful xk’s, there is a completely unique polynomial in x of degree much less than n whose graph passes thru the points. it's miles easiest to remember the fact that n, the variety of records points, is also the range of coefficients, despite the fact that a number of the main coefficients might be zero, so the diploma would possibly certainly be much less than n 1.once more, there are many one of kind formulation for the polynomial, however all of them outline theequal characteristic.
This polynomial is called the interpolating polynomial because it precisely reproduces
the given statistics:
P(xk) = yk, ok = 1, . . . , n.
we observe other polynomials, of decrease diploma, that handiest approximate the
information. they're not interpolating polynomials.

2}

P = polyinterp(x,y,symx)
quite(P)
which produces
P =
(x*(x - 1)*(x - three))/2 + 5*(x/2 - 1)*(x/3 - 1)*(x - 1) +
(sixteen*x*(x/2 - 1/2)*(x - 2))/3 - 6*x*(x/2 - three/2)*(x - 2)
/ x
sixteen x | - - 1/2 | (x - 2)
x (x - 1) (x - three) / x / x 2 /
----------------- + 5 | - - 1 | | - - 1 | (x - 1) + ------------------------
2 2 / three / three
/ x
- 6 x | - - 3/2 | (x - 2)
2 /
This expression is a rearrangement of the Lagrange shape of the interpolating polynomial.
Simplifying the Lagrange form with
P = simplify(P)
adjustments P to the strength form
P =
x^3 - 2*x - five

x = 1:6;
y = [16 18 21 17 15 12];
disp([x; y])
u = .seventy five:.05:6.25;
v = polyinterp(x,y,u);
plot(x,y,’o’,u,v,’r-’);
produces
1 2 three 4 5 6
16 18 21 17 15 12

with best six nicely spaced points, we are able to start to
see the number one difficulty with full-degree polynomial interpolation. In between
the statistics points, mainly within the first and closing subintervals, the feature suggests
immoderate variant. It overshoots the adjustments inside the facts values. As end result, fulldegree
polynomial interpolation is hardly ever used for statistics and curve Itsprimary software is in the derivation of other numerical strategies.

-----Linear Interpolation:------

create a simple image of the records set from the closing segment by plotting the records twice, once with circles at the facts factors and once with straight traces connecting the factors. the following statements produce parent three.3.
x = 1:6;
y = [16 18 21 17 15 12];
plot(x,y,’o’,x,y,’-’);
To generate the lines, the Matlab images routines use piecewise linear interpolation.The algorithm sets thedegree for extra sophisticated algorithms. three quantities are concerned. The interval index okay need tobe determined so that
xk x < xk+1.
The neighborhood variable, s, is given through
s = x xk.
the primary divided distinction is
okay =
yk+1 yk
xk+1 xk
.
With those portions in hand, the interpolant is
L(x) = yk + (x xk)
yk+1 yk
xk+1 xk

feature v = piecelin(x,y,u)
%PIECELIN Piecewise linear interpolation.
% v = piecelin(x,y,u) reveals the piecewise linear L(x)
% with L(x(j)) = y(j) and returns v(k) = L(u(k)).
% First divided difference
delta = diff(y)./diff(x);
% discover subinterval indices k so that x(okay) <= u < x(okay+1)
n = length(x);
k = ones(size(u));
for j = 2:n-1
k(x(j) <= u) = j;
stop
% examine interpolant
s = u - x(k);
v = y(ok) + s.*delta(k);

b)p = pchip(x,y,xq)
pp = pchip(x,y)
p = pchip(x,y,xq) returns a vector of interpolated values p similar to the query factors in xq. The values of p are decided via shape-keeping piecewise cubic interpolation of x and y
pp = pchip(x,y) returns a piecewise polynomial shape to be used with ppval and the spline utility unmkpp.

examine the interpolation consequences produced via spline and pchip for 2 different features.

Create vectors of x values, characteristic values at those factors y, and query points xq. Compute interpolations on the query points the use of each spline and pchip. Plot the interpolated feature values on the query factors for contrast.
x = -3:3;
y = [-1 -1 -1 0 1 1 1];
xq1 = -three:.01:three;
p = pchip(x,y,xq1);
s = spline(x,y,xq1);
plot(x,y,'o',xq1,p,'-',xq1,s,'-.')
legend('pattern factors','pchip','spline','vicinity','SouthEast')
chip is favorable since it does no longer oscillate as freely among the sample points.

perform a second contrast using an oscillatory sample characteristic.

vectors for the x values and function values y, and then use pchip to construct a piecewise polynomialstructure.

x = zero:25;
y = besselj(1,x);
xq2 = zero:0.01:25;
p = pchip(x,y,xq2);
s = spline(x,y,xq2);
plot(x,y,'o',xq2,p,'-',xq2,s,'-.')
legend('pattern factors','pchip','spline')

x = -five:five;
y = [1 1 1 1 0 0 1 2 2 2 2];
p = pchip(x,y);

output arguements

Interpolated values at question factors, again as a vector, matrix, or array. the scale of p depends at thesizes of the inputs:

If y is a vector, then p is a vector that has the identical duration as xq.
If y has two or greater dimensions denoted via n, then p has size [size(y,1) size(y,2) ... size(y,n-1) length(xq)].for instance, if y is a matrix, then p is of length [size(y,1) length(xq)].

c) Piecewise polynomial

Piecewise polynomial, back as a shape. Use this structure with the ppval function to assess the interpolating polynomials at one or extra question factors. The structure has these fields.

subject Description
shape
'pp' for piecewise polynomial
breaks
Vector of duration L+1 with strictly increasing elements that constitute the begin and give up of each of Lperiods
coefs
L-by-k matrix with each row coefs(i,:) containing the local coefficients of an order k polynomial at the ith c programming language, [breaks(i),breaks(i+1)]
pieces
wide variety of portions, L
order
Order of the polynomials
dim
Dimensionality of goal
the polynomial coefficients in coefs are neighborhood coefficients for every interval, you should subtract the lower endpoint of the corresponding knot c language to use the coefficients in a conventionalpolynomial equation. In other phrases, for the coefficients [a,b,c,d] on the c language [x1,x2].

Example Syntax

characteristic v = pchiptx(x,y,u)
%PCHIPTX Textbook piecewise cubic Hermite interpolation.
% v = pchiptx(x,y,u) reveals the shape-maintaining piecewise
% P(x), with P(x(j)) = y(j), and returns v(ok) = P(u(k)).
%
% See PCHIP, SPLINETX.
% First derivatives
h = diff(x);
delta = diff(y)./h;
d = pchipslopes(h,delta);
% Piecewise polynomial coefficients
n = length(x);
c = (three*delta - 2*d(1:n-1) - d(2:n))./h;
b = (d(1:n-1) - 2*delta + d(2:n))./h.^2;
% locate subinterval indices ok so that x(ok) <= u < x(k+1)
ok = ones(length(u));
for j = 2:n-1
k(x(j) <= u) = j;
end
% Evaluate interpolant
s = u - x(k);
v = y(k) + s.*(d(k) + s.*(c(k) + s.*b(k)));
The code for computing the pchip slopes uses the weighted harmonic mean
at interior breakpoints and a one-sided formula at the endpoints.
function d = pchipslopes(h,delta)
% PCHIPSLOPES Slopes for shape-preserving Hermite cubic
% pchipslopes(h,delta) computes d(k) = P’(x(k)).
% Slopes at interior points
% delta = diff(y)./diff(x).
% d(k) = 0 if delta(k-1) and delta(k) have opposites
% signs or either is zero.
% d(k) = weighted harmonic mean of delta(k-1) and
% delta(k) if they have the same sign.
16 Chapter 3. Interpolation
n = length(h)+1;
d = zeros(size(h));
k = find(sign(delta(1:n-2)).*sign(delta(2:n-1))>0)+1;
w1 = 2*h(okay)+h(k-1);
w2 = h(okay)+2*h(k-1);
d(ok) = (w1+w2)./(w1./delta(okay-1) + w2./delta(ok));
% Slopes at endpoints
d(1) = pchipend(h(1),h(2),delta(1),delta(2));
d(n) = pchipend(h(n-1),h(n-2),delta(n-1),delta(n-2));
feature d = pchipend(h1,h2,del1,del2)
% Noncentered, form-maintaining, three-point components.
d = ((2*h1+h2)*del1 - h1*del2)/(h1+h2);
if sign(d) ~= sign(del1)
d = 0;
elseif (signal(del1)~=sign(del2))&(abs(d)>abs(three*del1))
d = 3*del1;
end
The splinetx M-record computes the slopes by setting up and solving a tridiagonal
machine of simultaneous linear equations.
feature d = splineslopes(h,delta);
% SPLINESLOPES Slopes for cubic spline interpolation.
% splineslopes(h,delta) computes d(ok) = S’(x(k)).
% uses no longer-a-knot end situations.
% Diagonals of tridiagonal system
n = period(h)+1;
a = zeros(length(h)); b = a; c = a; r = a;
a(1:n-2) = h(2:n-1);
a(n-1) = h(n-2)+h(n-1);
b(1) = h(2);
b(2:n-1) = 2*(h(2:n-1)+h(1:n-2));
b(n) = h(n-2);
c(1) = h(1)+h(2);
c(2:n-1) = h(1:n-2);
% proper-hand facet
r(1) = ((h(1)+2*c(1))*h(2)*delta(1)+ ...
h(1)^2*delta(2))/c(1);
r(2:n-1) = three*(h(2:n-1).*delta(1:n-2

------*---------

end