出现错误“只能将'exp'函数应用于无量纲的数量",不确定如何解决此问题 [英] Getting error "Can only apply 'exp' function to dimensionless quantities", Not sure how to fix this

查看:135
本文介绍了出现错误“只能将'exp'函数应用于无量纲的数量",不确定如何解决此问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为我的一位教授努力将一些MatLab代码转换为python(不是只是为了把一些东西拼在一起的作业),而我一直坚持这一部分.

I have been working on converting some MatLab code over to python for one of my professors (not an assignment just working on putting together some stuff) and I am stuck on this one part.

当我运行代码时,我得到UnitTypeError:只能将'exp'函数应用于无量纲的数量",我尝试解决此问题的所有方法均无效.我认为该错误是由linspace命令引起的,但不确定.任何帮助都会很棒.

When I run the code I am getting UnitTypeError: "Can only apply 'exp' function to dimensionless quantities", all of the methods I have tried to fix this won't work. I imagine the error is caused by the linspace command but am not sure. Any help with this would be great.

这是一行

IM0 = ((2*h*c**2)/(l**5))/(np.exp(h*c/( h*c/(k*T1*l)))-1)

常量来自天文学

h = const.h;
c = const.c;
k = const.k_B;
l = np.linspace(0, 1.5e-6, 1500);
T1 = 3750

推荐答案

astropy常量是类的实例.在将它们用作np.exp():

The astropy constants are instances of classes. Try extracting the "value" for each before using them as an argument to np.exp():

import astropy.constants as const
import numpy as np

h = const.h.value
c = const.c.value
k = const.k_B.value
l = np.linspace(0, 1.5e-6, 1500);
T1 = 3750
IM0 = ((2*h*c**2)/(l**5))/(np.exp(h*c/( h*c/(k*T1*l)))-1)

但是,请注意IM0存在数值问题.在所有l上的分母为零.

However please note there are numerical problems with IM0. The denominator is zero over all l.

这篇关于出现错误“只能将'exp'函数应用于无量纲的数量",不确定如何解决此问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆