算法的数学描述图解

实例

用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. binlog2sql之MySQL数据闪回实践

    DBA或开发人员,有时会误删或者误更新数据,如果是线上环境并且影响较大,就需要能快速回滚.传统恢复方法是利用备份重搭实例,再应用去除错误sql后的binlog来恢复数据.此法费时费力,甚至需要停机维护 ...

  2. C++ Primer 笔记——枚举类型

    1.和类一样,每个枚举类型定义了一种新的类型.枚举属于字面值常量类型. 2.C++包含两种枚举:限定作用域的和不限定作用域的.C++11新标准引入了限定作用域的枚举类型. }; // 限定作用域的枚举 ...

  3. python 利用split读取文本文件中每一行的数字并保存至相应文件夹

    import re from numpy import * def getStr(file_path,file_path1): fp = open(file_path, 'r') op = open( ...

  4. JQuery之左侧菜单

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 一脸懵逼学习Hive的元数据库Mysql方式安装配置

    1:要想学习Hive必须将Hadoop启动起来,因为Hive本身没有自己的数据管理功能,全是依赖外部系统,包括分析也是依赖MapReduce: 2:七个节点跑HA集群模式的: 第一步:必须先将Zook ...

  6. jmeter4.x centos7部署笔记

    1. jmeter依赖 java8或以上版本 安装 java : 参考  https://tecadmin.net/install-java-8-on-centos-rhel-and-fedora/ ...

  7. Aho-Corasick算法实现(简单关键字过滤)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...

  8. element-ui MessageBox的bug

    通过 use引用messageBox有bug Vue.use(MessageBox) 页面一开始会有一个弹窗,内容空白 Vue.component(MessageBox.name, MessageBo ...

  9. 【BZOJ4715】囚人的旋律

    题解: 思考了很久这个图的特点没有发现 看了题解瞬间醒悟原来要在序列上做 还原出这张图显然是O(N^2)可以做的 然后其实就比较简单了 首先为了满足独立集,我们需要保证所取元素递增 为了满足覆盖集,我 ...

  10. mysql中trim()函数的用法

    去除左空格函数: LTRIM(str) mysql> SELECT LTRIM(' barbar'); -> 'barbar' 去除右空格函数: RTRIM(str) mysql> ...