ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 20 Jun 2018 23:19:35 +0200How to substitute values for derivatives?https://ask.sagemath.org/question/42698/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?Wed, 20 Jun 2018 21:01:41 +0200https://ask.sagemath.org/question/42698/how-to-substitute-values-for-derivatives/Answer by Emmanuel Charpentier for <p>I would like to first differentiate a function, then substitute values for derivatives within the function.</p>
<p>For example, a function might be <code>cos(theta)*sin(phi)</code>. I would like to differentiate with respect to another variable (time in this case):</p>
<pre><code>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
</code></pre>
<p>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:</p>
<ol>
<li>Define each derivative to be a specific value and evaluate the expression, or</li>
<li>Substitute variables for derivatives, which can in turn be substituted with values and the expression evaluated?</li>
</ol>
https://ask.sagemath.org/question/42698/how-to-substitute-values-for-derivatives/?answer=42700#post-id-42700WorksForMe(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.Wed, 20 Jun 2018 23:19:35 +0200https://ask.sagemath.org/question/42698/how-to-substitute-values-for-derivatives/?answer=42700#post-id-42700