利用python求解物理学中的双弹簧质能系统详解


Posted in Python onSeptember 29, 2017

前言

本文主要给大家介绍了关于利用python求解物理学中双弹簧质能系统的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

物理的模型如下:

利用python求解物理学中的双弹簧质能系统详解

在这个系统里有两个物体,它们的质量分别是m1和m2,被两个弹簧连接在一起,伸缩系统为k1和k2,左端固定。假定没有外力时,两个弹簧的长度为L1和L2。

由于两物体有重力,那么在平面上形成摩擦力,那么摩擦系数分别为b1和b2。所以可以把微分方程写成这样:

利用python求解物理学中的双弹簧质能系统详解

这是一个二阶的微分方程,为了使用python来求解,需要把它转换为一阶微分方程。所以引入下面两个变量:

利用python求解物理学中的双弹簧质能系统详解

这两个相当于运动的速度。通过运算可以改为这样:

利用python求解物理学中的双弹簧质能系统详解

这时可以线性方程改为向量数组的方式,就可以使用python定义了

代码如下:

# Use ODEINT to solve the differential equations defined by the vector field 
from scipy.integrate import odeint 
 
def vectorfield(w, t, p): 
 """ 
 Defines the differential equations for the coupled spring-mass system. 
 
 Arguments: 
  w : vector of the state variables: 
     w = [x1,y1,x2,y2] 
  t : time 
  p : vector of the parameters: 
     p = [m1,m2,k1,k2,L1,L2,b1,b2] 
 """ 
 x1, y1, x2, y2 = w 
 m1, m2, k1, k2, L1, L2, b1, b2 = p 
 
 # Create f = (x1',y1',x2',y2'): 
 f = [y1, 
   (-b1 * y1 - k1 * (x1 - L1) + k2 * (x2 - x1 - L2)) / m1, 
   y2, 
   (-b2 * y2 - k2 * (x2 - x1 - L2)) / m2] 
 return f 
 
# Parameter values 
# Masses: 
m1 = 1.0 
m2 = 1.5 
# Spring constants 
k1 = 8.0 
k2 = 40.0 
# Natural lengths 
L1 = 0.5 
L2 = 1.0 
# Friction coefficients 
b1 = 0.8 
b2 = 0.5 
 
# Initial conditions 
# x1 and x2 are the initial displacements; y1 and y2 are the initial velocities 
x1 = 0.5 
y1 = 0.0 
x2 = 2.25 
y2 = 0.0 
 
# ODE solver parameters 
abserr = 1.0e-8 
relerr = 1.0e-6 
stoptime = 10.0 
numpoints = 250 
 
# Create the time samples for the output of the ODE solver. 
# I use a large number of points, only because I want to make 
# a plot of the solution that looks nice. 
t = [stoptime * float(i) / (numpoints - 1) for i in range(numpoints)] 
 
# Pack up the parameters and initial conditions: 
p = [m1, m2, k1, k2, L1, L2, b1, b2] 
w0 = [x1, y1, x2, y2] 
 
# Call the ODE solver. 
wsol = odeint(vectorfield, w0, t, args=(p,), 
    atol=abserr, rtol=relerr) 
 
with open('two_springs.dat', 'w') as f: 
 # Print & save the solution. 
 for t1, w1 in zip(t, wsol):   
  out = '{0} {1} {2} {3} {4}\n'.format(t1, w1[0], w1[1], w1[2], w1[3]); 
  print(out) 
  f.write(out);

在这里把结果输出到文件two_springs.dat,接着写一个程序来把数据显示成图片,就可以发表论文了,代码如下:

# Plot the solution that was generated 
 
from numpy import loadtxt 
from pylab import figure, plot, xlabel, grid, hold, legend, title, savefig 
from matplotlib.font_manager import FontProperties 
 
t, x1, xy, x2, y2 = loadtxt('two_springs.dat', unpack=True) 
 
figure(1, figsize=(6, 4.5)) 
 
xlabel('t') 
grid(True) 
lw = 1 
 
plot(t, x1, 'b', linewidth=lw) 
plot(t, x2, 'g', linewidth=lw) 
 
legend((r'$x_1$', r'$x_2$'), prop=FontProperties(size=16)) 
title('Mass Displacements for the\nCoupled Spring-Mass System') 
savefig('two_springs.png', dpi=100)

最后来查看一下输出的png图片如下:

利用python求解物理学中的双弹簧质能系统详解

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对三水点靠木的支持。

Python 相关文章推荐
基于Python的身份证号码自动生成程序
Aug 15 Python
Python实现查找系统盘中需要找的字符
Jul 14 Python
在Django中管理Users和Permissions以及Groups的方法
Jul 23 Python
python 计算文件的md5值实例
Jan 13 Python
Python使用sorted排序的方法小结
Jul 28 Python
TensorFlow Session会话控制&Variable变量详解
Jul 30 Python
PyQt 图解Qt Designer工具的使用方法
Aug 06 Python
Python如何在DataFrame增加数值
Feb 14 Python
django 扩展user用户字段inlines方式
Mar 30 Python
Python内置函数property()如何使用
Sep 01 Python
python基础入门之普通操作与函数(三)
Jun 13 Python
python中mongodb包操作数据库
Apr 19 Python
python中装饰器级连的使用方法示例
Sep 29 #Python
python中map()函数的使用方法示例
Sep 29 #Python
python中reduce()函数的使用方法示例
Sep 29 #Python
Django Admin 实现外键过滤的方法
Sep 29 #Python
python 调用c语言函数的方法
Sep 29 #Python
python文件名和文件路径操作实例
Sep 29 #Python
Python 实现简单的shell sed替换功能(实例讲解)
Sep 29 #Python
You might like
如何给phpcms v9增加类似于phpcms 2008中的关键词表
2013/07/01 PHP
php利用smtp类实现电子邮件发送
2015/10/30 PHP
PHP实现支持CURL字符串证书传输的方法
2019/03/23 PHP
PHP实现带进度条的Ajax文件上传功能示例
2019/07/02 PHP
JavaScript多线程的实现方法
2007/05/08 Javascript
JavaScript代码复用模式实例分析
2012/12/02 Javascript
HTML复选框和单选框 checkbox和radio事件介绍
2012/12/12 Javascript
jquery中的ajax方法怎样通过JSONP进行远程调用
2014/05/04 Javascript
使用JavaScript获取地址栏参数的方法
2014/12/19 Javascript
javascript HTML5文件上传FileReader API
2020/03/27 Javascript
jQuery插件扩展操作入门示例
2017/01/16 Javascript
Javascript仿京东放大镜的效果
2017/03/01 Javascript
nodejs入门教程二:创建一个简单应用示例
2017/04/24 NodeJs
JS实现闭包中的沙箱模式示例
2017/09/07 Javascript
微信小程序项目实践之主页tab选项实现
2018/07/18 Javascript
详解微信小程序之scroll-view的flex布局问题
2019/01/16 Javascript
使用Vue实现调用接口加载页面初始数据
2019/10/28 Javascript
Vue 使用Props属性实现父子组件的动态传值详解
2019/11/13 Javascript
原生JavaScript实现留言板
2021/01/10 Javascript
如何管理Vue中的缓存页面
2021/02/06 Vue.js
Flask的图形化管理界面搭建框架Flask-Admin的使用教程
2016/06/13 Python
Python入门之三角函数sin()函数实例详解
2017/11/08 Python
python3下实现搜狗AI API的代码示例
2018/04/10 Python
Python之pandas读写文件乱码的解决方法
2018/04/20 Python
修改python plot折线图的坐标轴刻度方法
2018/12/13 Python
Python机器学习算法库scikit-learn学习之决策树实现方法详解
2019/07/04 Python
详解CSS3中常用的样式【基本文本和字体样式】
2020/10/20 HTML / CSS
资深财务管理人员自我评价
2013/09/22 职场文书
政治思想表现评语
2014/05/04 职场文书
博士生专家推荐信
2014/09/26 职场文书
体育教师个人工作总结
2015/02/09 职场文书
环卫个人总结
2015/03/03 职场文书
2015年人事专员工作总结
2015/04/29 职场文书
Java 在线考试云平台的实现
2021/11/23 Java/Android
mysql sock 文件解析及作用讲解
2022/07/15 MySQL
JS前端宏任务微任务及Event Loop使用详解
2022/07/23 Javascript