Ask Your Question
1

How to substitute values for derivatives?

asked 2018-06-20 21:01:41 +0100

bob323 gravatar image

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 flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2018-06-20 23:19:35 +0100

Emmanuel Charpentier gravatar image

updated 2018-06-20 23:22:22 +0100

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2018-06-20 21:01:41 +0100

Seen: 839 times

Last updated: Jun 20 '18