function [a,y,dist]=lloydmax(funfcn,b,n,tol,p1,p2,p3)
%LLOYDMAX 	Returns the the Lloyd-Max quantizer and the mean-squared 
%	  	quantization error for a symmetric distribution.
%         	[A,Y,DIST]=LLOYDMAX(FUNFCN,B,N,TOL,P1,P2,P3)     
% 	  	funfcn=The density function given 
%	  	in an m-file. It can depent on up to three 
%	  	parameters, p1,p2,p3.
% 	  	a=The vector giving the boundaries of the 
%	  	quantization regions. 
%	  	[-b,b] approximates support of the density function.
%	  	n=The number of quantization regions.
%	  	y=The quantization levels.
% 	  	p1,p2,p3=Parameters of funfcn.
%	  	tol=the relative error.

args=[];
for j=1:nargin-4
  args=[args,',p',int2str(j)];
end
args=[args,')'];
v=eval(['variance(funfcn,-b,b,tol',args]);
a(1)=-b;
d=2*b/n;
for i=2:n
  a(i)=a(i-1)+d;
end
a(n+1)=b;
dist=v;
[y,newdist]=eval(['mse_dist(funfcn,a,tol',args]);
while(newdist<0.99*dist),
  for i=2:n
    a(i)=(y(i-1)+y(i))/2;
  end
  dist=newdist;
  [y,newdist]=eval(['mse_dist(funfcn,a,tol',args]);
end
