1 year ago
#71271
coen paassen
Sympy holds function, instead of returning float
I am working on a school project, and got an error which I don't know how to solve. While using Sympy to get the partial derivatives of a function and later entering a x and y value, I am not able to get a single number out of it. I am holding a long expression like this:
-0.641831092731613*(0.5 + 0.2*pi)*exp(-0.2*(-pi - 2.5)**2) + 0.479462137331569*exp(-0.2*(-pi - 2.5)**2) - 0.96274663909742*(0.5 - 0.2*pi)*exp(-0.2*(-2.5 + pi)**2) + 0.719193205997354*exp(-0.2*(-2.5 + pi)**2)
I am using the N function build in Sympy, but it does not seem to do the trick. Anyone knows what I am doing wrong?
code:
def afgeleide(variabele):
x, y = sympy.symbols('x y', real=True)
z = -1*sympy.cos(x)*sympy.cos(y)*(sympy.exp(-0.1*((x-sympy.pi)**2+(y-sympy.pi)**2))+1.5*sympy.exp(-0.1*((x+sympy.pi)**2+(y+sympy.pi)**2)))
if variabele == "x":
return z.diff(x)
elif variabele == "y":
return z.diff(y)
else:
return "verkeerde variabele, probeer x of y"
def gradientdescent(beginx,beginy,alpha,aantalpogingen=0):
x, y = sympy.symbols('x y',real = True)
afgeleidex = afgeleide("x").subs([(x,beginx),(y,beginy)])
sympy.N(afgeleidex)
print(afgeleidex)
python
sympy
0 Answers
Your Answer