how to generate uniformly random varaibles with inequality constraints

1 Ansicht (letzte 30 Tage)
Hi,
I have two variables function f(c1,c2) and I would like to randomly generates c1 and c2 with the constraints that
  • Maximum >= f(c1,c2)>=C here C is a constant, which is less than the optima for f in a constraint area
  • c1>=0, c2>=0,
  • c1+c2<=100
Do you know you to realize this? For example, as in the image below, I would like take a random point of (c1,c2) in the darkest blue area.
  1 Kommentar
xueqi
xueqi am 1 Aug. 2013
I think maybe I can first generate c1 and c2 randomly with the constraints
  • Condition1: c1>=0, c2>-0
  • Condition2: c1+c2<=100
Then I check if the answers satisfies the constraints
  • Condition3: Maximum >= f(c1,c2)>=C
If it satisfies, then great. But if not, then I repeat the first step again and again until find a set of c1 and c2 satisfies condition3. So I think I need a loop with uncertain reiterations. Now my questions are
  1. If this method will violates the randomness of what I want actually
  2. How to do a loop with random reiterations

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

xueqi
xueqi am 1 Aug. 2013
Add IF CONDITION BRKEAK END in the loop will do. Set the iteration to a really big number to make sure(with a really small probability to fail) that the loop will generate at least one set of c1 and c2 satisfy the condition3.
LOL why in this thread is only myself ask and answer...

Weitere Antworten (1)

the cyclist
the cyclist am 1 Aug. 2013
Bearbeitet: the cyclist am 1 Aug. 2013
What you describe is the "rejection method", and it does not violate the randomness that you want.
Here is an example that is similar to what you want:
% The function that defines one of the criteria
f = @(x,y) x.^2 + y.^2;
% Number of points to generate
nPoints = 1000;
% Pre-allocate memory for the random data
c1 = zeros(nPoints,1);
c2 = zeros(nPoints,1);
% Initialize counter for the number of points we have
i = 0;
% Run a loop until we have the number we need
while i < nPoints
% Generate trial points
c1_try = randi([0 100]);
c2_try = randi([0 100]);
% If these points meet the criteria, store them and increment the counter
if (c1_try+c2_try < 100) && f(c1_try,c2_try) > 3000
i = i+1;
c1(i) = c1_try;
c2(i) = c2_try;
end
end
% Plot the resulting points
figure
plot(c1,c2,'.')
This is not the most efficient method. (For example, you could speed this up by generating all the random numbers upfront.) But I thought it might be better to keep it simple so that you can see what is going on.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by