1
$\begingroup$

Here is my problem:

I want to achieve a loop and assign the outcomes of Solve to variables for future use. But the equations in Solve may have no solutions. Under this circumstance, I want to assign 0 to my variable. But I don't know how to achieve it.

For example:

  solution = Solve[15 - 0.005 Qi^2 - (Qi Qj)/500 == 0 &&  10 - (3 Qi Qj)/2000 + 0.00025 Qj^2 == 0, {Qi, Qj}, Reals]
    x1 = solution[[1, 1, 2]]
    x2 = solution[[1, 2, 2]]

The outcomes of Solve under this pair of parameters are empty. So I want to assign 0 to x1,x2 but I don't know why.

Thanks!

$\endgroup$

1 Answer 1

0
$\begingroup$
solution =  Solve[15 - 0.005 Qi^2 - (Qi Qj)/500 == 0 && 
    10 - (3 Qi Qj)/2000 + 0.00025 Qj^2 == 0, {Qi, Qj}, Reals] // Quiet
  {}
{x1, x2} = If[solution == {}, {0, 0}, {Qi, Qj} /. solution]
{0, 0}
x1
0

Alternatively,

solution2 =  Solve[15 - 0.005 Qi^2 - (Qi Qj)/500 == 0 && 
     10 - (3 Qi Qj)/2000 + 0.00025 Qj^2 == 0, {Qi, Qj},  Reals] /. 
  {} -> Thread[{Qi, Qj} -> 0] // Quiet
  {Qi -> 0, Qj -> 0}
{x1, x2} = {Qi, Qj} /. solution2
 {0, 0}
x1
 0
$\endgroup$
2
  • $\begingroup$ Thank you very much! $\endgroup$
    – cclinoom
    Commented May 3, 2020 at 13:25
  • $\begingroup$ @lin my pleasure. Thank you for the accept. $\endgroup$
    – kglr
    Commented May 3, 2020 at 13:26

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.