% CGL-Model with backward Euler periodic b.c.

xmax=2*pi*20
% backward Euler on diffusion forward Euler on nonlinear term
alpha=1
R=1

clf
fig1=figure(1);

c1=0
c3=1.4
nx=800
dx=xmax/(nx+1)
tmax=200
dt=0.1

ntplot=1;
nt=0

x=[1:nx]*dx;
 

  A=1
  nwave=10
  q=nwave/10
  y=A*exp(i*q*x);
  t=0
 

%eps=2
%y=.4*sin(.8*x+eps*cos(x/(2*nwave)));
%amp=.1
%y=amp*randn(1,nx);
 
Y=y';

e=ones(nx,1);

D0=spdiags([e],0,nx,nx);
D2=1/(dx*dx)*(spdiags([e -2*e e],-1:1,nx,nx)+spdiags([e],nx-1,nx,nx)+spdiags([e],-nx+1,nx,nx));
Mt=1/dt*spdiags([e],0,nx,nx);

Dl=Mt-(1+i*c1)*alpha*D2-alpha*R*D0;
Dr=Mt+(1+i*c1)*(1-alpha)*D2+(1-alpha)*R*D0;

% transponiere mit ': yt=y'
delete(fig1)
fig1=figure(1);
lin1=line(x,real(y),'EraseMode','background');
lin2=line(x,imag(y),'EraseMode','background');
lin3=line(x,abs(y),'EraseMode','background');
axis([0 xmax -1.5 1.5]);
drawnow;


while t < tmax
Yc=(conj(Y).*Y.*Y);
rhs=Dr*Y-(1-i*c3)*Yc;
Y=Dl\rhs;
y=Y';

if rem(nt,ntplot) == 0
delete(lin1);
delete(lin2);
delete(lin3);
lin1=line(x,real(y),'EraseMode','background');
lin2=line(x,imag(y),'EraseMode','background');
lin3=line(x,abs(y),'EraseMode','background');
drawnow;
end

nt=nt+1;
t=t+dt;

end
