Updating multiple variables randomly with a constraint in a while cycle

2 views (last 30 days)
I have a pretty complex function that calculates a 1 by 6 vector (phi) from another 1 by 6 vector (x) like (phi)=function(x) I want now to fix phi and look for all the possible x that give me that phi with the costraint that sum(x)==1 and that for every element of x(i), 0<= x <= 1
I'm trying with a WHILE loop but I don't know how to randomly update the x variable preventing it to try the same x multiple times; my desired solution have 3 digits of accuracy like x=[0.0112 0.0997 0.157 0.227 ecc.] I know there could be a maximum of 2 solutions and I need to find them both eventually.
I don't necessarily need to use a WHILE loop If there is any MATLAB functionality that can solve this it would be great.
  11 Comments
Sindar
Sindar on 1 Jun 2020
In that case, the number of possible solutions is dramatically more, and significantly harder to either count or iterate over
Sindar
Sindar on 1 Jun 2020
Therefore, my suggestion is to drop the discretization, and treat it as a continuous optimization problem. Once you have an answer, truncate it to your desired accuracy and check how poor the agreement is.

Sign in to comment.

Answers (1)

Jeff Miller
Jeff Miller on 1 Jun 2020
You might be able to do what you want with nested while loops, something like this. Pretty tedious though.
x = zeros(6,1);
step = 0.001; % minimum step between adjacent x values
x1max = 1 - 5*step; % x1 cannot be bigger than this because at least step is needed for all others
x(1) = 0;
nFound = 0; % number of solutions found so far
while x(1)<=x1max && nFound<2
x(1) = x(1) + step;
x2max = 1 - x(1) - 4*step;
x(2) = 0;
while x(2)<=x2max && nFound<2
x(2) = x(2) + step;
x3max = 1 - x(1) - x(2) - 3*step;
x(3) = 0;
% add further nested while loops like the above for x(3), x(4) and x(5)
% finally, deep in the midst of all these while loops, do this
x(6) = 1 - sum(x(1:5));
phi = func(x);
% check phi here.
% If phi is a solution you want, save it and increment nFound
end
end
  1 Comment
Sindar
Sindar on 1 Jun 2020
While this technically would work, there are several issues for this particular problem:
  • the number of iterations is ridiculous (I estimated 8e12 above)
  • Gabriele eventually clarified that they mean 3 significant figures, not 3 places after the decimal
  • while possible, I think it unlikely that there is a way to check phi such that only the two desired solutions pass. Instead, I expect that this is a normal convergence problem where the closer x gets to x_solution, the closer phi gets to phi_solution. So, depending on what tolerance you set, you could either miss x_solution ("nah, this phi is 1e-16 off from the real one") or find solutions that are not distinct (one step apart)
  • since you are iterating through in a set order, it doesn't make much sense to use "while" over "for". It makes it harder to track progress, restart from where you left off, parallelize, etc..
Still, this is close to the optimal solution to the particular request given. But, it won't work. Conclusion: use a different algorithm

Sign in to comment.

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!