Reputation:
I am looking to create a random distribution of gaussian curve shapes on a large mesh. I basically want to take this function :
Z = 0.3*exp(-5*(x.^2+y.^2))-0.1;
Z(Z<0)=0;
and be able to choose its location (in x & y coords), and have multiple plots.
So at the moment, I have this:
But I would like to have this generated:
Is there a reasonably simple way to do this? I have tried to play around with the code but I am afraid I'm not a mathematics, nor a MATLAB expert.
Any help would be much appreciated.
Upvotes: 3
Views: 188
Reputation: 35525
Look at this!
the way this works:
code:
clear
n_gaussians=15;
gaussians=0;
sigma=1; % std
mindist=sigma; % if distance is smaller than this gaussians "collide"
[x,y]= meshgrid(-5:0.1:5,-5:0.1:5);
used=[];
Z=zeros(size(x));
while gaussians<n_gaussians
xm=(rand(1)-0.5)*10;
ym=(rand(1)-0.5)*10;
notvalid=0;
for ii=1:size(used,2)
% if we are too close to any point.
if norm([xm-used(1,ii),ym-used(2,ii)])<mindist
notvalid=1; % do not add this gauusian
end
end
if notvalid
continue
end
used(:,end+1)=[xm;ym];
Zaux = 0.3/sigma*exp(-5*((x-xm).^2+(y-ym).^2)/sigma.^2)-0.1;
Zaux(Zaux<0)=0;
Z=Z+Zaux;
gaussians=gaussians+1;
end
surf(x,y,Z);
axis equal
Upvotes: 4