1

I want to solve for x in eq1 using sympy. When I try to solve the equation using sp.solve(), I get an error. When I use sp.solve_linear(), it seems to work but I am unable to evaulate the numerical value of the solution. All my variables are defined except x, and I want to solve for x. Is there another solver I could use? My code is below:

import sympy as sp
from sympy import pi, log

R0, Hb, P_, y, m, xhi_ = sp.symbols("0.003 5e8, 1000 2500 4*pi*10e-7 1")
P = P_/2

x,  y = sp.symbols('x y')

eq1 = x - sp.sqrt((P - (m*(y*y)/2*pi)*log(R0/x))/(pi*xhi_*Hb))

a = sp.solve_linear(eq1, x)

sp.pprint(a)
sp.pprint(sp.N(a))
crisperm
  • 11
  • 1

1 Answers1

0

I think you would actually need:

import sympy as sp
from sympy import pi, log, sqrt

R0, Hb, P_, y, m, xhi_, x = sp.symbols("R0 Hb P_ y m xhi_ x")
P = P_/2

eq1 = x - sp.sqrt((P - (m*(y*y)/2*pi)*log(R0/x))/(pi*xhi_*Hb))

a = sp.solve_linear(eq1, x)

sp.pprint(a)

⎛         ____________________    ⎞
⎜        ╱          2    ⎛R₀⎞     ⎟
⎜       ╱      π⋅m⋅y ⋅log⎜──⎟     ⎟
⎜      ╱   P             ⎝x ⎠     ⎟
⎜     ╱    ─ - ──────────────     ⎟
⎜    ╱     2         2            ⎟
⎜-  ╱      ────────────────── , √π⎟
⎝ ╲╱             Hb⋅xhi           ⎠

Then you could do:

import numpy as np

a[0].subs({R0:0.003, Hb:5e8, P_:1000, y:2500, m:4*np.pi*10e-7, xhi_:1, pi:np.pi})
-4.47213595499958e-5*sqrt(-123.370055013617*log(0.003/x) + 500)

But that's not what you had in mind originally, if I understood correctly; you actually want a value for x, so solve would be appropriate:

a = sp.solve(eq1, x)

That fails with

NotImplementedError: multiple generators [x, sqrt(P_/(2*Hbxhi_) - pim*y**2*log(R0/x)/(2*Hb*xhi_))]

No algorithms are implemented to solve equation x - sqrt((P_/2 - pimy**2*log(R0/x)/2)/(Hb*xhi_))/sqrt(pi)

which seems to imply that this equation cannot be solved analytically be sympy.

You could try a numerically solver as shown e.g. here.

Cleb
  • 25,102
  • 20
  • 116
  • 151