欧拉方法python代码实现

如有不对,请批评指正!

1. 显式Euler和隐式Euler

在这里插入图片描述

2. 改进的Euler

在这里插入图片描述

3. 龙格-库塔(Runge-Kutta)

在这里插入图片描述

4. 举个栗子

在这里插入图片描述
代码仅为了说明计算过程,并未进行优化!

import math
import matplotlib.pyplot as plt

y_e = []  # 显式
y_i = []  # 隐式
y_pro = []  # 改进
y_p = []  # 改进
y_rk = []  # 龙格-库塔
y_c = []  # 解析解
x = []
h = 0.1  # 步长
num = 20


def fun(x0, y0):
    return -2 * x0 / y0 + y0


def Euler():
    print('{:>3s}{:>8s}{:>9s}{:>12s}{:>9s}{:>9s}'.format('x', 'y_e', 'y_i', 'y_pro', 'y_rk', 'y_c'))
    for i in range(num):
        # 显示Euler
        y_e.append(round(y_e[i] + h * fun(x[i], y_e[i]), 5))

        # 隐式Euler
        y_i1 = y_i[i] + h * fun(x[i], y_i[i])
        y_i2 = y_i[i] + h * fun(x[i + 1], y_i1)
        while abs(y_i2 - y_i1) > 1e-6:
            y_i1 = y_i2
            y_i2 = y_i[i] + h * fun(x[i + 1], y_i1)
        y_i.append(y_i2)

        # 改进Euler
        y_p.append(y_pro[i] + h * fun(x[i], y_pro[i]))
        y_pro.append(round(y_pro[i] + h / 2 * (fun(x[i], y_pro[i]) + fun(x[i + 1], y_p[i])), 5))

        # 龙格-库塔
        k1 = fun(x[i], y_rk[i])
        k2 = fun(x[i] + h / 2, y_rk[i] + h / 2 * k1)
        k3 = fun(x[i] + h / 2, y_rk[i] + h / 2 * k2)
        k4 = fun(x[i] + h, y_rk[i] + h * k3)
        y_rk.append(round(y_rk[i] + h / 6 * (k1 + 2 * k2 + 2 * k3 + k4), 5))

        # 解析解
        y_c.append(round(math.sqrt(1 + 2 * x[i + 1]), 5))

        print('{}{:>10f}{:>10f}{:>10f}{:>10f}{:>10f}\n'.format(
            round(x[i + 1], 1),
            y_e[i + 1],
            y_i[i + 1],
            y_pro[i + 1],
            y_rk[i + 1],
            y_c[i + 1]))

    plt.plot(x, y_e, label='Euler_Explicit', color='r', marker='+')
    plt.plot(x, y_i, label='Euler_Implicit', color='c', marker='>')
    plt.plot(x, y_pro, label='Euler_Improvement', color='b', marker='3')
    plt.plot(x, y_rk, label='Euler_RungeKutta', color='m', marker='d')
    plt.plot(x, y_c, label='Analytical solution', color='g', marker='o')
    plt.legend()
    plt.show()


if __name__ == '__main__':
    # 初始值
    x.append(0.0)
    y_e.append(1)  # 显式
    y_i.append(1)  # 隐式
    y_pro.append(1)  # 改进
    y_rk.append(1)  # 龙格-库塔
    y_c.append(1)  # 解析解
    for i in range(num):
        x.append(round(x[0] + (i + 1) * h, 1))
    Euler()

5. 运行结果

num = 6时
在这里插入图片描述

在这里插入图片描述
num = 20
在这里插入图片描述

参考:代码实现:显示欧拉、隐式欧拉、改进欧拉、龙格库塔法

如有不对,敬请批评指正!
(转载文章请注明作者和出处)

  • 6
    点赞
  • 71
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
隐式欧拉法的Python实现如下所示: ```python 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 euler") return u ``` 这段代码中,我们定义了一个函数`implicit_euler`,它接受参数`rangee`表示求解的范围,`h`表示步长,`fun`表示微分方程的函数,`x0`和`y0`表示初始条件。然后我们初始化了一些变量,包括时间步长`step`,自变量`x`和因变量`u`。接下来,我们使用隐式欧拉法的迭代公式进行计算,最后使用`plt.plot`函数绘制出结果,并返回因变量的数组。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Python数值求解微分方程(欧拉法,隐式欧拉)](https://blog.csdn.net/m0_59485658/article/details/126310098)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.268^v1^koosearch"}}] [.reference_item style="max-width: 50%"] - *2* [常微分方程之欧拉法、隐式欧拉法、改进欧拉法以及梯形法的原理](https://blog.csdn.net/liuqihang11/article/details/122203027)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.268^v1^koosearch"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值