Reputation: 2334
I am very confused with simple MATLAB random number generation problem. I know it looks silly and basic level some how but I have tried google before but could not find any useful solution.
I have bounds like this
Lb = [150,100,150]; %in MW
Ub = [600,400,200]; %in MW
Pd = 850; %in MW
I want to create a random guess which sum will be equal to Pd and which should be within limits of Lower and Upper Bounds.
I am able to generate row vector within limits like this
`u0=Lb+(Ub-Lb).*rand(1,d);`
and I know that to generate random number which sum would be equal to Pd can be obtained like this
u0=rand(1,d);
u0=u0/sum(u0);
u0=u0*Pd;
How to combine both effect in single random number generation. thanks
Upvotes: 2
Views: 158
Reputation: 3476
This is not as easy as it looks, as the bound will put some extra constraints on the problem :
1) Your current solution :
Your corrected code (As stated by @il_raffa in the comments) would look like :
Lb = [150,100,150]; %in MW
Ub = [600,400,200]; %in MW
Pd = 850; %in MW
d=3;
u0=Lb+(Ub-Lb).*rand(1,d) % Output before normalization
u0=(u0/sum(u0))*Pd % Output after normalization
Outputs :
u0 =
516.6257 371.7376 156.3493 % Before Normalization
u0 =
420.3374 302.4535 127.2091 % After Normalization
As you can see, the sum of the elements of u0
was quite bigger than Pd
, and the third value in u0
was pretty close from the lower bound. Thus after Normalization the third value in u0
is lower than the corresponding lower bound.
A similar problem will happen with the solution @CarlWitthoft presents in the comments. It is possible that after generating n-1
random numbers, the n-th number making the sum be equal to Pd
would not lie within the bound range.
2) Conclusion :
The only way to do it in my opinion is to iterate until you find a solution, so it would be something along the lines of :
Lb = [150,100,150]; %in MW
Ub = [600,400,200]; %in MW
Pd = 850; %in MW
d=3;
u0=[Inf Inf Inf];
while(~all((u0>Lb).*(u0<Ub)))
u0=Lb+(Ub-Lb).*rand(1,d);
u0=(u0/sum(u0))*Pd;
end
Output :
u0 =
533.9384 134.6124 181.4492
Upvotes: 2