On Fri, Jun 3, 2011 at 8:05 PM, Gilbert Gede <gilbertg...@gmail.com> wrote:
> Can anyone give some insight into how I could get this desired behavior,
> taking the derivative of an expression wrt a time-differentiated symbol, to
> work in a way consistent with existing SymPy behavior?  Thanks.
> -Gilbert
>

I implemented Euler-Lagrange equations in Maple. I've put the code for
doing it below. Basically, I create symbols for each of the functions
and substitute these. Then, I take the derivative, and revert back.
This code is quite fast in Maple (at the time I originally wrote the
code, it was more than 2 orders of magnitude faster than Maple's
equivalent) and I think most of the functionality necessary is in
Python and SymPy. It should be fairly straightforward to convert to
SymPy. This code handles any number of variables (as a list), but it
only does the traditional Euler-Lagrange equation, not some of the
others that can arise due to Calculus of Variations.

I hope this helps.

Cheers,

Tim.

---
# Calculate the Mass (d/dt(dL_dqv)) and Stiffness (dL_dq) contributions to the
# Euler-Lagrange equation. Note that the Stiffness contribution does
not include the negative sign.
     EulerLagrange := proc(Lagrangian::anything, variables::list)
        local num_list, qv_name, vel_var, qv_subs, qv_unsubs, Lagrange_subs1,
Lagrange_subs2, dL_dqv1, dL_dqv2, dL_dqv, dL_dqvt, dL_dq,       dL_dq1,
dL_dq2, dL_dq3, q_name, q_subs, q_unsubs:

        # create a list of indices from 1 to the number of variables used in
the formulation
        num_list := [seq(i,i=1..nops(variables))]:

        # Define a list of generalized velocity and position variables
        qv_name := map2(cat,qv,num_list):
        q_name := map2(cat,q,num_list):

        # Equate the time derivatives of the system variable to the
generalized velocities
        # also define the reverse mapping
        vel_var := map(diff,variables,t):
        qv_subs := zip(equate,vel_var,qv_name):
        qv_unsubs := zip(equate,qv_name,vel_var):

        # Equate the generalized positions to the system variables and define
the reverse mapping
        q_subs := zip(equate,variables,q_name):
        q_unsubs := zip(equate,q_name,variables):

        # Convert the Lagrangian to the generalized position and velocity 
variables
        Lagrange_subs1 := subs(qv_subs,Lagrangian):
        Lagrange_subs2 := subs(q_subs,Lagrange_subs1):

        # Differentiate the Lagrangian with respect to the generalized
velocities and positions
        dL_dqv1 := map2(diff,Lagrange_subs2,qv_name):
        dL_dq1 := map2(diff,Lagrange_subs2,q_name):

        # Revert back to the system variables
        dL_dq2 := map2(subs,qv_unsubs,dL_dq1):
        dL_dqv2 := map2(subs,qv_unsubs,dL_dqv1):
        dL_dqv := map2(subs,q_unsubs,dL_dqv2):
        dL_dq := map2(subs,q_unsubs,dL_dq2):
        dL_dqvt := map(diff,dL_dqv,t):

        # Return the two components of the Euler-Lagrange Equation
        return (dL_dqvt, dL_dq):
    end proc:



-- 
Tim Lahey
PhD Candidate, Systems Design Engineering
University of Waterloo
http://about.me/tjlahey

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to sympy@googlegroups.com.
To unsubscribe from this group, send email to 
sympy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Reply via email to