Python数值求解微分方程方法(欧拉法,隐式欧拉)

作者:Chandler_river 时间:2023-06-29 10:45:29 

不说什么,先上代码

这里先求解形如Python数值求解微分方程方法(欧拉法,隐式欧拉)的微分方程

1.欧拉法

def eluer(rangee,h,fun,x0,y0):
   step = int(rangee/h)
   x = [x0] + [h * i for i in range(step)]
   u = [y0] + [0     for i in range(step)]
   for i in range(step):
       u[i+1] = u[i] + h * fun(x[i],u[i])
   plt.plot(x,u,label = "eluer")
   return u

2.隐式欧拉法

def implicit_euler(rangee,h,fun,x0,y0):
   step = int(rangee/h)
   x = [x0] + [h * i for i in range(step)]
   u = [y0] + [0     for i in range(step)]
   v = ["null"] + [0 for i in range(step)]
   for i in range(step):
           v[i+1] = u[i] + h * fun(x[i],u[i])
           u[i+1] = u[i] + h/2 * (fun(x[i],u[i]) + fun(x[i],v[i+1]))
   plt.plot(x,u,label = "implicit eluer")
   return u

3.三阶runge-kutta法

def order_3_runge_kutta(rangee,h,fun,x0,y0):
   step = int(rangee/h)
   k1,k2,k3 = [[0 for i in range(step)] for i in range(3)]
   x = [x0] + [h * i for i in range(step)]
   y = [y0] + [0     for i in range(step)]
   for i in range(step):
       k1[i] = fun(x[i],y[i])
       k2[i] = fun(x[i]+0.5*h,y[i]+0.5*h*k1[i])
       k3[i] = fun(x[i]+0.5*h,y[i]+2*h*k2[i]-h*k1[i])
       y[i+1] = y[i] + 1/6 * h * (k1[i]+4*k2[i]+k3[i])
   plt.plot(x,y,label = "order_3_runge_kutta")
   return y

4.四阶runge-kutta法

def order_4_runge_kutta(rangee,h,fun,x0,y0):
   step = int(rangee/h)
   k1,k2,k3,k4 = [[0 for i in range(step)] for i in range(4)]
   x = [x0] + [h * i for i in range(step)]
   y = [y0] + [0     for i in range(step)]
   for i in range(step):
       k1[i] = fun(x[i],y[i])
       k2[i] = fun(x[i]+0.5*h,y[i]+0.5*h*k1[i])
       k3[i] = fun(x[i]+0.5*h,y[i]+0.5*h*k2[i])
       k4[i] = fun(x[i]+h,y[i]+h*k3[i])
       y[i+1] = y[i] + 1/6 * h * (k1[i]+2*k2[i]+2*k3[i]+k4[i])
   plt.plot(x,y,label = "order_4_runge_kutta")
   return y

5.上图

Python数值求解微分方程方法(欧拉法,隐式欧拉)

 当然,想要成功操作,得加上这个

rangee = 1
fun = lambda x,y:y-2*x/y

implicit_euler(rangee,0.0001,fun,0,1)
order_4_runge_kutta(rangee,0.0001,fun,0,1)
order_3_runge_kutta(rangee,0.0001,fun,0,1)
eluer(rangee,0.0001,fun,0,1)
plt.legend()
plt.show()

来源:https://blog.csdn.net/Chandler_river/article/details/124295877

标签:Python,数值,求解
0
投稿

猜你喜欢

  • mac系统OS X10.10版本安装最新5.7.9mysql的方法

    2024-01-14 10:05:38
  • PyQt5实现画布小程序

    2022-03-02 07:37:46
  • Django实现发送邮件功能

    2021-05-13 10:25:44
  • Python面向对象之内置函数相关知识总结

    2022-06-05 10:30:24
  • 从品牌网站看交互设计

    2009-08-18 12:39:00
  • OpenCV-PS扩散毛玻璃效果的实现代码

    2022-03-17 22:45:52
  • python实现某考试系统生成word试卷

    2022-05-20 18:29:09
  • python实现人像动漫化的示例代码

    2021-08-16 06:36:17
  • Thinkphp5文件包含漏洞解析

    2023-07-01 19:42:51
  • pygame实现贪吃蛇游戏

    2021-09-23 00:29:21
  • Python干货实战之逆向登录世界上最大的游戏平台Stream

    2023-05-13 11:25:19
  • Python 设计模式中的创建型建造者模式

    2021-05-23 02:41:50
  • Tensorflow自定义模型与训练超详细讲解

    2023-09-19 10:18:00
  • python requests实现上传excel数据流

    2022-07-13 16:35:30
  • flask框架自定义过滤器示例【markdown文件读取和展示功能】

    2023-03-07 19:22:27
  • 高性能PHP框架Symfony2经典入门教程

    2023-11-16 02:39:23
  • Python eval()函数和ast.literal_eval()的区别你知道吗

    2022-06-26 14:37:55
  • Pandas如何将表格的前几行生成html实战案例

    2021-06-05 12:01:27
  • Python使用文件锁实现进程间同步功能【基于fcntl模块】

    2022-07-04 17:45:15
  • 在网页中实现细线边框的两种方法

    2011-06-14 09:47:26
  • asp之家 网络编程 m.aspxhome.com