function mlclegen(gmin,gmax,gnum,TSkip,TFull,StepLen,opts,p,xstart,FileName)
%compute Lyapunov exponents of the generic variational equation for the MLC circuit, 
%needed for the Pecora Carroll approach to study the stability of the synchronous motion

fileID = fopen(FileName,'w');
for g=0:gnum-1
  g=gmin+(gmax-gmin)*g/(gnum-1);
  p.gamma=g;

  xs=xstart;
  xs(4:12)=eye(3);
  [t,x] = ode45(@mlcGenVERHS,[0 TSkip],xs,opts,p);
  xs=x(end,:);
  xs(4:12)=eye(3);
  lambdaSum=zeros(3,1);
  T=0; 
  while T<TFull
    [t,x] = ode45(@mlcGenVERHS,[0 StepLen],xs,opts,p);
    T=T+StepLen;
    xs=x(end,:);
    J=reshape(xs(4:12),3,3);
    [Q,R] = qr(J);
    lambdaSum=lambdaSum+log(abs(diag(R)));
    lambda=lambdaSum/(T);
    xs(4:12)=Q;
  end
  fprintf(fileID,'gamma: %7.5f, Step: %7.5f, TSkip: %7.5f, TFull: %7.5f, MaxLambda: %7.5f\n',p.gamma,StepLen,TSkip,TFull,max(lambda));
  disp(sprintf('gamma: %7.5f, Step: %7.5f, TSkip: %7.5f, TFull: %7.5f, MaxLambda: %7.5f',p.gamma,StepLen,TSkip,TFull,max(lambda)))
end
fclose(fileID);
