双摆混沌理论 中的一个常见示例,这里通过 python 的作图工具包Matplotlib 来模拟双摆的运动过程:

注意:在 vs2017 中可以正确运行程序,在 cmd 环境下有时报错

"""
===========================
The double pendulum problem
=========================== This animation illustrates the double pendulum problem.
""" # Double pendulum formula translated from the C code at
# http://www.physics.usyd.edu.au/~wheat/dpend_html/solve_dpend.c from numpy import sin, cos
import numpy as np
import matplotlib.pyplot as plt
import scipy.integrate as integrate
import matplotlib.animation as animation G = 9.8 # acceleration due to gravity, in m/s^2
L1 = 1.0 # length of pendulum 1 in m
L2 = 1.0 # length of pendulum 2 in m
M1 = 1.0 # mass of pendulum 1 in kg
M2 = 1.0 # mass of pendulum 2 in kg def derivs(state, t): dydx = np.zeros_like(state)
dydx[0] = state[1] del_ = state[2] - state[0]
den1 = (M1 + M2)*L1 - M2*L1*cos(del_)*cos(del_)
dydx[1] = (M2*L1*state[1]*state[1]*sin(del_)*cos(del_) +
M2*G*sin(state[2])*cos(del_) +
M2*L2*state[3]*state[3]*sin(del_) -
(M1 + M2)*G*sin(state[0]))/den1 dydx[2] = state[3] den2 = (L2/L1)*den1
dydx[3] = (-M2*L2*state[3]*state[3]*sin(del_)*cos(del_) +
(M1 + M2)*G*sin(state[0])*cos(del_) -
(M1 + M2)*L1*state[1]*state[1]*sin(del_) -
(M1 + M2)*G*sin(state[2]))/den2 return dydx # create a time array from 0..100 sampled at 0.05 second steps
dt = 0.05
t = np.arange(0.0, 20, dt) # th1 and th2 are the initial angles (degrees)
# w10 and w20 are the initial angular velocities (degrees per second)
th1 = 120.0
w1 = 0.0
th2 = -10.0
w2 = 0.0 # initial state
state = np.radians([th1, w1, th2, w2]) # integrate your ODE using scipy.integrate.
y = integrate.odeint(derivs, state, t) x1 = L1*sin(y[:, 0])
y1 = -L1*cos(y[:, 0]) x2 = L2*sin(y[:, 2]) + x1
y2 = -L2*cos(y[:, 2]) + y1 fig = plt.figure()
ax = fig.add_subplot(111, autoscale_on=False, xlim=(-2, 2), ylim=(-2, 2))
ax.grid() line, = ax.plot([], [], 'o-', lw=2)
time_template = 'time = %.1fs'
time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes) def init():
line.set_data([], [])
time_text.set_text('')
return line, time_text def animate(i):
thisx = [0, x1[i], x2[i]]
thisy = [0, y1[i], y2[i]] line.set_data(thisx, thisy)
time_text.set_text(time_template % (i*dt))
return line, time_text ani = animation.FuncAnimation(fig, animate, np.arange(1, len(y)),
interval=25, blit=True, init_func=init) # ani.save('double_pendulum.mp4', fps=15)
plt.show()

原文地址:

https://matplotlib.org/examples/animation/double_pendulum_animated.html 作图工具包

https://docs.scipy.org/doc/scipy-0.18.1/reference/tutorial/general.html 微积分求解工具包

https://docs.huihoo.com/scipy/scipy-zh-cn/double_pendulum.html 简单双摆轨迹图

https://en.wikipedia.org/wiki/Catastrophe_theory 突变理论

双摆模拟 python(转)的更多相关文章

  1. Mock实现模拟python的对象

    Mock库的应用 Mock在Python3.3之前是第三方库,需要安装 pip install Mock :导入 import mock Mock在Python3.3之后是Python标准库,导入方式 ...

  2. OpenAI 开源机器人模拟 Python 库,并行模拟处理速度提升400%

    10000da.cnvboyule.cnjiaeidaypt.cn  在过去一年的研究中,OpenAI团队开源一个使用 MuJoCoengine开发的用于机器人模拟的高性能Python库.雷锋网了解到 ...

  3. Enigma模拟-Python

    设计思想 Enigma机的机械结构: 键盘:加密人员通过键盘进行输入 转子:Enigma机上一般装有至少3个转轮.每个转轮有代表26个字母的触头和触点,触点和触头在转轮内部有导线相连(一个转轮相当于一 ...

  4. 模拟python中的Yield伪并发

    并发,在操作系统中,是指一个时间段中有几个程序都处于已启动运行到运行完毕之间,且这几个程序都是在同一个处理机上运行,但任一个时刻点上只有一个程序在处理机上运行. #Yield伪并发 _author_= ...

  5. C++模拟python风格的print函数--打印vector,map,list等结构

    // 最基本实现 template<typename T> static void print(T t) { std::cout << t; } // 处理 std::pair ...

  6. Python 的mock模拟测试介绍

    如何不靠耐心测试 可能我们正在写一个社交软件并且想测试一下"发布到Facebook的功能",但是我们不希望每次运行测试集的时候都发布到Facebook上. Python的unitt ...

  7. Python学习--04条件控制与循环结构

    Python学习--04条件控制与循环结构 条件控制 在Python程序中,用if语句实现条件控制. 语法格式: if <条件判断1>: <执行1> elif <条件判断 ...

  8. CoffeeScript实现Python装潢器

    在上篇Angular遇上CoffeeScript – NgComponent封装中,我们讲述了CoffeeScript这门小巧的语言,摒弃JavaScript中糟粕(“坑”)部分,并将JavaScri ...

  9. Python IDLE 运行错误:IDLE's subprocess didn't make connection. --已解决(原创)!

    Python IDLE 错误描述: Subprocess Startup ErrorIDLE's subprocess didn't make connection. Either IDLE can' ...

随机推荐

  1. msp430入门编程10

    msp430中C语言操作端口I/O10 msp430中C语言的模块化头文件及实现11 msp430中C语言的模块化头文件及库文件12 msp430入门学习 msp430入门编程

  2. Devu and Flowers lucas定理+容斥原理

    Devu wants to decorate his garden with flowers. He has purchased n boxes, where the i-th box contain ...

  3. Bootstrap官网文档查询

    Ctrl+F 在出现的小搜索框里面输入要查找的东西.回车即可!

  4. js的声明与引入

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. SVN 资源权限管理系统 SVNAdmin

    原文:https://www.oschina.net/p/svn-admin

  6. 关于Java函数不支持参数默认值的讨论,最后一条亮了 2333

  7. Python标准库:内置函数tuple([iterable])

    本函数实现从可迭代对象生成一个元组对象返回.元组对象是一个不可改动的列表对象. 样例: #tuple() print(tuple([1, 2, 3])) print(tuple((1, 2, 3))) ...

  8. 踩坑录-mysql不允许远程连接(错误码:1130) Host'xxx.xxx.xxx.xxx' is not allowed to connect to this MySQL server“

    每次搭建mysql环境都会遇见同样的问题,在此分享一下踩坑笔录. 一.问题描述 安装成功后,本地直接链接远程mysql,默认为不允许远程访问,则客户端提示1130 - Host'xxx.xxx.xxx ...

  9. cocos2d-x中绘制3D图形--3D ToolKit for cocos2dx实现原理

    首先:了解具体情况请看这里:https://github.com/wantnon2/3DToolKit-for-cocos2dx 在看代码之前,最好还是先把项目git下来执行一下demoproject ...

  10. JSTL简单入门学习实例

    Maven依赖: <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</ ...