算法的数学描述图解

实例

用Euler算法求解初值问题
$$ \frac{dy}{dx}=y+\frac{2x}{y^2}$$
初始条件$y(0)=1$,自变量的取值范围$x \in [0, 2]$

算法Python3代码求解

```
# 导入包
import numpy as np
import matplotlib.pyplot as plt
# 定义求解函数 y_dot = y + 2*x/(y*y)
def fx(y, x):
return y + 2*x/(y*y)
# 算法定义
def ode_euler(f, y0, tf, h):
"""
Solve and ODE using Euler method.
Solve the ODE y_dot = f(y, t)
Parameters
------------
:param f: function
Function describing the ODE
:param y0: array_like
Initial conditions.
:param tf: float
Final time.
:param h: float
Time step
:return:
y : array_like
Solution to the ODE.
t : array_like
Time vector.
"""

y0 = np.array(y0)
ts = np.arange(0, tf + h, h)
y = np.empty((ts.size, y0.size))
y[0, :] = y0
for t, i in zip(ts[1:], range(ts.size - 1)):
y[i + 1, :] = y[i, :] + h * f(y[i, :], t)
return y, ts

实例应用案例

def newton_cooling_example():

print('Solving Newton Cooling ODE...')

y, ts = ode_euler(fx, 1, 2, 0.01)

print('Done.')

plt.figure()

plt.plot(ts, y)

plt.xlabel('time [s]')

plt.title('Solution to the Newton cooling equation')

plt.show()

<h2>代码中的部分函数理解</h2>
<h3>numpy.array</h3>
`numpy.array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0)`
[参考numpy.array]( https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.array.html)
output:**创建一个array**,返回类型为ndarray
<strong>实例</strong>

np.array([1, 2, 3.0]) # array([1., 2., 3.])

np.array([[1, 2], [3, 4]]) # array([[1, 2], [3, 4]])

np.array([1, 2, 3], dtype=complex) # array([1.+0.j, 2.+0.j, 3.+0.j])


<h3>numpy.arange</h3>
[参考numpy.arange](https://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html)
`numpy.arange([start, ]stop, [step, ]dtype=None)`
**作用**:在给定间隔内返回均匀间隔的值。
值在半开区间[start, stop)内生成(换句话说,包括开始但不包括终止)。返回的是ndarray而不是列表。
np.arange()函数返回一个有终点和起点的固定步长的排列,如[1,2,3,4,5],起点是1,终点是5,步长为1。
参数个数情况: np.arange()函数分为一个参数,两个参数,三个参数三种情况 : 1. 一个参数时,参数值为终点,起点取默认值0,步长取默认值1。
2. 两个参数时,第一个参数为起点,第二个参数为终点,步长取默认值1。
3. 三个参数时,第一个参数为起点,第二个参数为终点,第三个参数为步长。其中步长支持小数。 **案例**

np.arange(3,7) # array([3, 4, 5, 6])

np.arange(3,7,2) # array([3, 5])

<h3>numpy.ma.size</h3>
`numpy.ma.size(obj, axis=None)`
[参考](https://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.ma.size.html#numpy.ma.size)
**案例**

a = np.array([[1,2,3],[4,5,6]])

np.size(a) # 6

np.size(a,1) # 3

np.size(a,0) # 2

<h3>numpy.empty</h3>
[参考](https://docs.scipy.org/doc/numpy/reference/generated/numpy.empty.html)
`numpy.empty(shape, dtype=float, order='C')`
**shape** : int or tuple of int Shape of the empty array, e.g., (2, 3) or 2.
**out** : ndarray
**案例**

np.empty([2, 2])

结果

array([[ -9.74499359e+001, 6.69583040e-309],

[ 2.13182611e-314, 3.06959433e-309]]) #random

np.empty([2, 2], dtype=int)

结果

array([[-1073741821, -1067949133],

[ 496041986, 19249760]]) #random

数学——Euler方法求解微分方程详解(python3)的更多相关文章

  1. 详解 Python3 正则表达式(四)

    上一篇:详解 Python3 正则表达式(三) 本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些注明和修改 ^_^ 更多强大的功能 ...

  2. 详解 Python3 正则表达式(三)

    上一篇:详解 Python3 正则表达式(二) 本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些批注和修改 ^_^ 模块级别的函数 ...

  3. 详解 Python3 正则表达式(二)

    上一篇:详解 Python3 正则表达式(一) 本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些批注和修改 ^_^ 使用正则表达式 ...

  4. 详解 Python3 正则表达式(一)

    本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些批注和修改 ^_^ 正则表达式介绍 正则表达式(Regular expressio ...

  5. .NET Excel导出方法及其常见问题详解

    摘要:.NET Excel导出方法及其常见问题详解. 一.Excel导出的实现方法 在.net 程序开发中,对于Excel文件的导出我们一共有三种导出方式: 利用文件输出流进行读写操作 这种方式的导出 ...

  6. (转)详解Python3 中hasattr()、getattr()、setattr()、delattr()函数及示例代码数

    原文:https://www.jb51.net/article/138363.htm hasattr()函数 hasattr()函数用于判断是否包含对应的属性 语法: hasattr(object,n ...

  7. 详解 Python3 正则表达式(五)

    上一篇:详解 Python3 正则表达式(四) 本文翻译自:https://docs.python.org/3.4/howto/regex.html 博主对此做了一些注明和修改 ^_^ 非捕获组和命名 ...

  8. cloudemanager安装时出现failed to receive heartbeat from agent问题解决方法(图文详解)

    不多说,直接上干货! 安装cdh5到最后报如下错误: 安装失败,无法接受agent发出的检测信号. 确保主机名称正确 确保端口7182可在cloudera manager server上访问(检查防火 ...

  9. cloudemanager安装时出现8475 MainThread agent ERROR Heartbeating to 192.168.30.1:7182 failed问题解决方法(图文详解)

    不多说,直接上干货!   问题详情 解决这个问题简单的,是因为有进程占用了.比如 # ps aux | grep super root ? Ss : : /opt/cm-/lib64/cmf/agen ...

随机推荐

  1. CentOS下将Python的版本升级为3.x

    本文主要介绍在Linux(CentOS)下将Python的版本升级为3.x的方法 众所周知,在2020年python官方将不再支持2.7版本的python,所以使用3.x版本的python是必要的,但 ...

  2. 步步为营103-ZTree 二级联动

    1:添加引用 <%--流程类别多选--引用js和css文件--开始--%> <link rel="stylesheet" href="../css/zT ...

  3. mysql集群7.4.1

    一:mysql集群原理: 1 mysql集群分为三个节点: 1.1 控制节点:本身不提供服务只是控制整个集群的开启与关闭 1.2 数据节点:真正提供数据库的存储,并和其他数据节点关联用 1.3 sql ...

  4. 令人疑惑的 std::remove 算法

    摘自<Effective STL>第32条 remove的声明: template<class ForwardIterator, class T> ForwardIterato ...

  5. 【转】利用 selenium 的 webdrive 驱动 headless chrome

    1.参考 使用 headless chrome进行测试 2.概念 Headless模式解决了什么问题: 自动化工具例如 selenium 利用有头浏览器进行测试,面临效率和稳定性的影响,所以出现了 H ...

  6. Note for "Some Remarks on Writing Mathematical Proofs"

    John M. Lee is a famous mathematician, who bears the reputation of writing the classical book " ...

  7. nginx 设置自签名证书以及设置网址http强制转https访问

    自签名证书可以在自己的内网环境或者非对外环境使用,保证通信安装 1.生产证书 直接使用脚本生产: 中途会提示书如1次域名和4次密码,把一下文件保存为sh文件,赋予x权限后 直接执行,根据提示输入. # ...

  8. C# 之 数字格式化

    格式规范的完整形式:{index [,width][:formatstring]} index是此格式程序引用的格式字符串之后的参数,从零开始计数:width(可选) 是要设置格式的字段的宽度,wid ...

  9. js防止安卓手机软键盘弹出挤压页面导致变形的方法

    5防止安卓手机软键盘弹出挤压页面导致变形的方法 输入框定位在底部,手机端打开,输入框聚焦后软键盘打开为什么会瞬间自动关闭呢? 先看看问题: 1.原来是这样的: 2.在苹果手机里面是正常的: 3.到了安 ...

  10. Codeforces Gym100543G Virus synthesis 字符串 回文自动机 动态规划

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF-100543G.html 题目传送门 - CF-Gym100543G 题意 你可以对一个字符串进行以下两种操 ...