2
$\begingroup$

I am new to Mathematica, so probably this question will be very basic. I have three shares (that are between 0 and 1), say $a$, $b$ and $c$. They sum up to one $(a+b+c=1)$. For every $b$ from 0 to 1, I need to find all the possible combinations of $a$ and $c$. I tried with a solve equation inside a loop, like this:

p = For[b = 0, b <= 1, b += 0.01,
  Solve[{a + b + c == 1 && 0 <= a <= 1 && 0 <= c <= 1}, {a, c}]] 

and it does not work because of inexact coefficients. I also tried with FindRoot but did not work. Do you know some way that I could use to solve this problem? Thank you!

$\endgroup$
1
  • 1
    $\begingroup$ I don't understand what you mean by all the possible combinations of a and c. There are infinitely many for each value of b (of course, except when b = 1) since you only have one (linear) equation, but three variables. $\endgroup$ Jun 8, 2018 at 21:56

1 Answer 1

2
$\begingroup$

The general problem, i.e. of finding all values of $a$ and $c$ for which $a+b+c=1$ for a certain value of $b$ admits infinite solutions, as mentioned in comments.

However, your proposed code seems to imply that you can work with a discrete resolution, i.e. that $a,b,c$ can vary by e.g. no less than $0.01$. You can use e.g. IntegerPartitions to solve this problem. If you want a resolution of $0.01=1/100$, then you can use IntegerPartitions to find all integer triplets that sum to $1/0.01=100$, then divide each triplet by $100$:

IntegerPartitions[100, {3}]/100.

(* Out: {{0.98, 0.01, 0.01}, {0.97, 0.02, 0.01}, {0.96, 0.03, 0.01}, ...
     ... {0.35, 0.33, 0.32}, {0.34, 0.34, 0.32}, {0.34, 0.33, 0.33}} *)

This approach, however, will not produce positional duplicates such as {0.01, 0.01, 0.98} and {0.01, 0.98, 0.01}, which you would have obtained by your approach based on Solve. The approach below adds them back in:

1./100 Permutations /@ (PadRight[#, 3] &) /@ IntegerPartitions[100, 3] // Flatten[#, 1] &
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.