function y=energy(x)

%This computes the renormalized energy of a function (if
%your function is level m, then x should be a 3^(m-1) vector; 
%the program calculates the level on its own, assuming it has length that is a 
%power of 3, but usually I only use it for three-vectors).

w=round(log3(max(size(x))))-1;


y=0;
%{
for i=1:1:max(size(x))-1
    for j=i+1:1:max(size(x))
    y=y+(x(i)-x(j))^2;
    end
end
%}

for n=1:1:max(size(x))/3
    y=y+(x(3*n-2)-x(3*n-1))^2+(x(3*n-2)-x(3*n))^2+(x(3*n-1)-x(3*n))^2;
end
y=(5/3)^w*y;
