0

I am trying to differentiate the function z. But it is giving me an error which says: raise TypeError("can't convert the expression to float")

TypeError: can't convert expression to float

I am not able to figure out my mistake.

from sympy import *
import numpy as np 
import math


T_wb =Symbol ('T_wb')
z =math.pow(10, 10.79574 * (1 - 273.16/T_wb)                    \          
    - 5.028 * math.log10(T_wb/273.16)                           \
            + 1.50475 * math.pow(10, -4)                        \
            * (1 - math.pow(10, -8.2969 * (T_wb / 273.16 - 1))) \
            + 0.42873 * math.pow(10, -3)                        \
            * (math.pow(10, 4.76955 * (1 - 273.16 / T_wb)) - 1) \
            + 2.78614)
zprime = z.diff(T_wb)
print (zprime)
xdze2
  • 3,986
  • 2
  • 12
  • 29

2 Answers2

2

The mathematical functions from the math module are not the same as the mathematical function from the sympy module. The first ones work on numbers (floats) as the second ones work on sympy expressions and symbols, in order to perform analytical derivation. Therefore, sympy.log has to be used instead of math.log.

The pow function is different. It is a built-in python function (equivalent to the operator **). So it is similar the other operators (+, -, *, /), there is no need to call a special function. For instance type(T_wb**2) gives well sympy.core.power.Pow

import sympy as sp

T_wb = sp.Symbol('T_wb')

z =   pow(10, 10.79574 * (1 - 273.16/T_wb)) \
    - 5.028 * sp.log(T_wb/273.16, 10) \
    + 1.50475e-4 * (1 - pow(10, -8.2969 * (T_wb / 273.16 - 1))) \
    + 0.42873e-3 * (pow(10, 4.76955 * (1 - 273.16 / T_wb)) - 1) \
    + 2.78614

zprime = z.diff(T_wb)
print(zprime)

gives:

0.55857099968694*10**(4.76955 - 1302.850278/T_wb)*log(10)/T_wb**2 + 2948.9643384*10**(10.79574 - 2948.9643384/T_wb)*log(10)/T_wb**2 + 4.57049358434617e-6*10**(-0.0303737736125348*T_wb + 8.2969)*log(10) - 5.028/(T_wb*log(10))
xdze2
  • 3,986
  • 2
  • 12
  • 29
0

Its throwing error in math.log10(T_wb/273.16) T_wb is a variable and when it is trying to calculate log sympy is not able to convert it into float because its a variable.

Ankit Kumar Namdeo
  • 1,426
  • 1
  • 12
  • 24