% showcspsoln(x,d,lambda) plots the degree d polynomial solution
% function x(l) = (x1(l),x2(l),...,xn(l)) forall l in lambda. This
% function requires n >= 2 and plots as many of the xi(lambda) as
% possible. The showlambda input argument is optional and defaults
% to 0 (false). If showlambda is true (i.e. any value other than 0),
% then a 3d plot of lambda vs. x1(lambda) vs. x2(lambda) is made. If
% showlambda is false, then a 2d plot x1 vs. x2 is made for n=2 and
% a 3d plot x1 vs. x2 vs. x3 is made for n >= 3.

function showcspsoln(x,d,lambda,showlambda)

nx = length(x);
if (rem(nx,d+1) ~= 0)
  error('length of x must be divisible by d+1');
end
n = nx/(d+1);
if (n < 2) error('n must be at least 2'); end
if (nargin < 4) showlambda = 0; end

nlambda = length(lambda);
V = zeros(n,nlambda);

for j=1:nlambda
  V(:,j) = evalvecpoly(x,d,lambda(j));  
end

zlabelstr = '';
if (showlambda)
  graphtitle = 'lambda   vs.   x(lambda) = (x1(lambda),x2(lambda))';
  xlabelstr = 'lambda';
  ylabelstr = 'x1(lambda)';
  zlabelstr = 'x2(lambda)';
  plot3(lambda,V(1,:),V(2,:));
elseif (n == 2)
  graphtitle = 'eigenvectors x = (x1 x2)';
  xlabelstr = 'x1';
  ylabelstr = 'x2';
  plot(V(1,:),V(2,:));
else
  graphtitle = 'eigenvectors x = (x1 x2 x3)';
  xlabelstr = 'x1';
  ylabelstr = 'x2';
  zlabelstr = 'x3';
  plot3(V(1,:),V(2,:),V(3,:));
end

title(graphtitle);
xlabel(xlabelstr);
ylabel(ylabelstr);
zlabel(zlabelstr);
