function ShowPoly()
% Examines the Chebychev, Legendre, and Hermite Polynomials
clc
close all
% Check out EvalPoly, PlotPoly, ScalePoly, and DerPoly...
P = MakePoly([-6 5 5 -5 1])
PlotPoly(P,-1.2,3.2)
Three_times_P = ScalePoly(3,P)
Derivative_of_P = DerPoly(P)
% Check out SubPoly and MultPoly...
P = MakePoly([10 20 30 40])
Q = MakePoly([1 2 3 4 5 6 7])
PminusQ = SubPoly(P,Q)
QminusP = SubPoly(Q,P)
PtimesQ = MultPoly(P,Q)
QtimesP = MultPoly(Q,P)

% Check out MakeChebychev...
figure
fprintf('\n\nChebychev Polynomial Coefficients...\n')
T = MakeChebychev(10);
for k=1:8
fprintf('\nk = %1d: ',k)
fprintf('%8.3f ',T(k).a)
PlotPoly(T(k),-1,1)
title(sprintf('Chebychev Polynomial: Degree = %1d',k),'Fontsize',16)
pause(.5)
end

% Check out MakeLegendre...
figure
fprintf('\n\nLegendre Polynomial Coefficients...\n')
P = MakeLegendre(10);
for k=1:8
fprintf('\nk = %1d: ',k)
fprintf('%8.3f ',P(k).a)
PlotPoly(P(k),-1,1)
title(sprintf('Legendre Polynomial: Degree = %1d',k),'Fontsize',16)
pause(.3)
end

% Check out MakeHermite...
figure
fprintf('\n\nHermite Polynomial Coefficients...\n')
H = MakeHermite(10);
for k=1:8
fprintf('\nk = %1d: ',k)
fprintf('%8.3f ',H(k).a)
PlotPoly(H(k),-3,3)
title(sprintf('Hermite Polynomial: Degree = %1d',k),'Fontsize',16)
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Required subfunctions....

function P = MakePoly(a)
% a is a length n vector.
% P is a two-field structure that encodes a polynomial p(x).
% P.a specifies the coefficients:
% p(x) = P.a(1) + P.a(2)x + a(3)x^2 + ... + P.a(n)x^(n-1)
% P.deg is the degree of the polynomial (i.e.,P.deg == n-1)
deg = length(a)-1;
P = struct('a',a,'deg',deg);

function PlotPoly(P,L,R)
% P encodes a polynomial p(x) and L<R
% Opens a figure window and displays p(x) across the interval [L,R]
% The x-axis is displayed as a red line

function yVals = EvalPoly(P,xVals)
% P encodes a polynomial p(x)
% xVals is a column n-vector
% yVals is a column n-vector with yVals(k) = p(xVals(k)), k=1:n

function R = ScalePoly(alfa,P)
% alfa is a scalar and P encodes a polynomial p(x)
% R encodes the polynimal alfa*p(x)

function R = DerPoly(P)
% P encodes a polynomial p(x)
% R encodes its derivative

function R = SubPoly(P,Q)
% P encodes the polynomial p(x)
% Q encodes the polynomial q(x)
% R encodes the polynomial r(x) = p(x)-q(x)

function R = MultPoly(P,Q)
% P encodes the polynomial p(x)
% Q encodes the polynomial q(x)
% R encodes the polynomial r(x) = p(x)*q(x)

function T = MakeChebychev(n)
% T is a length-n structure array of polynomials
% T(k) encodes the k-th Chebychev polynomial

function P = MakeLegendre(n)
% P is a length-n structure array of polynomials
% P(k) encodes the k-th Legendre polynomial

function H = MakeHermite(n)
% H is a length-n structure array of polynomials
% H(k) encodes the k-th Hermite polynomial