function srf = gmmsurf(gam, kern, dat, n, sigma)

gamma = gam;
prekern = kern;
dsize = size(dat);
decsrf = [];
xv = [];
mygamma = [];

nclust = n;
rng = 20;
scale = .2;
offs = 10;

for x = 1:rng
	x
	xv(x) = (x-offs)/scale;
	for y = 1:rng
		prob = 0;
		me = [(x-offs)/scale (y-offs)/scale];
		for d= 1:dsize(1)
			mygamma(d) = kernel(me, dat(d,:));
		end
	for c = 1:nclust
		A = gamma(c,:)'*gamma(c,:);
		dis = kernel(me,me)+sum(sum(A.*prekern))-2*sum(gamma(c,:).*mygamma);
		distance(x, c) = dis;
		prob = prob+(1./(2*pi*sigma).^(dsize(2)/2))* exp((-1./(2*sigma^2))*(dis^2));
	end
	decsrf(x,y) = prob;
	end
end

%hold off; plot(cpoint(:,1), cpoint(:,2), 'x'); hold on; plot(cstop(:,1), cstop(:,2), 'red*')
hold off; plot(dat(:,1), dat(:,2), 'black+'); hold on; 
%plot(cpoint(:,1), cpoint(:,2), 'bluex');
%plot(cstop(:,1), cstop(:,2), 'redx')
%vscale = max(max(decsrf))-min(min(decsrf))
%mean(decsrf)
surf(xv, xv, (decsrf')-.000000001-max(max(decsrf))); shading('interp')
view([0 90]);
srf = decsrf';



