简体   繁体   English

TypeError:无法将表达式转换为浮点型

[英]TypeError: can't convert expression to float

I am a python newbie. 我是python新手。 I am trying to evaluate Planck equation using python. 我正在尝试使用python评估Planck方程。 I have written a simple program. 我写了一个简单的程序。 But when I give input to it is giving me an error. 但是,当我输入信息时,会给我一个错误。 can anyone hep me where I am going wrong? 谁能帮我解决我的问题? Here is the program and the error: 这是程序和错误:

Program: 程序:

from __future__ import division
from sympy.physics.units import *
from math import *
import numpy
from scipy.interpolate import interp1d

#Planck's Law evaluation at a single wavelength and temperature
def planck_law(wavelength,temperature):
    T=temperature
    f=c/wavelength
    h=planck
    k=boltzmann
    U=2*h/(c**3)*(f**3)/(exp(h*f/(k*T))-1)
    return U.evalf()

Input: I have imported the function as 'cp' and the input is as follows 输入:我已将函数导入为“ cp”,输入如下

value = (cp.planck_law(400,2000))

Error: 错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>`enter code here`
  File "Camera_performance.py", line 14, in planck_law
  U=2*h/(c**3)*(f**3)/(exp(h*f/(k*T))-1)
  File "/usr/lib/python2.7/dist-packages/sympy/core/expr.py", line 221, in __float__
  raise TypeError("can't convert expression to float")

TypeError: can't convert expression to float

It seem like you are mixing namespaces, since your are using from ... import * . 似乎您在混合名称空间,因为您使用的from ... import * You wanted to use sympy.exp() but your code uses math.exp() . 您想使用sympy.exp()但是您的代码使用math.exp() It is good practice to keep the namespaces separated, ie never use from ... import * - it might seem like more typing at first, but will produce much cleaner easier comprehensible code in the end. 好的做法是将名称空间保持分离,即从不使用from ... import * -乍一看似乎输入更多,但最终会产生更清晰易懂的代码。 Try: 尝试:

import sympy as sy
import sympy.physics.units as units

def planck_law(wavelength,temperature):
     """Planck's Law evaluation at a single wavelength and temperature """   
     T=temperature
     f=units.c/wavelength
     h=units.planck
     k=units.boltzmann
     U=2*h/(units.c**3)*(f**3)/(sy.exp(h*f/(k*T))-1)
     return U.evalf()

# Test:
print(planck_law(640e-9*units.m, 500*units.K))
# Result: 1.503553603007e-34*kg/(m*s)

Your term h*f/(k*T) isn't unitless, so you cannot pass it to exp() . 您的术语h*f/(k*T)不是无单位的,因此您不能将其传递给exp() Doesn't make sense in a physical context, either ;-) 在物理环境中也没有意义;-)

You can get a result if you divide it by K and m : 如果将结果除以Km则可以得到结果:

exp(h*f/(k*T)/K/m)

but that surely makes no sense. 但这肯定没有任何意义。 It's just to make the program run (into nonsense). 只是为了使程序运行(胡说八道)。

I guess you will have to check your formula and find out how you really want to compute the value you want to pass to exp() . 我猜您将不得不检查您的公式并找出您真正想要如何计算要传递给exp()

EDIT: 编辑:

As Pascal pointed out, you are just missing the units for the arguments you pass. 正如Pascal指出的那样,您只是缺少传递的参数的单位。 Try it with: 尝试使用:

planck_law(400e-9*m,2000*K)

Returns: 返回值:

3.20224927538564e-22*kg/(m*s)

When you call your function, you need to pass units to the temperature and wavelength argument. 调用函数时,需要将单位传递给温度和波长参数。 Calling cp.planck_law(400*meters,2000*K) gives the expected 1.15133857387385e-33*kg/(m*s) . 调用cp.planck_law(400*meters,2000*K)得到预期的1.15133857387385e-33*kg/(m*s)

The problem arose with the exponential function, which, rightfully, expects to receive a dimensionless argument. 问题是由指数函数引起的,该函数理所当然地希望收到一个无量纲的参数。 Since the arguments you were passing did not include units, Sympy treated them as dimensionless floats, rather than a temperature and a length, as required. 由于您传递的参数不包含单位,因此Sympy将其视为无量纲浮点,而不是根据需要的温度和长度。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:无法将表达式转换为使用Sympy浮动 - TypeError: can't convert expression to float with Sympy sympy TypeError:无法将表达式转换为浮点数 - sympy TypeError: can't convert expression to float Sympy:TypeError:无法将表达式转换为浮点型 - Sympy: TypeError: can't convert expression to float Python:sympy TypeError:无法将表达式转换为浮点数 - Python : sympy TypeError: can't convert expression to float 类型错误:无法将表达式转换为浮点数,使用符号 x,多项式插值 - TypeError: can't convert expression to float, with symbolic x, polynomial interpolation TypeError:无法将表达式转换为浮点数 - 定积分 sympy - TypeError: can't convert expression to float - definite integral sympy odeint -(TypeError: can't convert expression to float ) - 将表达式导入 function 以执行 odeint - odeint -(TypeError: can't convert expression to float ) - Importing expression into a function to perform odeint 如何做自然日志的派生类型错误:无法将表达式转换为浮点数 - How to do a derivative of a natural log getting TypeError: can't convert expression to float 非线性方程求解Sympy Python用于液压 - 需要解决TypeError(“无法将表达式转换为浮点数”) - Non-linear equation solving Sympy Python for hydraulics - Need resolve TypeError(“can't convert expression to float”) Sympy“无法将表达式转换为浮点数”错误 - “Can't convert expression to float” error with sympy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM