function [res, t, xi, u, uxi ]  = residualtransistormol(sol,n,tf)
x = cos( pi*(0:n+1)/(n+1) );
D = gallery('chebspec',n+2);
% Problem parameters, shared with nested functions.
L = 1;
d = 0.1;
eta = 10;
K = d;
I_p = 1;
% pdex3 has 0 <= x3 <= 1.  Here we use -1 <= x <= 1.
% Change of variable means x = 2x3 - 1, hence d/dx = (1/2) d/dx3. 
em = 2*50;
en = 200;
%t = linspace(0,tf,em);
t = RefineMesh( sol.x, 3 );
j0 = find(t>=2/em);
j0 = j0(1);
em = length(t);
[u,ut] = deval( sol, t );
ux = D*[zeros(1,size(u,2));u;zeros(1,size(u,2))];
%xi = linspace(-1,1,en);
xi = RefineMesh(x,3);
en = length(xi);
res = zeros(en,em);
w = genbarywts( x, 1 );
for i=2:em,
  [uxi, uxxi] = hermiteval( [0;u(:,i);0], xi, x, 1, w, D );
  utxi = hermiteval( [0;ut(:,i);0], xi, x, 1, w, D);
  [uxxi2, uxxxi] = hermiteval( ux(:,i), xi, x, 1, w, D );
  %res = ut + d*eta*ux -d * uxx;
  res(:,i) = utxi + 2*d*eta*uxxi - 4*d*uxxxi;
end
figure
colormap([0,0,0])
sc = 0.5e-3;
mesh( t(j0:end), (xi(2:end-1)+1)/2, res(2:end-1,j0:end) ) , axis([0,tf, 0, 1, -5*sc, 5*sc]), view(70,60), set(gca,'fontsize',16),xlabel('t','fontsize',16), ylabel('x','fontsize',16),zlabel('residual','fontsize',16)

