Home > Mobile >  how to use math.log10 in sympy
how to use math.log10 in sympy

Time:03-18

I have a problem whit sympy, i need to solve a system of two equation : B1=2.51/(Re(f^(0.5))) f=(1/(-2*log10((epsilon/D/3.7) B1)))^2 I tryed using sympy, numpy and fsolve, but there is a problem with the use of math.log10, that returns a float:

from sympy import*
import math
def f_cole(Epsilon, D, Re):

#Fattore di attrito Equazione di Colebrook per flusso turbolento

B1,f=symbols('B1,f')
eq1 =Eq(( 2.51 / (Re*(f**0.5))-B1),0)
eq2=Eq(((1/(-2* math.log10((Epsilon/D/ 3.7) B1)))**2 -f),0)

solveset((eq1,eq2), (B1,f))

return(f)

That returns :

TypeError: can't convert expression to float

CodePudding user response:

You cannot mix functions from module math with sympy expressions. Functions from math expect floats as input, not expressions.

Instead, use sympy.log.

import sympy

x = sympy.Symbol('x', real=True)
print('log(x):       ', sympy.log(x))
print('log(exp(x)):  ', sympy.simplify(sympy.log(sympy.exp(x))))
print('log10(x):     ', sympy.log(x, 10))
print('log10(10**x): ', sympy.simplify(sympy.log(10**x, 10)))

Output:

log(x):        log(x)
log(exp(x)):   x
log10(x):      log(x)/log(10)
log10(10**x):  x
  • Related