ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 29 Mar 2016 21:38:43 -0500finding the derivative of a functional w.r.t a functionhttps://ask.sagemath.org/question/32876/finding-the-derivative-of-a-functional-wrt-a-function/ I've defined a system as:
var('a, t');
function('x, y');
de1 = diff(x(t),t) == y(t);
de2 = diff(y(t),t) == -a*x(t) - (a-4)/a*y(t) - y(t)^3;
I'd like to compute the derivatives of the rhs of de1 and de2 w.r.t to x(t) and y(t) (to eventually form a Jacobian matrix), i.e. the derivative of the rhs of de1 w.r.t to x(t) is Zero and w.r.t y(t) is 1. I've tried the following:
diff(de1.rhs(),y);
diff(de1.rhs(),y(t));
derivative(de1.rhs(),y(t));
I get errors on all three. I'd appreciate any help. thank you.
Thu, 24 Mar 2016 01:50:20 -0500https://ask.sagemath.org/question/32876/finding-the-derivative-of-a-functional-wrt-a-function/Answer by nbruin for <p>I've defined a system as:</p>
<p>var('a, t');
function('x, y');</p>
<p>de1 = diff(x(t),t) == y(t);
de2 = diff(y(t),t) == -a<em>x(t) - (a-4)/a</em>y(t) - y(t)^3;</p>
<p>I'd like to compute the derivatives of the rhs of de1 and de2 w.r.t to x(t) and y(t) (to eventually form a Jacobian matrix), i.e. the derivative of the rhs of de1 w.r.t to x(t) is Zero and w.r.t y(t) is 1. I've tried the following:</p>
<p>diff(de1.rhs(),y);
diff(de1.rhs(),y(t));
derivative(de1.rhs(),y(t));</p>
<p>I get errors on all three. I'd appreciate any help. thank you.</p>
https://ask.sagemath.org/question/32876/finding-the-derivative-of-a-functional-wrt-a-function/?answer=32903#post-id-32903setting F=de2.rhs(), can you use that dF/dy = dF/dt / (dy/dt) ? Would the following do the trick?
sage: F=de2.rhs()
sage: (diff(F,t) / diff(y(t),t)).subs(de1).subs(de2)
-(3*(y(t)^3 + a*x(t) + (a - 4)*y(t)/a)*y(t)^2 - a*y(t) + (y(t)^3 + a*x(t) + (a - 4)*y(t)/a)*(a - 4)/a)/(y(t)^3 + a*x(t) + (a - 4)*y(t)/a)
For the other derivatives a similar approach should apply.Tue, 29 Mar 2016 02:42:02 -0500https://ask.sagemath.org/question/32876/finding-the-derivative-of-a-functional-wrt-a-function/?answer=32903#post-id-32903Comment by sophia for <p>setting F=de2.rhs(), can you use that dF/dy = dF/dt / (dy/dt) ? Would the following do the trick?</p>
<pre><code>sage: F=de2.rhs()
sage: (diff(F,t) / diff(y(t),t)).subs(de1).subs(de2)
-(3*(y(t)^3 + a*x(t) + (a - 4)*y(t)/a)*y(t)^2 - a*y(t) + (y(t)^3 + a*x(t) + (a - 4)*y(t)/a)*(a - 4)/a)/(y(t)^3 + a*x(t) + (a - 4)*y(t)/a)
</code></pre>
<p>For the other derivatives a similar approach should apply.</p>
https://ask.sagemath.org/question/32876/finding-the-derivative-of-a-functional-wrt-a-function/?comment=32913#post-id-32913that works great, thank you!Tue, 29 Mar 2016 21:38:43 -0500https://ask.sagemath.org/question/32876/finding-the-derivative-of-a-functional-wrt-a-function/?comment=32913#post-id-32913Comment by nbruin for <p>setting F=de2.rhs(), can you use that dF/dy = dF/dt / (dy/dt) ? Would the following do the trick?</p>
<pre><code>sage: F=de2.rhs()
sage: (diff(F,t) / diff(y(t),t)).subs(de1).subs(de2)
-(3*(y(t)^3 + a*x(t) + (a - 4)*y(t)/a)*y(t)^2 - a*y(t) + (y(t)^3 + a*x(t) + (a - 4)*y(t)/a)*(a - 4)/a)/(y(t)^3 + a*x(t) + (a - 4)*y(t)/a)
</code></pre>
<p>For the other derivatives a similar approach should apply.</p>
https://ask.sagemath.org/question/32876/finding-the-derivative-of-a-functional-wrt-a-function/?comment=32911#post-id-32911OK, in that case you should probably replace x(t), y(t) with variables and work with that:
sage: var('X,Y')
(X, Y)
sage: [[d.rhs().subs([x(t)==X,y(t)==Y]).diff(v) for d in [de1,de2]] for v in [X,Y]]
[[0, -a], [1, -3*Y^2 - (a - 4)/a]]Tue, 29 Mar 2016 15:21:46 -0500https://ask.sagemath.org/question/32876/finding-the-derivative-of-a-functional-wrt-a-function/?comment=32911#post-id-32911Comment by sophia for <p>setting F=de2.rhs(), can you use that dF/dy = dF/dt / (dy/dt) ? Would the following do the trick?</p>
<pre><code>sage: F=de2.rhs()
sage: (diff(F,t) / diff(y(t),t)).subs(de1).subs(de2)
-(3*(y(t)^3 + a*x(t) + (a - 4)*y(t)/a)*y(t)^2 - a*y(t) + (y(t)^3 + a*x(t) + (a - 4)*y(t)/a)*(a - 4)/a)/(y(t)^3 + a*x(t) + (a - 4)*y(t)/a)
</code></pre>
<p>For the other derivatives a similar approach should apply.</p>
https://ask.sagemath.org/question/32876/finding-the-derivative-of-a-functional-wrt-a-function/?comment=32910#post-id-32910your example is correct. I'm trying to accomplish something like this:
the rhs of de1 is just y(t). lets call the rhs of de1 F, so F = y(t). now dF/dy = 1. but dF/dx = 0, and that's what i'm trying to calculate - the derivative of the rhs of de1 w.r.t y and w.r.t x.Tue, 29 Mar 2016 15:12:33 -0500https://ask.sagemath.org/question/32876/finding-the-derivative-of-a-functional-wrt-a-function/?comment=32910#post-id-32910Comment by nbruin for <p>setting F=de2.rhs(), can you use that dF/dy = dF/dt / (dy/dt) ? Would the following do the trick?</p>
<pre><code>sage: F=de2.rhs()
sage: (diff(F,t) / diff(y(t),t)).subs(de1).subs(de2)
-(3*(y(t)^3 + a*x(t) + (a - 4)*y(t)/a)*y(t)^2 - a*y(t) + (y(t)^3 + a*x(t) + (a - 4)*y(t)/a)*(a - 4)/a)/(y(t)^3 + a*x(t) + (a - 4)*y(t)/a)
</code></pre>
<p>For the other derivatives a similar approach should apply.</p>
https://ask.sagemath.org/question/32876/finding-the-derivative-of-a-functional-wrt-a-function/?comment=32909#post-id-32909Why do you think that dx/dy should be 0?
If I take x(t)=sin(t), then y(t)=cos(t) satisfies dx/dt=y . Using the chain rule, it's straightforward to check that
dy/dx=d(cos(t))/d(sin(t))
is not equal to 0.Tue, 29 Mar 2016 14:38:06 -0500https://ask.sagemath.org/question/32876/finding-the-derivative-of-a-functional-wrt-a-function/?comment=32909#post-id-32909Comment by sophia for <p>setting F=de2.rhs(), can you use that dF/dy = dF/dt / (dy/dt) ? Would the following do the trick?</p>
<pre><code>sage: F=de2.rhs()
sage: (diff(F,t) / diff(y(t),t)).subs(de1).subs(de2)
-(3*(y(t)^3 + a*x(t) + (a - 4)*y(t)/a)*y(t)^2 - a*y(t) + (y(t)^3 + a*x(t) + (a - 4)*y(t)/a)*(a - 4)/a)/(y(t)^3 + a*x(t) + (a - 4)*y(t)/a)
</code></pre>
<p>For the other derivatives a similar approach should apply.</p>
https://ask.sagemath.org/question/32876/finding-the-derivative-of-a-functional-wrt-a-function/?comment=32908#post-id-32908Thanks a lot for replying. I tried what you suggested with de1 first, and it works when the rhs is only a function of y(t). so to compute dF/dx, i calculated it as (dF/dt)/(dx/dt) as follows:
sage: de1 = diff(x(t),t) == y(t)
sage: F = de1.rhs()
sage: (diff(F,t)/diff(x(t),t)).subs(de1)
the result is : D[0](y)(t)/y(t), whereas it should just be Zero.
any further suggestions would be greatly appreciated. Thank you.Tue, 29 Mar 2016 12:19:51 -0500https://ask.sagemath.org/question/32876/finding-the-derivative-of-a-functional-wrt-a-function/?comment=32908#post-id-32908