Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to substitute values for derivatives?

I would like to first differentiate a function, then substitute values for derivatives within the function.

For example, a function might be cos(theta)*sin(phi). I would like to differentiate with respect to another variable (time in this case):

sage: t = var('t')
sage: theta = function('theta')(t)
sage: phi = function('phi')(t)
sage: A = cos(theta)*sin(phi)
sage: C = diff(A,t,t)
sage: C
-cos(theta(t))*sin(phi(t))*diff(phi(t), t)^2 - 2*cos(phi(t))*sin(theta(t))*diff(phi(t), t)*diff(theta(t), t) - cos(theta(t))*sin(phi(t))*diff(theta(t), t)^2 + cos(phi(t))*cos(theta(t))*diff(phi(t), t, t) - sin(phi(t))*sin(theta(t))*diff(theta(t), t, t)
sage: C.subs(theta=5,phi=6,diff(theta(t),t)=7,diff(phi(t),t)=8,diff(theta(t),t,t)=9,diff(phi(t),t,t)=10)
  File "<ipython-input-147-f1b68bf72320>", line 1
SyntaxError: keyword can't be an expression

I believe the error comes from trying to define the entire function theta(t), diff(phi(t),t,t), etc. as a single number. It should work just as well to substitute other variables in the place of the derivatives; I only want the derivatives there for getting the initial equations. Is there a way to either:

  1. Define each derivative to be a specific value and evaluate the expression, or
  2. Substitute variables for derivatives, which can in turn be substituted with values and the expression evaluated?