MATLAB: Find n-number of solutions that satisfy x+y+z=1

equation

Hello,
How could one generate n random points (x,y,z) that satisfy the equation x+y+z=1 ?
Thanks

Best Answer

With only one exception that I can think of off the top of my head, the sample & then normalize solutions that people usually tell you to use are not able to produce a uniform sampling. So, while the result may be "random" it won't uniformly sample the possible sets of numbers that satisfy the constraint. In order to get true uniformity, you need to use a tool designed explicitly to solve that problem.
n = 1e4;
x = rand(n,3);
x = x./sum(x,2);
Now, lets plot the points. Are they truly uniform over the region of interest?
plot3(x(:,1),x(:,2),x(:,3),'.')
view(-45,0)
I've rotated the plot so the point set is in the image plane. Does it look random? NO. In fact, the points are NOT sampled uniformly from that region.
The corners of the triangle are much more sparsely sampled than the rest of the triangular region.
Instead, see how randfixedsum does, on the same problem:
Y = randfixedsum(3,n,1,0,1)';
plot3(Y(:,1),Y(:,2),Y(:,3),'.')
view(-45,0)
As you should see, this point set is quite uniformly sampled over the domain of interest.
Alternatively,
Y = randFixedLinearCombination(n,1,[1 1 1],[0 0 0],[1 1 1]);
plot3(Y(:,1),Y(:,2),Y(:,3),'.')
view(-45,0)
As you can see, both tools are able to produce a nicely uniform sampling.
So use randfixedsum , or my randFixedLinearCombination , which is able to solve a slightly more complex problem, but in a very high number of dimensions, won't be as efficient as the one by Roger. 3 dimensions is not high though, so take your pick. They will both be efficient here, and both nicely uniform.