%le(wts, s, iters)
function [output]=lyapunov(wts,s,iters)

%calculates LE for iters iterations
%Oguz Yetkin 11/24/1998
%for now,works for square matrices (because of all-connected simplification)

d0=0.000001;
d1=0;
%wts is an N by M array, outputs is a vector of size N
n=size(wts); n=n(2);
m=size(wts);m=m(1);
x=zeros(size(n));   %assuming all is square
xe=zeros(size(n));
y=x;		%just to initialize
ye=xe;
gaussnum=32;

%init x to random, xe to x+d0
x=rand(1,n);
xe=x+d0;	%adds d0 to each element!!!

dsum=0;	%separation ?
lsum=0;
le=0;
for i=1:iters
	y=niterate(wts, x, s)
	ye=niterate(wts, xe, s)
	dsum=0	
	for j=1:size(y)
		dsum=dsum+(y(i)-ye(i)).^2;   %euclidean distance
	end
	d1=sqrt(dsum)
	for j=1:n              %Readjust ye for separation d0
                ye(j)=y(j)+(d0/d1).*(ye(j)-y(j))
	end	
	lsum=lsum+log(d1/d0)
	le=lsum./i	%running average
end
	
output=le;

