# 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
C.subs(theta=Integer(5),phi=Integer(6),diff(theta(t),t)=Integer(7),diff(phi(t),t)=Integer(8),diff(theta(t),t,t)=Integer(9),diff(phi(t),t,t)=Integer(10))
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?
edit retag close merge delete

Sort by » oldest newest most voted

WorksForMe(TM):

sage: var("t")
t
sage: theta=function("theta")
sage: A=cos(theta(t))
sage: diff(A,t)
-sin(theta(t))*diff(theta(t), t)
sage: var("B,C")
(B, C)
sage: diff(A,t).subs([diff(theta(t),t)==B,theta(t)==C])
-B*sin(C)


But remember that in the subs method, the single equal sign (which is a marker for keywords in a function call) is accepted for a single substitition. To to multiple substitutions, use either a list of equations (whose sides are separated by ==, i. e. a double equal sign) or a dictionary.

more