%% Create Piecewise Polynomial with Repeated Pieces
% Create and plot a piecewise polynomial with four intervals that alternate between two quadratic polynomials.
% The first two subplots show a quadratic polynomial and its negation shifted to the intervals [-8,-4] and [-4,0]. The polynomial is
% 1-\left(\frac{x}{2}-1\right)^2=\frac{-x^2}{4}+x.
% The third subplot shows a piecewise polynomial constructed by alternating these two quadratic pieces over four intervals. Vertical lines are added to show the points where the polynomials meet.

subplot(2,2,1)
cc = [-1/4 1 0]; 
pp1 = mkpp([-8 -4],cc);
xx1 = -8:0.1:-4; 
plot(xx1,ppval(pp1,xx1),'k-')

subplot(2,2,2)
pp2 = mkpp([-4 0],-cc);
xx2 = -4:0.1:0; 
plot(xx2,ppval(pp2,xx2),'k-')

subplot(2,1,2)
pp = mkpp([-8 -4 0 4 8],[cc;-cc;cc;-cc]);
xx = -8:0.1:8;
plot(xx,ppval(pp,xx),'k-')
hold on
line([-4 -4],ylim,'LineStyle','--')
line([0 0],ylim,'LineStyle','--')
line([4 4],ylim,'LineStyle','--')
hold off

% Copyright 2012 The MathWorks, Inc.