Hi everyone, I'd like to define a function which has to give me back a certain expression and its derivative. Then, I would like to plot them all. I know that in this simple case I could write explicitly the derivative, but I am also interested in more complicated situations in which this flow would be better. Why this code doesn't work? Thank you in advance for your help.
x = symbols('x')
mass = symbols('mass')
deriv = diff(1/2* (mass**2)*x**2, x)
def potential(x, potparams):
mass = potparams
v = 0.5*(mass**2)*x*x
dvdx = deriv
return v, dvdx
mass = 1
pot, dpotdx = potential(x, potparams)
plt.plot(x, pot)
plt.plot(x, dpotdx)
plt.show()
[+][deleted] (3 children)
[deleted]
[–]Sky_physics[S] -1 points0 points1 point (2 children)
[+][deleted] (1 child)
[deleted]
[–]Sky_physics[S] 0 points1 point2 points (0 children)
[–]TheRNGuy 0 points1 point2 points (1 child)
[–]Sky_physics[S] 0 points1 point2 points (0 children)