STEM 隨筆︰古典力學︰運動學【六‧上】

熟悉 numpy 、 scipy 、 sympy … 者,或早已能讀

Dynamics with Python balancing the five link pendulum

Dynamics with Python

We’ve been working on a conference paper to demonstrate the ability to do multibody dynamics with Python. We’ve been calling this work flowPyDy, short for Python Dynamics. Several pieces of the puzzle have come together lately to really demonstrate the power of the scientific python software packages to handle complex dynamic and controls problems (i.e. IPython notebooks, matplotlib animations, python-control, and our software package mechanics which is a part of SymPy). After writing the draft of our paper, which uses a general n-link pendulum as it’s main example, I came across this blog post by Wolfram demonstrating their ability to symbolically derive the equations of motion for the n-link pendulum and stabilize it with an LQR controller. It inspired me to replicate the example as I realized that it was relatively easy to do with all free and open source software!

In this example problem we will derive the equations of motion of an n-link pendulum on a laterally sliding cart and then develop a controller to stabilize it. Balancing a single inverted pendulum is a classic problem that is many times a student’s first experience with non-linear dynamics and control. The problem here is extended to a general n-link pendulum and as we will see the equations of motion quickly get messy with greater than 2 links.

 

也。早就了然

pydy/examples/npendulum/

The equations of motion are derived for a multi-pendulum with n links, the system is simulated, animated and a controller is developed to balance the pendulum. The problem is detailed in-depth in the IPython notebook file. It is hosted as a blog post athttp://www.moorepants.info/blog/npendulum.html.

 

 

範例於胸矣。但又何妨溫故知新耶?

/scipy-2017-codegen-tutorial

SymPy code generation tutorial at SciPy 2017 http://www.sympy.org/scipy-2017-codeg…

Automatic Code Generation with SymPy

sympy logo

Watch the video of the tutorial online

Automatic Code Generation with SymPy | SciPy 2017 Tutorial | Jason, Aaron, Björn & Kenneth

 

This tutorial will introduce code generation concepts using the SymPy library. SymPy is a pure Python library for symbolic mathematics. Code generation refers to the act of converting a SymPy symbolic expression into equivalent code in some language, typically for numeric evaluation. This allows one to use SymPy to symbolically model a problem and generate fast numerical code for specific platforms that executes that model. This is a powerful tool that is useful to scientists in many domains. Code generation allows users to speed up existing code, to deal only with the high level mathematics of a problem, avoids mathematical errors and typos, makes it possible to deal with expressions that would otherwise be too large to write by hand, and opens possibilities to perform automatic mathematical optimizations of expressions.

SymPy supports generating code for C, C++, Fortran, MATLAB/Octave, Python, Cython, Julia, JavaScript, LLVM, Rust, Haskell, Mathematica, Tensorflow, and Theano, and can easily be extended to other languages. SymPy’s code generation is used by libraries such as PyDy, pyodesys, sympybotics, pycalphad, and many other programs.

SciPy 2017 Codegen Tutorial

Introduction

This repository contains all of the source code and Jupyter notebooks for the SciPy 2017 tutorial “Automatic Code Generation with SymPy”.

The original proposal for this tutorial can be found on the SymPy Wiki.

The statically rendered Jupyter notebooks in this repository can be viewed at sympy.org/scipy-2017-codegen-tutorial or onNBViewer.

 

此所以引用

Numerical integration of Ordinary Differential Equations

※ 註

ODEPACKOrdinary Differential Equation Solvers


ODEPACK is a FORTRAN77 library which implements a variety of solvers for ordinary differential equations, by Alan Hindmarsh.

The library includes routines commonly referred to as

  • LSODE solves nonstiff or stiff systems y’ = f(y,t);
  • LSODES is like LSODE, but in the stiff case the Jacobian matrix is assumed to be sparse, and treated with sparse routines;
  • LSODA automatically switches between nonstiff and stiff solvers depending on the behavior of the problem;
  • LSODAR is like LSODE, but includes the ability to detect and solve for solutions of a related set of algebraic equations;
  • LSODPK is like LSODE, but uses preconditioned Krylov space iterative methods for the linear equation solvers;
  • LSODKR includes the rootfinding ability of LSODA, and the Krylov solvers of LSODPK.
  • LSODI solves the implicit system A(y,t)*y'(t) = g(y,t).
  • LSOIBT is like LSODI, but assumes the matrix A is block tridiagonal.
  • LSODIS is like LSODI, but assumes the matrix A is sparse.

ODEPACK is a collection of Fortran solvers for the initial value problem for ordinary differential equation systems. It consists of nine solvers, namely a basic solver called LSODE and eight variants of it — LSODES, LSODA, LSODAR, LSODPK, LSODKR, LSODI, LSOIBT, and LSODIS. The collection is suitable for both stiff and nonstiff systems. It includes solvers for systems given in explicit form, dy/dt = f(t,y), and also solvers for systems given in linearly implicit form, A(t,y) dy/dt = g(t,y). Two of the solvers use general sparse matrix solvers for the linear systems that arise. Two others use iterative (preconditioned Krylov) methods instead of direct methods for these linear systems. The most recent addition is LSODIS, which solves implicit problems with general sparse treatment of all matrices involved.

The ODEPACK solvers are written in standard Fortran 77, with a few exceptions, and with minimal machine dependencies. There are separate double and single precision versions of ODEPACK. The actual solver names are those given above with a prefix of D- or S- for the double or single precision version, respectively, i.e. DLSODE/SLSODE, etc. Each solver consists of a main driver subroutine having the same name as the solver and some number of subordinate routines. For each solver, there is also a demonstration program, which solves one or two simple problems in a somewhat self-checking manner.

Recently, the ODEPACK solvers were upgraded to improve their portability in numerous ways. Among the improvements are (a) renaming of routines and Common blocks to distinguish double and single precision versions, (b) use of generic intrinsic function names, (c) elimination of the Block Data subprogram, (d) use of a portable routine to set the unit roundoff, and (e) passing of quoted strings to the error message handler. In addition, the prologue and internal comments were reformatted, and use mixed upper/lower case. Numerous minor corrections and improvements were also made.

The above upgrade operations were applied to LSODE earlier than they were to the rest of ODEPACK, and the two upgrades were done somewhat independently. As a result, some differences will be apparent in the source files of LSODE and the other solvers — primarily in the formatting of the comment line prologue of the main driver routine. In Subroutines DLSODE/SLSODE and their subordinate routines, the prologue was written in “SLATEC format”, while for the other solvers a more relaxed style was used. The differences are entirely cosmetic, however, and do not affect performance.

Documentation on the usage of each solver is provided in the initial block of comment lines in the source file, which (in most cases) includes a simple example.

 

筆記,願君更上一層樓呦!