% LLE
g = 1;
l = 1;
m = 1;

a = 0.1;
v = 20;

System = @(t,x) Kapitza(t,x,a,v,g,l)';

h = 1e-6;
tf = 20;
t = (0:h:tf);

W0 = [pi , 0];

d0 = 1e-5;

DeltaW0 = d0.*W0;
W0per = W0 + DeltaW0;

ignore = 100;

[~,N] = size(t);
Le = zeros(N,1);

lsum = 0;

for i = 1:N
    
    [~,W1] = ode45(System,[0,h],W0);
    W1 = W1(end,:);
    [~,W1per] = ode45(System,[0,h],W0per);
    W1per = W1per(end,:);
    
    DeltaW1 = W1per - W1;
    d1 = sqrt(DeltaW1*DeltaW1');
    
    DeltaW1 = (d0/d1).*DeltaW1;
    
    W0 = W1;
    DeltaW0 = DeltaW1;
    W0per = W0 + DeltaW0;
    
    if i > ignore
    lsum = lsum + log(d1/d0);
    Le(i) = lsum/(i-ignore + 1);
    Le(i) = Le(i)/h;
    
    fprintf('i = %g    lsum = %g    Le = %f   \n',i,lsum,Le(i))   
    
    end
    
end

save('LLE_Duffing.mat')
