3

I am trying to solve an equation using sympy's solve command but my output is an empty list [ ]. The only reason I think this could be happening is because there is no solution but I doubt that is the reason. Anyone know why I am not getting an answer? Thanks!

from sympy import *

class WaterModel:

    def fp_requirement(self, Ws0, Wp0, Wg0):
        greyW = 60.0
        potW = 126.0
        rainW = 17.05

        self.Ws0 = Ws0
        self.Wp0 = Wp0
        self.Wg0 = Wg0

        self.fp = var('fp')

        filt_greyW = self.fp*greyW
        dWg = self.Wg0 - greyW + (1 - self.fp)*greyW + rainW
        dWp = self.Wp0 - potW + filt_greyW 
        F = self.Ws0 + dWp + dWg
        self.fp = solve(F,self.fp)

        return self.fp 

a = WaterModel()
fp = a.fp_requirement(1500, 100, 100)
print(fp)
Zach Thomas
  • 147
  • 12

2 Answers2

3

I tried adding a few tracing statements to your function, just above the call to solve.

    print "dWg\t", dWg, type(dWg)
    print "dWp\t", dWp, type(dWp)
    print "F\t", F, type(F)
    print "self.fp\t", self.fp
    self.fp = solve(F,self.fp)

Output:

dWg 117.050000000000 - 60.0000000000000*fp <class 'sympy.core.add.Add'>
dWp -26.0000000000000 + 60.0000000000000*fp <class 'sympy.core.add.Add'>
F   1591.05000000000 <class 'sympy.core.numbers.Real'>
self.fp fp
[]

If I'm reading this correctly, your function evaluates the expressions rather than maintaining the symbolic nature of F. Thus, when you issue the solve directive, you're trying to solve a constant for the variable fp. That's why you get no solutions.


Ah, ha! There it is!

1500 + 117 - 26 - 60*fp + 60*fp => 1591

With fp out of the equation, there are no solutions.

Prune
  • 76,765
  • 14
  • 60
  • 81
  • Awesome! I am curious as to why my equation F is a constant and not a function of fp as dWg and dWp are. – Zach Thomas Sep 07 '16 at 22:15
  • I am not a **sympy** user ... however, my guess is that the "+" operator on sympy objects is *evaluate-and-add*, not the string concatenation you appear to expect. – Prune Sep 07 '16 at 22:17
  • The calculations are all done symbolically - note how `dWg` and `dWp` have an `fp` term in them. However, note how those `fp` terms are each other's negatives. `fp` cancels out! – user2357112 Sep 07 '16 at 22:20
  • Ah, ha! There it is! **1500 + 117 - 26 - 60*fp + 60*fp == 1591**. With **fp** out of the equation, there are no solutions. – Prune Sep 07 '16 at 22:30
  • Remember to accept your favourite answer (that of @user2357112), so SO can properly retire the question. I just noticed it above mine on this screen. – Prune Sep 07 '16 at 22:32
  • Huh?! Didn't @user2357112 find the critical problem -- or did you do that on your own? I was the 3rd one to see this ... someone else should get the "best answer" credit, no? – Prune Sep 07 '16 at 22:34
2

self.fp cancels out of your computation. The value of F is the same no matter what self.fp is, so solve can't help you.

Are you sure you have your equations right?

user2357112
  • 260,549
  • 28
  • 431
  • 505
  • So I am just trying to solve the equation 0 = Ws0 + dWp + dWg but I am trying to use sympy's 'solve' function. I am certain the equations I am using are correct. Maybe I am using 'self' incorrectly? Also, why would F be the same no matter what fp is? dWg and dWp is a function of fp – Zach Thomas Sep 07 '16 at 22:23
  • @ZachThomas: The class is pointless, but there's nothing technically wrong with your use of `self`. There probably is something wrong with your math if you were expecting `F` to have any dependence on `fp`, though. Try working it out by hand and see what happens to `fp`. – user2357112 Sep 07 '16 at 22:24
  • Thanks! And my class has other definition in it but I did not include them for simplicity. – Zach Thomas Sep 07 '16 at 22:26
  • Yeah it says no solution exists according to my work by hand. Thanks for the help! – Zach Thomas Sep 07 '16 at 22:30