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.Mon, 20 Feb 2017 19:58:54 +0100help on using chain rule in Sagehttps://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 08:25:01 +0100https://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>
https://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 11:04:15 +0100https://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>
https://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 19:58:54 +0100https://ask.sagemath.org/question/36673/help-on-using-chain-rule-in-sage/?comment=36677#post-id-36677