简体   繁体   English

Python:sympy TypeError:无法将表达式转换为浮点数

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

Currently, I'm working on a calculator, that works similar to a 'real' calculator, when determining definite integrals.目前,我正在研究一个计算器,在确定定积分时,它的工作原理类似于“真实”计算器。

Currently i can get it to work with functions such as目前我可以让它与功能一起工作,例如

  • sin(x)
  • cos(x)
  • e**x
  • n*x**x

However, it won't accept math.sqrt(x) as a function in my code, where it simply states, that但是,它不会在我的代码中接受math.sqrt(x)作为函数,它只是指出,

File "C:\Users\Nikolai Lund Kühne\.spyder-py3\integration.py", line 6, in <module>
  print(series(math.sqrt(x), x, x0=0, n=6))

File "C:\ProgramData\Anaconda3\lib\site-packages\sympy\core\expr.py", line 327, in __float__
  raise TypeError("can't convert expression to float")

TypeError: can't convert expression to float

My code is:我的代码是:

from sympy.functions import sin,cos
from sympy.abc import x
from sympy import series
from pprint import pprint
# Indsæt her funktionen f(x), variablen x, udviklingspunktet x0 og antal led n
print(series(math.sqrt(x), x, x0=0, n=6))

N = int(input("Antal summer(flere summer er mere præcist): "))
a = int(input("Integrer fra: "))
b = int(input("Integrer til: "))

# Vi anvender Midpoint metoden til integration og skriver funktionen ind, som skal integreres

def integrate(N, a, b):
    def f(x):
        return series(math.sqrt(x), x, x0=0, n=6)
    value=0
    value=2
    for n in range(1, N+1):
        value += f(a+((n-(1/2))*((b-a)/N)))
    value2 = ((b-a)/N)*value
    return value2

print("...................")
print("Her er dit svar: ")
print(integrate(N, a, b))

Can anyone help me here, it's greatly appreciated.任何人都可以在这里帮助我,非常感谢。

Disclaimer: I am quite new to programming and Python and would appreciate any help given.免责声明:我对编程和 Python 很陌生,希望得到任何帮助。 Sorry for the strange setup, I am used to LaTeX and MathJax when writing questions :)抱歉奇怪的设置,我在写问题时习惯了 LaTeX 和 MathJax :)

You get the error:你得到错误:

TypeError: can't convert expression to float

Since the argument passed as expr is math.sqrt(x) , and sympy doesn't expect that.由于作为expr传递的参数是math.sqrt(x) ,而sympy并不期望这一点。

Change from math.sqrt(x) to x**0.5 :math.sqrt(x)更改为x**0.5

print(series(x**0.5, x, x0=0, n=6))

The same applies to line 16.这同样适用于第 16 行。

声明:本站的技术帖子网页,遵循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 TypeError:无法将表达式转换为浮点数 - 定积分 sympy - TypeError: can't convert expression to float - definite integral sympy 非线性方程求解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 无法将表达式转换为浮点数 - 使用 sympy - Can't convert expression to float - using sympy TypeError:无法将表达式转换为浮点型 - TypeError: can't convert expression to float 类型错误:无法将表达式转换为浮点数,使用符号 x,多项式插值 - TypeError: can't convert expression to float, with symbolic x, polynomial interpolation sympy的plot_parametric说TypeError:无法将复数转换为浮点数 - sympy's plot_parametric says TypeError: can't convert complex to float
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM