# How to assign a value from the outcomes of Solve or 0?

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!

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

• Thank you very much! Commented May 3, 2020 at 13:25
• @lin my pleasure. Thank you for the accept.
– kglr
Commented May 3, 2020 at 13:26