dvd
dvd

Reputation: 3

How to explicit a term from an equation in sympy

I need to explicit the variable K, in function of P and T, from this equation. formula here I've done all steps up until now but this has got me stuck. I've tried to use solve from sympy library but it doesn't work.

import sympy as sp
from sympy import symbols, Eq, solve
#define symbols
T, P, K = symbols('T P K')
eq = sp.Eq(1.08866210790363*K*(1 - 0.5*K)**0.5/(0.666666666666667 - K)**1.5, 1.99036339653399e+441*P*sp.exp((0.724859000422363*P + 461.638532977748*P/T - 101419.64390802)/T)/T**344.113039591901)

solv=sp.solve(eq, K)

Sorry in advance if i omitted something, i'm new to python.

Upvotes: 0

Views: 245

Answers (2)

Mechanic Pig
Mechanic Pig

Reputation: 7736

I'm not familiar with sympy enough, so here's just to point out the problem: the value of 1.99036339653399e+441 is so large that double precision floating-point numbers cannot represent it:

>>> 1.99036339653399e+441
inf

We replace it with Python's decimal:

>>> from decimal import Decimal
>>> Decimal('1.99036339653399e+441')
Decimal('1.99036339653399E+441')

In this way, the solve function can run, but due to the complexity of the equation, the program needs to consume a lot of memory (9GiB+!) and time, and it may not even be able to solve successfully.

Upvotes: 0

Oscar Benjamin
Oscar Benjamin

Reputation: 14470

If we simplify your equation slightly we can divide the constant from the lhs over to the right and then just make the expression on the right be a symbol z since it doesn't depend on K. We can also replace floats with rationals which is particular important if there are floats in the exponents. That looks like this:

In [44]: eq = S('Eq(1.08866210790363*K*(1 - 0.5*K)**0.5/(0.666666666666667 - K)**1.5, 1.9903633965339
    ...: 9e+441*P*exp((0.724859000422363*P + 461.638532977748*P/T - 101419.64390802)/T)/T**344.113039
    ...: 591901)')

In [45]: eq
Out[45]: 
                                                                                                   
                                                                                                   
                                                                                                   
                                                                                                   
                                          -1.5            0.5                            -344.11303
1.08866210790363⋅K⋅(0.666666666666667 - K)    ⋅(1 - 0.5⋅K)    = 1.99036339653399e+441⋅P⋅T          

                               461.638532977748⋅P                  
         0.724859000422363⋅P + ────────────────── - 101419.64390802
                                       T                           
         ──────────────────────────────────────────────────────────
9591901                              T                             
       ⋅ℯ                                                          

In [46]: eq = Eq(Mul(*nsimplify(eq.lhs).args[1:]), z)

In [47]: eq
Out[47]: 
      _______    
     ╱     K     
K⋅  ╱  1 - ─     
  ╲╱       2     
───────────── = z
          3/2    
 (2/3 - K)       

Now we can solve this easily with check=False and get three different solutions:

In [48]: sol = solve(eq, K, check=False)

In [49]: len(sol)
Out[49]: 3

Here's the first solution:

In [53]: sol[0]
Out[53]: 
                                                                                                                             ________________________________________________________________________________________________    
                                                                                                                            ╱                                  _________________________________________________________         
                                                                                                                           ╱                                  ╱                     3                                 2          
                                                                                                                          ╱                                  ╱      ⎛       2      ⎞    ⎛         2           2      ⎞           
                                                                                                                         ╱                                  ╱       ⎜   24⋅z       ⎟    ⎜    432⋅z       144⋅z       ⎟           
                                                                                                                        ╱                                  ╱    - 4⋅⎜- ──────── + 4⎟  + ⎜- ────────── + ──────── - 16⎟           
                                                         2                                                             ╱               2          2       ╱         ⎜     2        ⎟    ⎜      2           2         ⎟           
                                                     24⋅z                                                             ╱           216⋅z       72⋅z      ╲╱          ⎝  6⋅z  - 3    ⎠    ⎝  54⋅z  - 27   6⋅z  - 3     ⎠           
                                                  - ──────── + 4                                                     ╱        - ────────── + ──────── + ──────────────────────────────────────────────────────────────── - 8     
                                                       2                                                          3 ╱               2           2                                      2                                         
                                                    6⋅z  - 3                                                      ╲╱            54⋅z  - 27   6⋅z  - 3                                                                           2
- ───────────────────────────────────────────────────────────────────────────────────────────────────────────── - ─────────────────────────────────────────────────────────────────────────────────────────────────────────── + ─
               ________________________________________________________________________________________________                                                        3                                                        3
              ╱                                  _________________________________________________________                                                                                                                       
             ╱                                  ╱                     3                                 2                                                                                                                        
            ╱                                  ╱      ⎛       2      ⎞    ⎛         2           2      ⎞                                                                                                                         
           ╱                                  ╱       ⎜   24⋅z       ⎟    ⎜    432⋅z       144⋅z       ⎟                                                                                                                         
          ╱                                  ╱    - 4⋅⎜- ──────── + 4⎟  + ⎜- ────────── + ──────── - 16⎟                                                                                                                         
         ╱               2          2       ╱         ⎜     2        ⎟    ⎜      2           2         ⎟                                                                                                                         
        ╱           216⋅z       72⋅z      ╲╱          ⎝  6⋅z  - 3    ⎠    ⎝  54⋅z  - 27   6⋅z  - 3     ⎠                                                                                                                         
  3⋅   ╱        - ────────── + ──────── + ──────────────────────────────────────────────────────────────── - 8                                                                                                                   
    3 ╱               2           2                                      2                                                                                                                                                       
    ╲╱            54⋅z  - 27   6⋅z  - 3   

Because we disabled checking the solutions might not all be valid but you can verify for yourself which is correct.

Finally you can substitute in for z to these solutions but you will get some big and complicated expressions back so I won't show them here.

Upvotes: 2

Related Questions