嘿伙计,所以我试图找到这个函数的衍生物:
y = 100e ^ -0.0482t
这是我的代码:
import math
from sympy import *
import numpy as np
x = Symbol('x')
y = (math.exp(-.0482 * x) * 100)
yprime = y.diff(x)
yprimerep = y.diff(5)
yprime
print(yprime)
我收到的错误是: TypeError:无法将表达式转换为float
我看过那个错误的各种含义,我不明白。我知道我做错了什么?
答案 0 :(得分:1)
import sympy
x = sympy.Symbol('x')
y = (sympy.exp(-.0482 * x) * 100)
yprime = y.diff(x) # get derivative
print yprime # print derivative
print y.diff(x, 5) # print 5th derivative
您的代码的问题是将math exp语句传递给sympy。
我不太熟悉同情,但我相信如果你想用5代替x,你可以做到
yprime.subs(x,5)
答案 1 :(得分:0)
无需导入数学和 numpy。 使用 sympy,因为其他模块中的值可能会使事情复杂化,并且模块的功能无法理解从其他模块派生的值。原则上,尽量坚持一个关于数学计算的模块。
在计算某个点的导数值时,您可以使用 sympy 中的 lamdify()。它将字符串转换为函数,让您计算该位置的值。
例如:
from sympy import *
x = Symbol('x')
f = input("Enter the function: ")
f_derivative = Derivative(f, x)
f_derivative = f_derivative.doit() #do the derivative and assign it to variable
print(f_derivative)
f = lambdify(x, f)
f_derivative = lambdify(x, f_derivative) #convert it to function
userInput = float(input("Try critical point: "))
print("Your input: ", userInput)
result = f_derivative(userInput) #pass the point to the function and assign it to variable
print("Result: ", result)