6 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.

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

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

Opportunities for recent engineering grads.

Apply Today
## 11 Comments

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/533798-updating-multiple-variables-randomly-with-a-constraint-in-a-while-cycle#comment_864493

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/533798-updating-multiple-variables-randomly-with-a-constraint-in-a-while-cycle#comment_864493

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/533798-updating-multiple-variables-randomly-with-a-constraint-in-a-while-cycle#comment_866043

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/533798-updating-multiple-variables-randomly-with-a-constraint-in-a-while-cycle#comment_866043

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/533798-updating-multiple-variables-randomly-with-a-constraint-in-a-while-cycle#comment_870003

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/533798-updating-multiple-variables-randomly-with-a-constraint-in-a-while-cycle#comment_870003

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/533798-updating-multiple-variables-randomly-with-a-constraint-in-a-while-cycle#comment_870239

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/533798-updating-multiple-variables-randomly-with-a-constraint-in-a-while-cycle#comment_870239

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/533798-updating-multiple-variables-randomly-with-a-constraint-in-a-while-cycle#comment_870967

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/533798-updating-multiple-variables-randomly-with-a-constraint-in-a-while-cycle#comment_870967

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/533798-updating-multiple-variables-randomly-with-a-constraint-in-a-while-cycle#comment_871011

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/533798-updating-multiple-variables-randomly-with-a-constraint-in-a-while-cycle#comment_871011

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/533798-updating-multiple-variables-randomly-with-a-constraint-in-a-while-cycle#comment_874943

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/533798-updating-multiple-variables-randomly-with-a-constraint-in-a-while-cycle#comment_874943

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/533798-updating-multiple-variables-randomly-with-a-constraint-in-a-while-cycle#comment_875843

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/533798-updating-multiple-variables-randomly-with-a-constraint-in-a-while-cycle#comment_875843

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/533798-updating-multiple-variables-randomly-with-a-constraint-in-a-while-cycle#comment_877063

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/533798-updating-multiple-variables-randomly-with-a-constraint-in-a-while-cycle#comment_877063

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/533798-updating-multiple-variables-randomly-with-a-constraint-in-a-while-cycle#comment_879019

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/533798-updating-multiple-variables-randomly-with-a-constraint-in-a-while-cycle#comment_879019

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/533798-updating-multiple-variables-randomly-with-a-constraint-in-a-while-cycle#comment_879051

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/533798-updating-multiple-variables-randomly-with-a-constraint-in-a-while-cycle#comment_879051

Sign in to comment.