> The major concern here being that if legendre.m is faster for a relatively small
> set of inputs, as specified in the vector X below, the gap may widen even more
> for a large matrix of inputs.
No! It is *exactly* the opposite! Chebfun (properly used) will be
*faster* than legendre() for large inputs, while legendre() will
typically be faster for small inputs, since for small inputs the
overhead of chebfun construction is too large for the faster evaluation
to be worth it.
Try this:
-----[BEGIN CODE]-----
% A few different grid sizes---try them!
%X = -1:.01:1;
%X = -1:.001:1;
%X = -1:.0001:1;
X = -1:.00001:1;
N = [0 1 2 3 4 5 6 100];
% Compute Legendre Polynomials using MATLAB's legendre.m
P_N = zeros(length(N),length(X));
tic
for n = 1:length(N)
P = legendre(N(n),X);
if n > 0
P = squeeze(P(1,:,:));
end
P_N(n,:) = P;
end
toc
% Compute Legendre polynomials with Chebfun's legpoly(), evaluating in a
% vectorized fashion.
tic
P = legpoly(N);
P_N_CHEBFUN = P(X.').';
toc
% Compute Legendre polynomials with Chebfun's legpoly(), building
% individual chebfuns for each degree.
P_N_CHEBFUN_2 = zeros(length(N), length(X));
tic
for n = 1:length(N)
P = legpoly(N(n));
P_N_CHEBFUN_2(n, :) = P(X).';
end
toc
% Plot output from MATLAB's legendre.m
fig_h1 = figure(1);
plot(X,P_N);
legend('$P_0$','$P_1$','$P_2$','$P_3$','$P_4$','$P_5$','$P_6$','$P_{100}$', 'location','southeast','interpreter','latex');
title('Legendre Polynomials (MATLAB)');
% Plot output from CHEBFUN's legpoly.m
fig_h2 = figure(2);
plot(P_N_CHEBFUN.');
legend('$P_0$','$P_1$','$P_2$','$P_3$','$P_4$','$P_5$','$P_6$','$P_{100}$','location','southeast','interpreter','latex');
title('Legendre Polynomials (CHEBFUN)');
% Plot output from CHEBFUN's legpoly.m
fig_h3 = figure(3);
plot(P_N_CHEBFUN_2.');
legend('$P_0$','$P_1$','$P_2$','$P_3$','$P_4$','$P_5$','$P_6$','$P_{100}$','location','southeast','interpreter','latex');
title('Legendre Polynomials (CHEBFUN)');
-----[END CODE]-----
With X = -1:0.01:1 selected, I get
Elapsed time is 0.003227 seconds.
Elapsed time is 0.008599 seconds.
Elapsed time is 0.017367 seconds.
So MATLAB's legendre() is faster here. With X = -1:0.00001:1 selected, I get
Elapsed time is 0.906905 seconds.
Elapsed time is 0.402918 seconds.
Elapsed time is 0.077085 seconds.
So Chebfun is faster here, and it is better to build individual chebfuns
than to do a vectorized evaluation with a quasimatrix.