Suppose a dynamic list of 5 that if one changes, all the others would change correspondingly to keep the sum 1. It comes from a practical problem that 5 probabilities of a total of 1, if one of them changes (by Slider), all the others will also change in an opposite direction, and keep the sum at 1.
And here is my attempted code:
v = {0.2, 0.2, 0.2, 0.2, 0.2};
v[[1]] = Dynamic[val];
Dynamic[val]
Slider[Dynamic[val]]
sum = 1;
Dynamic[v]
f := (#/(sum - #))*(sum - val) &
Slider[Dynamic[v[[1]], f /@ v], {0, 1}]
Slider[Dynamic[v[[2]], f /@ v], {0, 1}]
Slider[Dynamic[v[[3]], f /@ v], {0, 1}]
Slider[Dynamic[v[[4]], f /@ v], {0, 1}]
Slider[Dynamic[v[[5]], f /@ v], {0, 1}]
My intention was to create a pure function f that would allow the list to be updated following the rule of sum is 1, as the elements of the list v were changed. But this doesn't work in the way I thought it should.
Thanks in advance!
If one slider is moved the other four change proportionally with all summing to 1.
v = {0.2, 0.2, 0.2, 0.2, 0.2};
f = Function[{z, k},
{a, b, c, d, e} = v;
m = n = o = p = q = 1;
Switch[k,
1, a = z; m = 0,
2, b = z; n = 0,
3, c = z; o = 0,
4, d = z; p = 0,
5, e = z; q = 0];
sol = Solve[a x^m + b x^n + c x^o + d x^p + e x^q == 1, x];
xsol = First[x /. sol];
v = {a xsol^m, b xsol^n, c xsol^o, d xsol^p, e xsol^q}];
{Dynamic@v, Row[{"Total = ", Dynamic[Total[v]]}]}
Slider[Dynamic[v[[1]], f[#, 1] &]]
Slider[Dynamic[v[[2]], f[#, 2] &]]
Slider[Dynamic[v[[3]], f[#, 3] &]]
Slider[Dynamic[v[[4]], f[#, 4] &]]
Slider[Dynamic[v[[5]], f[#, 5] &]]