ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 20 Feb 2017 12:58:54 -0600help on using chain rule in Sagehttp://ask.sagemath.org/question/36673/help-on-using-chain-rule-in-sage/Does anyone have any tips for using Sage to take derivatives of functions of many variables?
For example, if I define
```w(x,y) = x^2 + y^2``` (say)
and then if I suppose that ```x``` and ```y``` depend on an independent variable ```t```, the chain rule applies for finding ```w.diff(t)```. The only way I found to do this in Sage is
```var('t')```
```x(t) = cos(t)``` (say)
```y(t) = sin(t)```
```w(x,y) = x^2 + y^2```
```w.diff(t)```
But this isn't really using the chain rule. If I instead try
```var('x,y')```
```(define w again)```
```(define x and y again)```
```w.diff(t)```
```Out: (x,y) |--> 0```
Apparently it thinks the derivative is zero because it still thinks ```x``` and ```y``` are two independent variables where they appear in ```w```, even though you get
```x```
```Out: t |--> cos(t)```
and similarly for ```y```. Any suggestions? Thanks.
Mon, 20 Feb 2017 01:25:01 -0600http://ask.sagemath.org/question/36673/help-on-using-chain-rule-in-sage/Answer by eric_g for <p>Does anyone have any tips for using Sage to take derivatives of functions of many variables? </p>
<p>For example, if I define </p>
<p><code>w(x,y) = x^2 + y^2</code> (say)</p>
<p>and then if I suppose that <code>x</code> and <code>y</code> depend on an independent variable <code>t</code>, the chain rule applies for finding <code>w.diff(t)</code>. The only way I found to do this in Sage is</p>
<p><code>var('t')</code></p>
<p><code>x(t) = cos(t)</code> (say)</p>
<p><code>y(t) = sin(t)</code> </p>
<p><code>w(x,y) = x^2 + y^2</code></p>
<p><code>w.diff(t)</code></p>
<p>But this isn't really using the chain rule. If I instead try</p>
<p><code>var('x,y')</code></p>
<p><code>(define w again)</code></p>
<p><code>(define x and y again)</code></p>
<p><code>w.diff(t)</code></p>
<p><code>Out: (x,y) |--> 0</code></p>
<p>Apparently it thinks the derivative is zero because it still thinks <code>x</code> and <code>y</code> are two independent variables where they appear in <code>w</code>, even though you get</p>
<p><code>x</code></p>
<p><code>Out: t |--> cos(t)</code></p>
<p>and similarly for <code>y</code>. Any suggestions? Thanks.</p>
http://ask.sagemath.org/question/36673/help-on-using-chain-rule-in-sage/?answer=36674#post-id-36674What about
sage: var('t u v')
(t, u, v)
sage: x = function('x')(t)
sage: y = function('y')(t)
sage: w(u,v) = u^2 + v^2
sage: diff(w(x,y), t)
2*x(t)*diff(x(t), t) + 2*y(t)*diff(y(t), t)
Mon, 20 Feb 2017 04:04:15 -0600http://ask.sagemath.org/question/36673/help-on-using-chain-rule-in-sage/?answer=36674#post-id-36674Comment by lefthandstander for <p>What about</p>
<pre><code>sage: var('t u v')
(t, u, v)
sage: x = function('x')(t)
sage: y = function('y')(t)
sage: w(u,v) = u^2 + v^2
sage: diff(w(x,y), t)
2*x(t)*diff(x(t), t) + 2*y(t)*diff(y(t), t)
</code></pre>
http://ask.sagemath.org/question/36673/help-on-using-chain-rule-in-sage/?comment=36677#post-id-36677Great! I see that ```diff(w,t)``` at the end gives the same behavior I was observing above. Thanks!Mon, 20 Feb 2017 12:58:54 -0600http://ask.sagemath.org/question/36673/help-on-using-chain-rule-in-sage/?comment=36677#post-id-36677