Home > Software engineering >  Calculating a variable in an equation to get multiple values
Calculating a variable in an equation to get multiple values

Time:11-10

I'm a python beginner and I'm trying to solve a equation. Here is the equation, which I am trying to solve for v:

**fSw - ((1)/(1 (Kro*Meu_w)/(Krw*Meu_nw))) = 0**

What I would like to do is set a single Meu_w and Meu_nw value and solve for a range of Kro and Krw values. I have tried making Krw and Kro arrays, but when I run my code, the only return I get is "[]". I believe this is an issue with the way I am trying to feed the set of values of Kro and Krw to the equation, i.e. What I want is to get multiple fSw values after feeding the arrays of Krw and Kro to the equation, but I'm not sure how to do this.

My code is as follows,

from sympy.solvers import solve
from sympy import Symbol

Meu_w = 1
Meu_nw = 2.4

fSw = Symbol('fSw')

Krw = np.array(Krw_fitted)
Kro = np.array(Kro_fitted)

equation = fSw - ((1)/(1 (Kro*Meu_w)/(Krw*Meu_nw)))
solutions = solve(equation,fSw)
print(f"fSw = {solutions}")

CodePudding user response:

This is not how SymPy works (with arrays) as you see.

You can make an array of fSw values by computing their values for each combination of array values. And since the equation is linear in fSw you can solve it once and just use the solution to give values once the array values are substituted:

equation = fSw - ((1)/(1 (Kro*Meu_w)/(Krw*Meu_nw)))
solution = solve(equation, fSw)[0]
M = []
for w in Krw_fitted:
    M.append([])
    for o in Kro_fitted:
        M[-1].append(solution.subs([(Kro,o),(Krw,w)]))

print(Matrix(M))  # will show your array of values
  • Related