利用Python求解微分方程怎么实现,用什么方法
Admin 2022-06-30 群英技术资讯 741 次浏览
这篇文章将为大家详细讲解有关“利用Python求解微分方程怎么实现,用什么方法”的知识,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。对于一些微分方程来说,数值解法对于求解具有很好的帮助,因为难以求得其原方程。
比如方程:

但是我们知道了它的初始条件,这对于我们叠代求解很有帮助,也是必须的。

那么现在我们也用Python去解决这一些问题,一般的数值解法有欧拉法、隐式梯形法等,我们也来看看这些算法对叠代的精度有什么区别?
```python
```python
import numpy as np
from scipy.integrate import odeint
from matplotlib import pyplot as plt
import os
#先从odeint函数直接求解微分方程
#创建欧拉法的类
class Euler:
#构造方法,当创建对象的时候,自动执行的函数
def __init__(self,h,y0):
#将对象与对象的属性绑在一起
self.h = h
self.y0 = y0
self.y = y0
self.n = 1/self.h
self.x = 0
self.list = [1]
#欧拉法用list列表,其x用y叠加储存
self.list2 = [1]
self.y1 = y0
#改进欧拉法用list2列表,其x用y1叠加储存
self.list3 = [1]
self.y2 = y0
#隐式梯形法用list3列表,其x用y2叠加储存
#欧拉法的算法,算法返回t,x
def countall(self):
for i in range(int(self.n)):
y_dere = -20*self.list[i]
#欧拉法叠加量y_dere = -20 * x
y_dere2 = -20*self.list2[i] + 0.5*400*self.h*self.list2[i]
#改进欧拉法叠加量 y_dere2 = -20*x(k) + 0.5*400*delta_t*x(k)
y_dere3 = (1-10*self.h)*self.list3[i]/(1+10*self.h)
#隐式梯形法计算 y_dere3 = (1-10*delta_t)*x(k)/(1+10*delta_t)
self.y += self.h*y_dere
self.y1 += self.h*y_dere2
self.y2 =y_dere3
self.list.append(float("%.10f" %self.y))
self.list2.append(float("%.10f"%self.y1))
self.list3.append(float("%.10f"%self.y2))
return np.linspace(0,1,int(self.n+1)), self.list,self.list2,self.list3
step = input("请输入你需要求解的步长:")
step = float(step)
work1 = Euler(step,1)
ax1,ay1,ay2,ay3 = work1.countall()
#画图工具plt
plt.figure(1)
plt.subplot(1,3,1)
plt.plot(ax1,ay1,'s-.',MarkerFaceColor = 'g')
plt.xlabel('横坐标t',fontproperties = 'simHei',fontsize =20)
plt.ylabel('纵坐标x',fontproperties = 'simHei',fontsize =20)
plt.title('欧拉法求解微分线性方程步长为'+str(step),fontproperties = 'simHei',fontsize =20)
plt.subplot(1,3,2)
plt.plot(ax1,ay2,'s-.',MarkerFaceColor = 'r')
plt.xlabel('横坐标t',fontproperties = 'simHei',fontsize =20)
plt.ylabel('纵坐标x',fontproperties = 'simHei',fontsize =20)
plt.title('改进欧拉法求解微分线性方程步长为'+str(step),fontproperties = 'simHei',fontsize =20)
plt.subplot(1,3,3)
plt.plot(ax1,ay3,'s-.',MarkerFaceColor = 'b')
plt.xlabel('横坐标t',fontproperties = 'simHei',fontsize =20)
plt.ylabel('纵坐标x',fontproperties = 'simHei',fontsize =20)
plt.title('隐式梯形法求解微分线性方程步长为'+str(step),fontproperties = 'simHei',fontsize =20)
plt.figure(2)
plt.plot(ax1,ay1,ax1,ay2,ax1,ay3,'s-.',MarkerSize = 3)
plt.xlabel('横坐标t',fontproperties = 'simHei',fontsize =20)
plt.ylabel('纵坐标x',fontproperties = 'simHei',fontsize =20)
plt.title('三合一图像步长为'+str(step),fontproperties = 'simHei',fontsize =20)
ax = plt.gca()
ax.legend(('$Eular$','$fixed Eular$','$trapezoid$'),loc = 'lower right',title = 'legend')
plt.show()
os.system("pause")
对于欧拉法,它的叠代方法是:

改进欧拉法的叠代方法:

隐式梯形法:

对于不同的步长,其求解的精度也会有很大的不同,我先放一几张结果图:

补充:基于python的微分方程数值解法求解电路模型
安装numpy(用于调节range) 和 matplotlib(用于绘图)
在命令行输入
pip install numpy pip install matplotlib
无损害,电容电压为5V,电容为0.01F,电感为0.01H的并联谐振电路
电路模型1

微分方程1

带电阻损耗的电容电压为5V,电容为0.01F,电感为0.01H的的并联谐振
电路模型2

微分方程2

import numpy as np
import matplotlib.pyplot as plt
L = 0.01 #电容的值 F
C = 0.01 #电感的值 L
u_0 = 5 #电容的初始电压
u_dot_0 = 0
def equition(u,u_dot):#二阶方程
u_double_dot = -u/(L*C)
return u_double_dot
def draw_plot(time_step,time_scale):#时间步长和范围
u = u_0
u_dot = u_dot_0 #初始电压和电压的一阶导数
time_list = [0] #时间lis
Votage = [u] #电压list
plt.figure()
for time in np.arange(0,time_scale,time_step):#使用欧拉数值计算法 一阶近似
u_double_dot = equition(u,u_dot) #二阶导数
u_dot = u_dot + u_double_dot*time_step #一阶导数
u = u + u_dot*time_step #电压
time_list.append(time) #结果添加
Votage.append(u) #结果添加
print(u)
plt.plot(time_list,Votage,"b--",linewidth=1) #画图
plt.show()
plt.savefig("easyplot.png")
if __name__ == '__main__':
draw_plot(0.0001,1)
import numpy as np
import matplotlib.pyplot as plt
L = 0.01 #电容的值 F
C = 0.01 #电感的值 L
R = 0.1 #电阻值
u_0 = 5 #电容的初始电压
u_dot_0 = 0
def equition(u,u_dot):#二阶方程
u_double_dot =(-R*C*u_dot -u)/(L*C)
return u_double_dot
def draw_plot(time_step,time_scale):#时间步长和范围
u = u_0
u_dot = u_dot_0 #初始电压和电压的一阶导数
time_list = [0] #时间lis
Votage = [u] #电压list
plt.figure()
for time in np.arange(0,time_scale,time_step):#使用欧拉数值计算法 一阶近似
u_double_dot = equition(u,u_dot) #二阶导数
u_dot = u_dot + u_double_dot*time_step #一阶导数
u = u + u_dot*time_step #电压
time_list.append(time) #结果添加
Votage.append(u) #结果添加
print(u)
plt.plot(time_list,Votage,"b-",linewidth=1) #画图
plt.show()
plt.savefig("result.png")
if __name__ == '__main__':
draw_plot(0.0001,1)
模型1

纵轴为电容两端电压,横轴为时间与公式计算一致
模型2结果

为电容两端电压,横轴为时间标题
最后我们可以根据调节电阻到达不同的状态

R=0.01,欠阻尼

R=1.7,临界阻尼

R=100,过阻尼
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍了python量化之搭建Transformer模型用于股票价格预测,文章围绕主题展开基于python搭建Transformer,需要的小伙伴可以参考一下
浏览器到WSGI Server:浏览器发送的请求会先到WSGI Server;environ:WSGI Server会将HTTP请求中的参数等信息封装到environ(一个字典)中。
本文详细讲解了Python中requests库的用法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
这篇文章主要介绍了Python的functools模块使用及说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
这篇文章主要和大家分享如何调用百度的接口实现图片的文字识别。整体是用Python实现,所需要使用的第三方库包括aip、PIL、keyboard、pyinstaller,需要的可以参考一下
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008