双摆模拟 python(转)
双摆 是 混沌理论 中的一个常见示例,这里通过 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(转)的更多相关文章
- Mock实现模拟python的对象
Mock库的应用 Mock在Python3.3之前是第三方库,需要安装 pip install Mock :导入 import mock Mock在Python3.3之后是Python标准库,导入方式 ...
- OpenAI 开源机器人模拟 Python 库,并行模拟处理速度提升400%
10000da.cnvboyule.cnjiaeidaypt.cn 在过去一年的研究中,OpenAI团队开源一个使用 MuJoCoengine开发的用于机器人模拟的高性能Python库.雷锋网了解到 ...
- Enigma模拟-Python
设计思想 Enigma机的机械结构: 键盘:加密人员通过键盘进行输入 转子:Enigma机上一般装有至少3个转轮.每个转轮有代表26个字母的触头和触点,触点和触头在转轮内部有导线相连(一个转轮相当于一 ...
- 模拟python中的Yield伪并发
并发,在操作系统中,是指一个时间段中有几个程序都处于已启动运行到运行完毕之间,且这几个程序都是在同一个处理机上运行,但任一个时刻点上只有一个程序在处理机上运行. #Yield伪并发 _author_= ...
- C++模拟python风格的print函数--打印vector,map,list等结构
// 最基本实现 template<typename T> static void print(T t) { std::cout << t; } // 处理 std::pair ...
- Python 的mock模拟测试介绍
如何不靠耐心测试 可能我们正在写一个社交软件并且想测试一下"发布到Facebook的功能",但是我们不希望每次运行测试集的时候都发布到Facebook上. Python的unitt ...
- Python学习--04条件控制与循环结构
Python学习--04条件控制与循环结构 条件控制 在Python程序中,用if语句实现条件控制. 语法格式: if <条件判断1>: <执行1> elif <条件判断 ...
- CoffeeScript实现Python装潢器
在上篇Angular遇上CoffeeScript – NgComponent封装中,我们讲述了CoffeeScript这门小巧的语言,摒弃JavaScript中糟粕(“坑”)部分,并将JavaScri ...
- Python IDLE 运行错误:IDLE's subprocess didn't make connection. --已解决(原创)!
Python IDLE 错误描述: Subprocess Startup ErrorIDLE's subprocess didn't make connection. Either IDLE can' ...
随机推荐
- 如何快速掌握plc或工控机与其他设备的modbus通讯协议?包括格式与实际过程 RT,本人从事工控行业多年,对于PLC与触摸屏也算比较熟悉,唯独对这个通讯协议比较难理解,请教高人指导,从什么地方开始下手,或者是说如何正确理解报文格式或正确写入
Modbus协议是OSI模型的第七层的应用层通讯协议,定义了不同类型设备间交换信息方式,以及信息的格式. Modbus的工作方式是请求/应答,每次通讯都是主站先发送指令,可以是广播,或是向特定从站的单 ...
- FTP指令说明
安装vsftpd: listen=YES: 是否监听端口 anonymous_enable=NO: 是否启用匿名用户 local_enable=YES: 是否允许本地用户登录 write_enable ...
- Mariadb 索引及外键
索引 索引相当于一本书的目录,在一个数据库或表有索引的情况下,会很便于查询数据,使查询更加效率,相对的也有缺点,不利于去修改,比较麻烦,有索引便于查询,那就意味着索引创建的越多越好么?然而并不是:索引 ...
- RELU 激活函数及其他相关的函数
RELU 激活函数及其他相关的函数 转载 2016年07月21日 20:51:17 45778 本博客仅为作者记录笔记之用,不免有很多细节不对之处. 还望各位看官能够见谅,欢迎批评指正. 更多相关博客 ...
- 利用SEH进行代码混淆
这几天在重看SEH机制,收获颇丰. 随手写了一个用SEH进行跳转的代码贴于此处以作纪念. 当发生异常,并捕捉了异常.在OS的异常处理机制下.会进入异常过滤函数. 过滤函数能够返回EXCEPTION_E ...
- 在Linux上安装zsh
简单介绍: 相对于绝大多数linux发行版默认的shell--bash,zsh绝对是一个优秀的替代品.zsh是交互型shell,同一时候它也是一个强大的编程语言,很多bash,ksh,tcsh优秀的地 ...
- [计算机故障]为什么我的手机SD卡一打开就是说“你的磁盘未格式化,现在需要格式化吗”?
现在随着智能手机的普及,越来越多的人使用到了手机SD卡.也有的是micro SD(更小一些). 最近一个朋友说,为什么我的手机SD卡插到手机里一打开就是说“你的磁盘未格式化,现在需要格式化吗?” 但是 ...
- go16---select
package main /* Channel Channel 是 goroutine 沟通的桥梁, goroutine是通过通信来进行内存的共享, 而不是通过内存的共享来进行通信,通过Channel ...
- Android默认系统声音/大小修改及配置【转】
本文转载自:http://blog.csdn.net/a8316124/article/details/60574859 在做定制需求的时候,需要修改系统通知的声音,将其禁用掉,避免第三方应用发送通知 ...
- js鼠标事情
js鼠标事情 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset=" ...