function [u] = burgex(x,t,u0)

% Solve the Burger's equation problem:
% u_t + u u_x = 0    u(x,0) = u0 + sin(x)   u(x,t) = u(x + 2 pi,t)

% if t=0, trivial
if t<=0.,
  u=u0+sin(x);
  return;
end

% find xbar, and reduce to the interval from 0 to 2 pi
xbar=x-u0*t;
while xbar<0., xbar=xbar+2.*pi; end
while xbar>2.*pi, xbar=xbar+2.*pi; end

% restrict xbar to from 0 to pi;
xbar2=xbar;
if xbar>pi, xbar2=2.*pi-xbar; end

% use Newton iteration to solve (x-xi)/t - sin(xi) = 0 for xi;

% initialize xi;
xi=0.;
dxi=1.e30;

for iter=1:12345,

  % compute change in xi;
  dxiprv=dxi;
  dxi=-((xbar2-xi)/t-sin(xi))/(-1./t-cos(xi));

  % check for convergence and exit if so
  if abs(dxi) >= abs(dxiprv) | dxi == 0.,
    % find u;
    u=(xbar2-xi)/t;
    if xbar ~= xbar2, u=-u; end
    u=u+u0;
    return;
  end

  % increment xi
  xi=xi+dxi;

end

% no convergence.  print an error message and stop.
error('*** No convergence in burgex')

