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.Sat, 29 Nov 2014 23:34:34 +0100How to define more complicated differential expressions in Sage?https://ask.sagemath.org/question/24983/how-to-define-more-complicated-differential-expressions-in-sage/ Dear Community,
i have a very hard time doing something pretty straight foreward in Sage. In mechanics, there is a Lagrange-formalism that yields the equation of motion of a system of $n$ degrees of freedom in the following way:
$$
0=\frac{d}{dt}\left(\frac{\partial L}{\partial x'_i(t)}\right) - \frac{\partial L}{\partial x_i(t)}
$$
where $L$ is the Lagrange function
$$
L=E_{Kin} (x'_{1}(t),x'_2(t)...x'_n(t); x_1(t),x_2(t)...x_n(t) ) - E _{Pot}(x_1(t),x_2(t),...,x_n(t))
$$
is the kinetic energy minus the potential energy. The kinetic energy depends on the speeds of the coordinates $x'_i(t)$, while the potential energy depends on the coordinates $x_i(t)$.
In Mathematica, the hack is
L = Ekin[x'[t]] - Epot[x[t]];
eq = 0 == D[D[L, x'[t]], t] - D[L, x[t]];
which gives
0 == Epot'[x[t]] + Ekin''[x'[t]] x''[t]
I can easily specify something for $E_{Kin}$ and $E_{Pot}$, solve the latter equation for $x''$, and give it as a right handside to some time integration scheme.
How can this be done in Sage? Assume you have complicated expressions for $E_{Pot}$ and $E_{Kin}$ which you do not want to differentiate by hand.
`var('L,x,t')`
`x = function('x',t)`
`L = function('L',x,t)`
allows for
`diff(L,t)`
but not
`diff(L,x)`
Thanks already!Mon, 24 Nov 2014 19:58:57 +0100https://ask.sagemath.org/question/24983/how-to-define-more-complicated-differential-expressions-in-sage/Answer by bekalph for <p>Dear Community,</p>
<p>i have a very hard time doing something pretty straight foreward in Sage. In mechanics, there is a Lagrange-formalism that yields the equation of motion of a system of $n$ degrees of freedom in the following way:</p>
<p>$$
0=\frac{d}{dt}\left(\frac{\partial L}{\partial x'_i(t)}\right) - \frac{\partial L}{\partial x_i(t)}
$$</p>
<p>where $L$ is the Lagrange function </p>
<p>$$
L=E_{Kin} (x'_{1}(t),x'_2(t)...x'_n(t); x_1(t),x_2(t)...x_n(t) ) - E _{Pot}(x_1(t),x_2(t),...,x_n(t))
$$</p>
<p>is the kinetic energy minus the potential energy. The kinetic energy depends on the speeds of the coordinates $x'_i(t)$, while the potential energy depends on the coordinates $x_i(t)$.</p>
<p>In Mathematica, the hack is</p>
<pre><code>L = Ekin[x'[t]] - Epot[x[t]];
eq = 0 == D[D[L, x'[t]], t] - D[L, x[t]];
</code></pre>
<p>which gives </p>
<pre><code>0 == Epot'[x[t]] + Ekin''[x'[t]] x''[t]
</code></pre>
<p>I can easily specify something for $E_{Kin}$ and $E_{Pot}$, solve the latter equation for $x''$, and give it as a right handside to some time integration scheme. </p>
<p>How can this be done in Sage? Assume you have complicated expressions for $E_{Pot}$ and $E_{Kin}$ which you do not want to differentiate by hand.</p>
<p><code>var('L,x,t')</code></p>
<p><code>x = function('x',t)</code></p>
<p><code>L = function('L',x,t)</code></p>
<p>allows for </p>
<p><code>diff(L,t)</code></p>
<p>but not </p>
<p><code>diff(L,x)</code></p>
<p>Thanks already!</p>
https://ask.sagemath.org/question/24983/how-to-define-more-complicated-differential-expressions-in-sage/?answer=25002#post-id-25002Perhaps the answer I have given for the question "latex typesetting for derivatives" at 31.Oct. can help you.
One can utilize the option "derivative_func" of Function to let perform a list containing the requested results of the partial differentiations during processing a product rule or a chain of differentiations. Tue, 25 Nov 2014 19:08:57 +0100https://ask.sagemath.org/question/24983/how-to-define-more-complicated-differential-expressions-in-sage/?answer=25002#post-id-25002Comment by bekalph for <p>Perhaps the answer I have given for the question "latex typesetting for derivatives" at 31.Oct. can help you.
One can utilize the option "derivative_func" of Function to let perform a list containing the requested results of the partial differentiations during processing a product rule or a chain of differentiations. </p>
https://ask.sagemath.org/question/24983/how-to-define-more-complicated-differential-expressions-in-sage/?comment=25056#post-id-25056I want to add a remark concerning the real problem with D0(y).
You can substitute the default output of an derivative-operation, e.g. D[0] (fxx)(z)
by any symbolic function you want by using the 'derivative_func'- option of 'function'.
Following Example using symbolic function definition
- fx with this option,
- fxx without this option.:
var('z')
dfxdz=function('dfxdz',latex_name='\frac{\operatorname{d}{x}}{\operatorname{d}{z}}')
def dfx(self,args,*kwds):return dfxdz(z)
fxx=function('fxx',latex_name='x')
fx=function('fx',derivative_func=dfx,latex_name='x')
For the input
fx(z).derivative(z) #with derivative_func
you get the output :
dfxdz(z)
For the input
fxx(z).derivative(z) #without derivative_func
you get the output:
D[0] (fxx)(z)Sat, 29 Nov 2014 23:34:34 +0100https://ask.sagemath.org/question/24983/how-to-define-more-complicated-differential-expressions-in-sage/?comment=25056#post-id-25056Comment by schneider chen for <p>Perhaps the answer I have given for the question "latex typesetting for derivatives" at 31.Oct. can help you.
One can utilize the option "derivative_func" of Function to let perform a list containing the requested results of the partial differentiations during processing a product rule or a chain of differentiations. </p>
https://ask.sagemath.org/question/24983/how-to-define-more-complicated-differential-expressions-in-sage/?comment=25032#post-id-25032Thanks for your efforts. I took this as an inspiration to convert expressions using strings and subs(), but it is quite cumbersome. The real problem is that SAGE gives D[0](x)(y) as output, but does not accept it as input! Less an expression which can be solved or derived for. The problem that I asked a solution for can be solved in Sage, by constantly replacing functions by variables back and forth, depending on whether one wants either to solve/derive for or SAGE to apply rules of differentiation correctly.Fri, 28 Nov 2014 08:34:39 +0100https://ask.sagemath.org/question/24983/how-to-define-more-complicated-differential-expressions-in-sage/?comment=25032#post-id-25032