function [ni,x]=corr1(ni,x,res,errnorm,tol)
disp('corr')
h=x(2:ni+1)-x(1:ni);
%
%computing new mesh size
%
if errnorm=='en';
hnew=tol*ones(size(h))./(res+eps*ones(size(res)))/sqrt(ni);
%
elseif errnorm=='l2'
hnew=sqrt(tol*ones(size(h))./(res+eps*ones(size(res)))/sqrt(ni));
%
elseif errnorm=='ma'
hnew=sqrt(tol*ones(size(h))./(res+eps*ones(size(res))));
end
%
%computing new mesh points
%
hnewmin=min(hnew);
if hnew(ni)==hnewmin;
  target=x(ni+1);
else
  if hnew(1)==hnewmin;
    target=x(1);
  else
     i=1;
     while hnew(i)>hnewmin;
     i=i+1;
     end
     target=x(i)+h(i)/2;
  end
end
%
front=x(1);
xnew=front;
interval=1;
%
while front<target-eps;
%
   if front+hnew(interval)<x(interval+1);
     front=min(front+hnew(interval),target);
     xnew=[xnew,front];
   else
     if interval==ni;
       front=x(interval+1);
       xnew=[xnew,front];
     else
         if x(interval+1)-front>h(interval+1);
           front=min(x(interval+1),target);
           xnew=[xnew,front];
         else
           hmin=min(h(interval),h(interval+1));
           front=min(min(front+hmin,x(interval+2)),target);
           xnew=[xnew,front];
         end
         interval=interval+1;
     end
   end
%
end       %of outer while
%
leftmesh=xnew
%
front=x(ni+1);
xnew=front;
interval=ni;
while front>target+eps;
%
   if front-hnew(interval)>x(interval);
     front=max(front-hnew(interval),target);
     xnew=[front,xnew];
   else
     if interval==1;
       front=x(1);
       xnew=[front,xnew];
     else
         if front-x(interval)>h(interval-1);
           front=max(x(interval),target);
           xnew=[front,xnew];
         else
           hmin=min(h(interval),h(interval-1));
           front=max(max(front-hmin,x(interval-1)),target);
           xnew=[front,xnew];
         end
         interval=interval-1;
     end
   end
%
end       %of outer while
%
xnew
x=[leftmesh,xnew(2:length(xnew))];
ni=length(x)-1;


