Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use math.log10 in sympy

Tags:

python

math

sympy

I have a problem with sympy, i need to solve a system of two equations:

B1=2.51/(Re(f^(0.5)))
f=(1/(-2*log10((epsilon/D/3.7)+B1)))^2

I tried 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
like image 582
Lea Di Donato Avatar asked Sep 05 '25 03:09

Lea Di Donato


1 Answers

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
like image 102
Stef Avatar answered Sep 07 '25 20:09

Stef