Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm trying to solve a system of equations:

Solve[ A1 D1 + E1 H1 == 0 && A2 D1 + A1 D2 + E2 H1 + E1 H2 == 0 &&
       C1 F1 - E1 G1 == 0 && C2 F2 - E2 G2 == 0 && A1 - B1 + C1 == 0 &&
        A2 - B2 + C2 == 0 &&  A3 - B3 + C3 == 0,      
       {A1,A2,A3,B1,B2,B3,C1,C2,C3,D1,D2,E1,E2,F1,F2,G1,G2,H1,H2}] 

Since we have more variables than equations, we have more than 1 solution satisfying the above equations but I don't want Mathematica to give me all possible solutions -- I would like just only one.

1. How do I get Mathematica to give me just one possible solution?

One way I thought about doing this is to plug in random numbers such as A2 = 1/2, B2 = 1, C2 = 1/2, etc. so that all other variables are determined. But this isn't a very effective strategy in case the numbers I plug in do not satisfy all of the above equations.

(One reason why I don't want all possible solutions to a system of equations is because suppose I am working with 30 equations and 80 variables. Then this is crashing Mathematica.)

share|improve this question
add comment

2 Answers 2

up vote 5 down vote accepted

You can use FindInstance :

FindInstance[A1 D1 + E1 H1 == 0 && A2 D1 + A1 D2 + E2 H1 + E1 H2 == 0 && 
  C1 F1 - E1 G1 == 0 && C2 F2 - E2 G2 == 0 && A1 - B1 + C1 == 0 && 
  A2 - B2 + C2 == 0 && A3 - B3 + C3 == 0, 
  {A1, A2, A3, B1, B2, B3, C1,C2, C3, D1, D2, E1, E2, F1, F2, G1, G2, H1, H2}]

(* {{A1 -> 0, A2 -> 0, A3 -> 0, B1 -> 0, B2 -> 0, B3 -> 0, C1 -> 0, 
     C2 -> 0, C3 -> 0, D1 -> 0, D2 -> 0, E1 -> 0, E2 -> 0, F1 -> 0, 
     F2 -> 0, G1 -> 0, G2 -> 0, H1 -> 0, H2 -> 0}} *)
share|improve this answer
    
Wow, thank you! It worked!! =) –  math-visitor Jul 2 '12 at 9:16
1  
@math-visitor Have a look at Reduce, it might suit your needs better. –  b.gatessucks Jul 2 '12 at 9:18
    
Thank you! I will definitely try that! –  math-visitor Jul 2 '12 at 9:20
1  
@math-visitor My answer make use of Solve and can help you to choose any solution you want. –  Artes Jul 2 '12 at 9:43
add comment

FindInstance

FindInstance[eqns,vars] gives you only a trivial solution (A1 == A2 == ...== H2 == 0), which is not what one reallly wants. FindInstance[eqns, vars, n] helps in finding n solutions, e.g. for n == 2 yields two non-trivial solutions :

FindInstance[ A1 D1 + E1 H1 == 0 && A2 D1 + A1 D2 + E2 H1 + E1 H2 == 0 && 
              C1 F1 - E1 G1 == 0 && C2 F2 - E2 G2 == 0 && A1 - B1 + C1 == 0 &&
              A2 - B2 + C2 == 0 && A3 - B3 + C3 == 0,
             {A1, A2, A3, B1, B2,  B3, C1, C2, C3, D1, D2, E1, E2, F1, F2, G1, G2, H1, H2}, 2]

Of course there are infinitely many such instances of solutions, so they aren't too interesting as well.

What one really would like is a symbolic solution. Thus one should make use of Solve or Reduce.

Solve

Working with Solve you can find what and how many symbolic solutions there are adding this option MaxExtraConditions -> All :

sols = Solve[ A1 D1 + E1 H1 == 0 && A2 D1 + A1 D2 + E2 H1 + E1 H2 == 0 && 
              C1 F1 - E1 G1 == 0 && C2 F2 - E2 G2 == 0 && A1 - B1 + C1 == 0 && 
               A2 - B2 + C2 == 0 && A3 - B3 + C3 == 0, 
             {A1, A2, A3, B1, B2, B3, C1, C2, C3, D1, D2, E1, E2, F1, F2, G1, G2, H1, H2}, 
             MaxExtraConditions -> All] // Quiet; 

so you can check how many solutions there are :

Length @ sols
27

and select n-k solutions, for 1 <= k < n <= 27 : sols[[ k;;n ]], e.g. the first one

sols[[1]]
 {B1 -> ConditionalExpression[A1 + C1, E1 != 0 && E2 != 0 && A1 D1 != 0], 
  B2 -> ConditionalExpression[A2 + C2, E1 != 0 && E2 != 0 && A1 D1 != 0], 
  C3 -> ConditionalExpression[-A3 + B3, E1 != 0 && E2 != 0 && A1 D1 != 0], 
  G1 -> ConditionalExpression[(C1 F1)/E1, E1 != 0 && E2 != 0 && A1 D1 != 0], 
  G2 -> ConditionalExpression[(C2 F2)/E2, E1 != 0 && E2 != 0 && A1 D1 != 0], 
  H1 -> ConditionalExpression[-((A1 D1)/E1), E1 != 0 && E2 != 0 && A1 D1 != 0], 
  H2 -> ConditionalExpression[-((A2 D1 + A1 D2 - (A1 D1 E2)/E1)/E1),
                               E1 != 0 && E2 != 0 && A1 D1 != 0]}

this means e.g. that B1 == A1 + C1 under conditions E1 != 0 && E2 != 0 && A1 D1 != 0.

One can observe that if we omit MaxExtraConditions or we add MaxExtraConditions -> Automatic then solutions will not be represented in terms of ConditionalExpressions and therefore some troubles can apear potentially.

Sometimes it will be handy to specify only a few variables. Then we can use also MaxExtraConditions in Solve, and specifying e.g. {A1, A2, A3, B1, B2, B3} we get only one symbolic solution :

Solve[ A1 D1 + E1 H1 == 0 && A2 D1 + A1 D2 + E2 H1 + E1 H2 == 0 && C1 F1 - E1 G1 ==0 &&
       C2 F2 - E2 G2 == 0 && A1 - B1 + C1 == 0 && A2 - B2 + C2 == 0 && A3 - B3 + C3 == 0, 
      {A1, A2, A3, B1, B2, B3}, MaxExtraConditions -> Automatic] // Quiet
{{A1 -> ConditionalExpression[-((E1 H1)/D1), F2 == (E2 G2)/C2 && F1 == (E1 G1)/C1], 
  A2 -> ConditionalExpression[((D2 E1 H1)/D1 - E2 H1 - E1 H2)/D1, 
                               F2 == (E2 G2)/C2 && F1 == (E1 G1)/C1], 
  B1 -> ConditionalExpression[C1 - (E1 H1)/D1, F2 == (E2 G2)/C2 && F1 == (E1 G1)/C1], 
  B2 -> ConditionalExpression[C2 + ((D2 E1 H1)/D1 - E2 H1 - E1 H2)/D1, 
                               F2 == (E2 G2)/C2 && F1 == (E1 G1)/C1], 
  B3 -> ConditionalExpression[A3 + C3,  F2 == (E2 G2)/C2 && F1 == (E1 G1)/C1]}}

Reduce

Reduce finds all solutions

r = Reduce[ A1 D1 + E1 H1 == 0 && A2 D1 + A1 D2 + E2 H1 + E1 H2 == 0 && 
            C1 F1 - E1 G1 == 0 && C2 F2 - E2 G2 == 0 && A1 - B1 + C1 == 0 && 
            A2 - B2 + C2 == 0 && A3 - B3 + C3 == 0, 
            {A1, A2, A3, B1, B2, B3, C1, C2, C3, D1, D2, E1, E2, F1, F2, G1, G2, H1, H2}];

being implicitly ConditionalExpression's. To select only one solution we just evaluate r[[n]] for 1<= n <= 25.

Warning

Comparing with sols, found by Solve the number of solutions may be slightly different because certain ConditionalExpression's repeat some identical solutions under different conditions :

Length @ r
25    
share|improve this answer
    
Wow.... this definitely helps. Thank you Artes! –  math-visitor Jul 2 '12 at 11:27
1  
@math-visitor I'm glad I could help. –  Artes Jul 2 '12 at 11:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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