链接: 智机+视觉+机器学习必要的工程数学(应用数学)知识练习与总结 – 线性代数+概率+统计.
注:.ipynb 笔记总结 转换成 .md 文件,图片上传麻烦,可以根据目录运行验证过的代码。
命令:打开 jupyter notebook:
ipython notebook
jupyter notebook
jupyter notebook 编辑 markdown 文档,鼠标点击 input 栏坐车,按下键盘 m 键,即可从 python3 转化成 markdown 编辑。
jupyter notebook 快捷键查看,点击页面空白区域,按下键盘 h 键,即可弹出快捷键窗口。
开启 jupyter notebook 的代码提示
pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user
pip install --user jupyter_nbextensions_configurator
jupyter nbextensions_configurator enable --user
Nbextensions勾选Hinteriand
- 分数,有理数
- 无理数
- 美化输出
- 代数运算1
- 展开和分解
- 求和计算
- 乘积计算
- 一元方程(应用题)
- 二元方程(应用题)
- 代数运算2
- 多元函数的代数表达式运算
- 字符串转sympy表达式
# 分数转有理数
1/3
0.3333333333333333
# 导入sympy库,显示有理数 rational number
from sympy import *
a = Rational(1, 3)
a
1 3 \displaystyle \frac{1}{3} 31
# 显示有理数变量类型
type(a)
sympy.core.numbers.Rational
# 对比显示 python 变量类型
b = 1 / 3
type(b)
float
# 无理数 pi,sympy库变量
pi
π \displaystyle \pi π
# 无理数 pi 类型
type(pi)
sympy.core.numbers.Pi
# 自然数 e,math库变量
import math
math.e
2.718281828459045
# 自然数 E,sympy库变量
E
e \displaystyle e e
# 显示符号数值
N(E)
2.71828182845905 \displaystyle 2.71828182845905 2.71828182845905
# 显示符号数值,限定显示位数
N(E, 5)
2.7183 \displaystyle 2.7183 2.7183
# 开启 sympy 的美化输出(默认开启)
c
# 输出表达式
pi ** 2
π 2 \displaystyle \pi^{2} π2
# 关闭 sympy 的美化输出(默认开启)
init_printing(pretty_print=False)
# 输出表达式
pi ** 2
pi**2
# 再次开启
init_printing(pretty_print=True)
# 1. 声明一个代数符号
x = Symbol('x')
# 2. 使用代数符号编写表达式
x * 2 + x + x * 3
6 x \displaystyle 6 x 6x
# 3. 声明另一个代数符号
y = Symbol('y')
# 4. 两个代数符号编写二元方程
x + y + y ** 2 + 2 * y + 3 * x
4 x + y 2 + 3 y \displaystyle 4 x + y^{2} + 3 y 4x+y2+3y
# 5. 一次声明多个代数符号
a, b, c = symbols('a, b, c')
# 6. 编写表达式并赋值给变量
f = a ** 2 + a + b + b ** 3
# 7. 输出变量
f
a 2 + a + b 3 + b \displaystyle a^{2} + a + b^{3} + b a2+a+b3+b
# 8. 在表达式中,用数字给代数符号赋值,求解表达式
f.subs({a: 1, b: 2}) # subs: substitude, **kwargs 形式
12 \displaystyle 12 12
# 9. 8的另一种赋值法
f.subs([(a, 1), (b, 2)])
12 \displaystyle 12 12
# 1. 展开多项式
# 声明多个变量
x, y = symbols('x, y')
# 编写表达式,并赋值
f = (x + y + 1) ** 2
# 输出表达式
f
( x + y + 1 ) 2 \displaystyle \left(x + y + 1\right)^{2} (x+y+1)2
# 展开表达式 f
expand(f)
x 2 + 2 x y + 2 x + y 2 + 2 y + 1 \displaystyle x^{2} + 2 x y + 2 x + y^{2} + 2 y + 1 x2+2xy+2x+y2+2y+1
# 展开表达式 (x + y + 1) ** 3
expand((x + y + 1) ** 3)
x 3 + 3 x 2 y + 3 x 2 + 3 x y 2 + 6 x y + 3 x + y 3 + 3 y 2 + 3 y + 1 \displaystyle x^{3} + 3 x^{2} y + 3 x^{2} + 3 x y^{2} + 6 x y + 3 x + y^{3} + 3 y^{2} + 3 y + 1 x3+3x2y+3x2+3xy2+6xy+3x+y3+3y2+3y+1
# 3. 化简表达式 (x + x * y) / x
simplify((x + x * y) / x)
y + 1 \displaystyle y + 1 y+1
# 4. 使用 sympy.plot 绘制图像
# sin(x)
plot(sin(x))
[外链图片转存中…(img-0NHyImBB-1605604520340)]
# cons(x)
plot(cos(x))
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PahHL65s-1605604520341)(output_35_0.png)]
# 2 * sin(x) * (sin(x) + cos(x))
plot(2 * sin(x) * (sin(x) + cos(x)))
[外链图片转存中…(img-rlS0qqQt-1605604520342)]
# 2. 三角函数
# 表达式
tri_f = 2 * sin(x) * (sin(x) + cos(x))
tri_f
2 ( sin ( x ) + cos ( x ) ) sin ( x ) \displaystyle 2 \left(\sin{\left(x \right)} + \cos{\left(x \right)}\right) \sin{\left(x \right)} 2(sin(x)+cos(x))sin(x)
# 化简三角函数
simplify(tri_f)
− 2 cos ( 2 x + π 4 ) + 1 \displaystyle - \sqrt{2} \cos{\left(2 x + \frac{\pi}{4} \right)} + 1 −2cos(2x+4π)+1
公式: 1 1 2 + 2 ⋅ 1 + 1 2 2 + 2 ⋅ 2 + . . . + 1 1 0 2 + 2 ⋅ 10 \frac{1}{1^2+2\cdot1}+\frac{1}{2^2+2\cdot2}+...+\frac{1}{10^2+2\cdot10} 12+2⋅11+22+2⋅21+...+102+2⋅101
# 1.写成求和公式形式 sigma()
sum_f = Sum(1 / (x ** 2 + 2 * x), (x, 1, 10)) # arg1: function_exp, arg2: dummy_var, start, end
sum_f
∑ x = 1 10 1 x 2 + 2 x \displaystyle \sum_{x=1}^{10} \frac{1}{x^{2} + 2 x} x=1∑10x2+2x1
# 2. evalue function, N() == .evalf(), 对函数求值,保留10位
N(sum_f, 10)
0.6628787879 \displaystyle 0.6628787879 0.6628787879
# evalue function recursively
sum_f.doit()
175 264 \displaystyle \frac{175}{264} 264175
公式: 1 1 2 + 2 ⋅ 1 × 1 2 2 + 2 ⋅ 2 × . . . × 1 1 0 2 + 2 ⋅ 10 \frac{1}{1^2+2\cdot1}\times\frac{1}{2^2+2\cdot2}\times...\times\frac{1}{10^2+2\cdot10} 12+2⋅11×22+2⋅21×...×102+2⋅101
# 1. 写成乘积公式形式
prod_f = Product(1 / (x ** 2 + 2 * x), (x, 1, 20))
prod_f
∏ x = 1 20 1 x 2 + 2 x \displaystyle \prod_{x=1}^{20} \frac{1}{x^{2} + 2 x} x=1∏20x2+2x1
# 2. 对函数求值,N()
N(prod_f, 20)
7.3137276902643132326 ⋅ 1 0 − 40 \displaystyle 7.3137276902643132326 \cdot 10^{-40} 7.3137276902643132326⋅10−40
# 对函数求值 doit
prod_f.doit()
1 1367291813901073295331429030297600000000 \displaystyle \frac{1}{1367291813901073295331429030297600000000} 13672918139010732953314290302976000000001
应用题:敌军在离我军8千米的驻地逃跑,时间是早晨4点,我军于5点出发以每小时10千米的速度追击, 结果在7点追上.求敌军逃跑时的速度是多少
分析:敌军的路程(速度 X 时间)+ 8 = 我军的路程(速度 X 时间)
方法:建模后使用 solve 求解
# 1. 建模,建立表达式,假设敌军速度为 x
expr = x * 3 + 8 - (10 * 2)
expr
3 x − 12 \displaystyle 3 x - 12 3x−12
# 2. solve 求解
solve(expr, x)
[ 4 ] \displaystyle \left[ 4\right] [4]
某城市现有人口42万人.计划一年后城镇人口增加0.8%,农村人中增加1.1%,这样全市人口得增加1%,求这个城市现有城镇人口和农村人口分别是多少人?
分析:增加后城镇人口 + 增加后农村人口 = 增加后城市人口,
假设原城镇人口x,原农村人口y,
增加后城镇人口x * (100 + 0.8) / 100,增加后农村人口y * (100 + 1.1) / 100
建模表达式
# 1. 建立数学模型表达式
# 数学模型1:增之前模型
expr_1 = x + y - 42
expr_1
x + y − 42 \displaystyle x + y - 42 x+y−42
# 数学模型2:增长后模型
expr_2 = x * (100 + 0.8) / 100 + y * (100 + 1.1) / 100 - 42 * (100 + 1) / 100
expr_2
1.008 x + 1.011 y − 42.42 \displaystyle 1.008 x + 1.011 y - 42.42 1.008x+1.011y−42.42
# 2. solve 求解
solve([expr_1, expr_2], [x, y])
{ x : 14.0 , y : 28.0 } \displaystyle \left\{ x : 14.0, \ y : 28.0\right\} {x:14.0, y:28.0}
# 第二种数学建模方式,使用等式
# 等式1:增加前
eq_1 = Eq(x + y, 42)
eq_1
x + y = 42 \displaystyle x + y = 42 x+y=42
# 等式2:增加后
eq_2 = Eq(x * (100 + 0.8) / 100 + y * (100 + 1.1) / 100, 42 * (100 + 1) / 100)
eq_2
1.008 x + 1.011 y = 42.42 \displaystyle 1.008 x + 1.011 y = 42.42 1.008x+1.011y=42.42
# solve 求解
solve([eq_1, eq_2], [x, y])
{ x : 14.0 , y : 28.0 } \displaystyle \left\{ x : 14.0, \ y : 28.0\right\} {x:14.0, y:28.0}
公式 : x 2 + 2 x + 1 x^{2}+2x+1 x2+2x+1
需求1:令 x = 2
需求2:令 x = y + 1
# 1. 建立表达式
expr = x ** 2 + 2 * x + 1
expr
x 2 + 2 x + 1 \displaystyle x^{2} + 2 x + 1 x2+2x+1
# 2. solve 求解, substitude x with 2
expr.subs(x, 2)
9 \displaystyle 9 9
# 3. solve 求解, substitude x with y + 1
expr.subs(x, y + 1)
2 y + ( y + 1 ) 2 + 3 \displaystyle 2 y + \left(y + 1\right)^{2} + 3 2y+(y+1)2+3
# 1. 建立一个二元三次方程
expr = x ** 3 + 3 * x * y + 2 * y + 1
expr
x 3 + 3 x y + 2 y + 1 \displaystyle x^{3} + 3 x y + 2 y + 1 x3+3xy+2y+1
# 2. solve 求解,使用 subs 用值替代对应的代数符号
expr.subs({x: 1, y: 2})
12 \displaystyle 12 12
# 1. 编写字符串表达式
expr_str = "x ** 2 + 3 * x + 2"
expr_str
'x ** 2 + 3 * x + 2'
# 2. 字符串表达式转表达式,simplify
expr = simplify(expr_str)
expr
x 2 + 3 x + 2 \displaystyle x^{2} + 3 x + 2 x2+3x+2
# 3. 计算表达式,给x赋值1
expr.subs(x, 1)
6 \displaystyle 6 6
- 常见函数
- 三角函数的参数
- 连续函数
- 非连续函数
- 无穷
- 无穷数列求解的计算过程
函数描述的是映射关系, 给定一个数集A,对A施加对应法则f,记作f(A),得到另一数集B,也就是
B=f(A),那么这个关系式就叫函数关系式,简称函数。
# 1. 幂函数
# 声明代数符号
x = Symbol('x')
# 使用代数符号建立表达式
expr = x ** 2
# 输出表达式
expr
x 2 \displaystyle x^{2} x2
# 绘制表达式
plot(expr)
[外链图片转存中…(img-qOwziZp0-1605604520346)]
# 绘制表达式, x值范围-5,7
plot(expr, (x, -5, 7))
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oKLEgk5C-1605604520348)(output_74_0.png)]
# 绘制表达式,x变量范围0-10,y轴范围-3,3
plot(exp(x), (x, 0, 10), ylim=(-3, 3))
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KNYDDNfI-1605604520348)(output_75_0.png)]
# 2. 三角函数
# sin(x)
sin(x)
sin ( x ) \displaystyle \sin{\left(x \right)} sin(x)
# 绘制 sin(x)
plot(sin(x))
[外链图片转存中…(img-XvYuSEYW-1605604520349)]
# 3. 自然数为底的幂函数
exp(x)
e x \displaystyle e^{x} ex
# 绘制自然数为底的幂函数
plot(exp(x))
[外链图片转存中…(img-L6dLFdUe-1605604520349)]
# 4. 以自然数为底的指数函数
log(x)
log ( x ) \displaystyle \log{\left(x \right)} log(x)
# 绘制
plot(log(x))
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Q5I2R0TM-1605604520350)(output_81_0.png)]
# 计算 log(2) 保留7位,N evalue
N(log(2), 7)
0.6931472 \displaystyle 0.6931472 0.6931472
[外链图片转存中…(img-YRV6S7FD-1605604520351)]
# 计算 sin(90度),
sin(pi / 2)
1 \displaystyle 1 1
# sin的反函数
asin(1)
π 2 \displaystyle \frac{\pi}{2} 2π
# cos的反函数
acos(0)
π 2 \displaystyle \frac{\pi}{2} 2π
连续函数就是在函数的任意一个位置,无论怎么放大,这条线都不会断掉.
# 创建连续函数 y = x ** 2 -1(等式),这个方法无法使用 plot
eq_expr = Eq(x ** 2 -1, y)
eq_expr
x 2 − 1 = y \displaystyle x^{2} - 1 = y x2−1=y
# 创建连续函数 y = x ** 2 - 1(表达式),可以使用 plot
y = x ** 2 -1
y
x 2 − 1 \displaystyle x^{2} - 1 x2−1
# 绘制表达式 y,x 值在 -2, 3 之间
plot(y, (x, -2, 3))
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gBff2RFb-1605604520352)(output_90_0.png)]
函数在某点是有间断的
间断点分为三种
# 定义一个非连续函数 y = (x ** 2 -1) / (x - 1)
y = (x ** 2 -1) / (x -1)
y
x 2 − 1 x − 1 \displaystyle \frac{x^{2} - 1}{x - 1} x−1x2−1
# 绘制函数,x值范围 -2, 3
plot(y, (x, -2, 3))
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IkwYJ4ok-1605604520353)(output_93_0.png)]
# 求解 x = 1 时的值 NaN: not a number
y.subs(x, 1)
NaN \displaystyle \text{NaN} NaN
连续是微积分的基础概念, 无穷是微积分的核心概念
# 1. 无穷符号
oo
∞ \displaystyle \infty ∞
# 2. math 库无穷
math.inf
i n f \displaystyle inf inf
# 3. 无穷加法
math.inf + 10000
i n f \displaystyle inf inf
# 4. 无穷加法
oo + 1
∞ \displaystyle \infty ∞
# 5. 无穷减法 math
math.inf - math.inf
n a n \displaystyle nan nan
# 6 无穷减法, sympy
oo - oo
NaN \displaystyle \text{NaN} NaN
热身:找规律
数列:1, 2, 3, 4, 5 … 求解数列通项公式
解:定义 Δ \Delta Δ 为数列顺序后一位与前一位的差,则:
数列:1, 2, 3, 4, 5 … 的 Δ \Delta Δ 数列为:
Δ \Delta Δ:1, 1, 1, 1, 1 …,则:
数列第N项通项公式: N = a 1 × n 1 + a 0 N=a_{1}\times n^{1}+a_{0} N=a1×n1+a0,其中N表示第n项的值,n表示项索引
现在求解系数 a 1 a_{1} a1与 a 0 a_{0} a0:
对于第一项: 1 = a 1 × 1 1 + a 0 1=a_{1}\times 1^{1}+a_{0} 1=a1×11+a0
对于第二项: 2 = a 1 × 2 1 + a 0 2=a_{1}\times 2^{1}+a_{0} 2=a1×21+a0
求解得出: a 1 = 1 , a 0 = 0 a_{1}=1, a_{0}=0 a1=1,a0=0,带入通项公式,得: N = n N=n N=n
求数列通项公式的方法
明确n的指数
Δ \Delta Δ 一次就变成相同的数列, 最高指数就是1,通项公式为: N = a 1 × n 1 + a 0 N=a_{1}\times n^{1}+a_{0} N=a1×n1+a0
Δ Δ \Delta\Delta ΔΔ 2次就变成相同的数列, 最高指数就是2,通项公式为: N = a 2 × n 2 + a 1 1 × n 1 + a 0 N=a_{2}\times n^{2}+a_1{1}\times n^{1}+a_{0} N=a2×n2+a11×n1+a0
Δ Δ Δ \Delta\Delta\Delta ΔΔΔ 3次就变成相同的数列, 最高指数就是,通项公式为: N = a 3 × n 3 + a 2 2 × n 2 + a 1 1 × n 1 + a 0 N=a_{3}\times n^{3}+a_2{2}\times n^{2}+a_1{1}\times n^{1}+a_{0} N=a3×n3+a22×n2+a11×n1+a0
以此类推,然后通过已知项求解通项公式的系数,然后求解第n项的值。
练习1
求数列的通项公式:2, 4, 7, 11, 16 …
解:
Δ = 2 , 3 , 4 , 5... \Delta=2, 3, 4, 5 ... Δ=2,3,4,5...
Δ Δ = 1 , 1 , 1 , 1 , . . . \Delta\Delta=1, 1, 1, 1, ... ΔΔ=1,1,1,1,... 或相等常量值
通项公式最大指数为2,形式为: N = a 2 × n 2 + a 1 × n 1 + a 0 N=a_{2}\times n^{2}+a_{1}\times n^{1}+a_{0} N=a2×n2+a1×n1+a0
使用 sympy 求解系数 a 2 , a 1 , a 0 a_{2}, a_{1}, a_{0} a2,a1,a0
# 导入 sympy
from sympy import *
# sympy 求解系数a2,a1,a0,需要3个等式
# 声明代数符号 a2,a1,a0
a2, a1, a0 = symbols("a2, a1, a0")
# 等式1
eq_1 = Eq(a2 * 1 ** 2 + a1 * 1 ** 1 + a0, 2)
# 等式2
eq_2 = Eq(a2 * 2 ** 2 + a1 * 2 ** 1 + a0, 4)
# 等式3
eq_3 = Eq(a2 * 3 ** 2 + a1 * 3 ** 1 + a0, 7)
# 使用 solve 求解等式
solve([eq_1, eq_2, eq_3])
{ a 0 : 1 , a 1 : 1 2 , a 2 : 1 2 } \displaystyle \left\{ a_{0} : 1, \ a_{1} : \frac{1}{2}, \ a_{2} : \frac{1}{2}\right\} {a0:1, a1:21, a2:21}
练习2
求数列的通项公式:2, 9, 28, 65, 126 …
解:
Δ = 7 , 19 , 37 , 61... \Delta=7, 19, 37, 61 ... Δ=7,19,37,61...
Δ Δ = 12 , 18 , 24 , . . . \Delta\Delta=12, 18, 24, ... ΔΔ=12,18,24,...
Δ Δ Δ = 6 , 6 , . . . \Delta\Delta\Delta=6, 6, ... ΔΔΔ=6,6,...
通项公式最大指数为3,形式为: N = a 3 × n 3 + a 2 × n 2 + a 1 × n 1 + a 0 N=a_{3}\times n^{3}+a_{2}\times n^{2}+a_{1}\times n^{1}+a_{0} N=a3×n3+a2×n2+a1×n1+a0
使用 sympy 求解系数 a 3 , a 2 , a 1 , a 0 a_{3}, a_{2}, a_{1}, a_{0} a3,a2,a1,a0
# 导入 sympy
from sympy import *
# sympy 求解系数a3, a2,a1,a0,需要3个等式
# 声明代数符号 a2,a1,a0
a3, a2, a1, a0 = symbols("a3, a2, a1, a0")
# 等式1
eq_1 = Eq(a3 * 1 ** 3 + a2 * 1 ** 2 + a1 * 1 ** 1 + a0, 2)
# 等式2
eq_2 = Eq(a3 * 2 ** 3 + a2 * 2 ** 2 + a1 * 2 ** 1 + a0, 9)
# 等式3
eq_3 = Eq(a3 * 3 ** 3 + a2 * 3 ** 2 + a1 * 3 ** 1 + a0, 28)
# 等式4
eq_4 = Eq(a3 * 4 ** 3 + a2 * 4 ** 2 + a1 * 4 ** 1 + a0, 65)
# 使用 solve 求解等式
solve([eq_1, eq_2, eq_3, eq_4])
{ a 0 : 1 , a 1 : 0 , a 2 : 0 , a 3 : 1 } \displaystyle \left\{ a_{0} : 1, \ a_{1} : 0, \ a_{2} : 0, \ a_{3} : 1\right\} {a0:1, a1:0, a2:0, a3:1}
更多练习
9,5,1,-3, …
9,5,2,0,-1,-1,0,2,5,9, …
10,5,2,0,-2,-5,-10, …
- 分段函数
- 左极限右极限
#. 复合函数的绘制- 极限的求法
- 求导引入
- 导数与趋势
- sympy求导计算
- 复合函数的导数
- 微分和积分
- 不定积分
- 定积分
- 微分方程
f ( x ) = { − x + 1 ( x < 0 ) x − 2 ( x > 0 ) f\left( x\right) =\begin{cases}-x+1\left( x <0\right) \\ x-2\left( x >0\right) \end{cases} f(x)={−x+1(x<0)x−2(x>0)
# 绘制分段函数
# 1. 声明代数符号
x = Symbol('x')
# 2. 定义分段函数1表达式
expr_1 = -x + 1
# 3. 定义分段函数2表达式
expr_2 = x - 2
# 4. 绘制分段函数,函数1值域-3,0, 函数2值域,0,3
plot((expr_1, (x, -3, 0)), (expr_2, (x, 0, 3)))
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8U7CQbYH-1605604520353)(output_112_0.png)]
# 对于上面的分段函数
# 1. 分段函数1的极限,左极限
left_limit = expr_1.subs(x, -0.000000001)
left_limit
1.000000001 \displaystyle 1.000000001 1.000000001
# 2. 分段函数2的极限,右极限
right_limit = expr_2.subs(x, 0.000000001)
right_limit
− 1.999999999 \displaystyle -1.999999999 −1.999999999
# 复合函数的绘制
# 1. 声明一个代数符号
x = Symbol("x")
# 2. 定义函数1
f = x ** 2 + E ** x
f
x 2 + e x \displaystyle x^{2} + e^{x} x2+ex
# 绘制 f
plot(f, xlim=(-3, 3), ylim=(-10, 10))
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JSsu0Srn-1605604520354)(output_118_0.png)]
# 3. 定义函数2
g = tan(x)
g
tan ( x ) \displaystyle \tan{\left(x \right)} tan(x)
# 绘制函数 g
plot(g, xlim=(-3, 3), ylim=(-10, 10))
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Dj8PieRK-1605604520355)(output_120_0.png)]
# 4. 定义复合函数
h = f / g
h
x 2 + e x tan ( x ) \displaystyle \frac{x^{2} + e^{x}}{\tan{\left(x \right)}} tan(x)x2+ex
# 绘制函数 h
plot(h, xlim=(-3, 3), ylim=(-10, 10))
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EVcUksVq-1605604520355)(output_122_0.png)]
# 求函数 f = (x ** 2 - 1) / (x - 1) 的极限
# 1. 定义函数 f
f = (x ** 2 - 1) / (x -1)
f
x 2 − 1 x − 1 \displaystyle \frac{x^{2} - 1}{x - 1} x−1x2−1
# 2. 求 f 的极限,x在1处
limit(f, x, 1) # compute the limit of f of x at the point 1
2 \displaystyle 2 2
# 3. 对比,求 f 在 x = 1 处的值,无解
f.subs(x, 1)
NaN \displaystyle \text{NaN} NaN
# 4. 求 f 的左极限
limit(f, x, 1, dir="-") # left limit of f of x at the point 1
2 \displaystyle 2 2
# 5. 求 f 的右极限
limit(f, x, 1, dir="+") # right limit of f of x at the point 1
2 \displaystyle 2 2
# 求函数 f = 1 / x 的极限
# 1. 声明代数符号
x = Symbol("x")
type(x) # sympy.core.symbol.Symbol
# 2. 定义函数 f
f = 1 / x
f
1 x \displaystyle \frac{1}{x} x1
# 3. 绘制函数,x值域 -2, 3, y轴范围 -10, 10
plot(f, (x, -2, 2), ylim=(-10, 10))
[外链图片转存中…(img-4HjJL7eZ-1605604520357)]
# 4. 求 f 的极限,x 在 0 处
limit(f, x, 0)
∞ \displaystyle \infty ∞
# 5. 求 f 的左极限,x 在 0 处
limit(f, x, 0, dir="-")
− ∞ \displaystyle -\infty −∞
# 6. 求 f 的右极限,x 在 0 处
limit(f, x, 0, dir="+")
∞ \displaystyle \infty ∞
[外链图片转存中…(img-woqyRq7x-1605604520357)]
导数的数学含义
如何求导:
f ′ ( x ) = lim h → 0 f ( x ) − f ( x − h ) h f'\left( x\right) =\lim _{h\rightarrow 0}\dfrac{f\left( x\right) -f\left( x-h\right) }{h} f′(x)=limh→0hf(x)−f(x−h)
sympy 求导步骤
# 1. 定义代数符号
x, f = symbols("x, f")
# 2. 定义函数 f
f = cos(x)
# 3. 求导
diff(f, x)
思考:
一个推进器的位置和时间关系的函数如下: x ( t ) = 2 t 3 − t + 1 x(t) = 2t^3 - t + 1 x(t)=2t3−t+1
求:
# 定义函数 x
x, t = symbols("x, t")
x = 2 * t ** 3 - t + 1
x
2 t 3 − t + 1 \displaystyle 2 t^{3} - t + 1 2t3−t+1
# 求1:t=1的时候推进器的位置
x.subs(t, 1)
2 \displaystyle 2 2
# 求2:t=2的时候推进器的位置
x.subs(t, 2)
15 \displaystyle 15 15
# 求3:在时间1到2的之间推进器的平均速度
(x.subs(t, 2) - x.subs(t, 1)) / (2 - 1)
13 \displaystyle 13 13
# 求4:在时间1到1.5之间推进器的平均速度
(x.subs(t, 1.5) - x.subs(t, 1)) / (1.5 - 1)
8.5 \displaystyle 8.5 8.5
# 求5:在时间1到1.1之间推进器的平均速度
(x.subs(t, 1.1) - x.subs(t, 1)) / (1.1 - 1)
5.62 \displaystyle 5.62 5.62
# 求6:在时间1的时候,推进器的速度是多少?
(x.subs(t, 1.0000001) - x.subs(t, 1)) / (1.0000001 - 1)
5.00000059729999 \displaystyle 5.00000059729999 5.00000059729999
# 绘制函数x
plot(x)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MaJeXG8I-1605604520357)(output_142_0.png)]
# 绘制函数x,t的值域0,5
plot(x, (t, 0, 5))
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tQnD6ICc-1605604520358)(output_143_0.png)]
给定函数: y = x 3 y=x^{3} y=x3
函数导数: y ′ = 3 x 2 y'=3x^{2} y′=3x2
绘制函数图像
# 1. 声明代数符号
x, y, d_y = symbols("x, y, d_y")
# 2. 定义函数 y
y = x ** 3
y
x 3 \displaystyle x^{3} x3
# 3. 定义函数 d_y
d_y = 3 * x ** 2
d_y
3 x 2 \displaystyle 3 x^{2} 3x2
# 4. 绘制函数 y,红线,不显示,x范围-5,5
plot1 = plot(y, line_color="red", show=False, xlim=(-5, 5), legned=True)
# 5. 绘制函数 d_y, 蓝线,不显示,y范围-5,5,x范围-5,5
plot2 = plot(d_y, line_color="blue", show=False, ylim=(-5, 5), xlim=(-5, 5), legend=True)
# 将plot1加入plot2绘制列表中
plot2.append(plot1[0])
# 显示plot2
plot2.show()
[外链图片转存中…(img-8Njbsu4S-1605604520358)]
红线的增长趋势是什么, 红线的y值是一直变大.
如果函数一直是单调增长的, 那么他的导数的值 是一直大于0
求导: f = − x 3 f=-x^{3} f=−x3
# 1. 定义函数 f
f = - x ** 3
f
− x 3 \displaystyle - x^{3} −x3
# 2. 对 f 关于自变量 x 求导
diff(f, x)
− 3 x 2 \displaystyle - 3 x^{2} −3x2
求导: f = 3 f=3 f=3
# 1. 定义函数 f
f = 3
f
3 \displaystyle 3 3
# 2. 对 f 关于自变量 x 求导
diff(f, x)
0 \displaystyle 0 0
求导: x u x^{u} xu
# 1. 定义函数 f
u, f = symbols("u, f")
f = pow(x, u)
f
x u \displaystyle x^{u} xu
# 2. 对 f 关于自变量 x 求导
diff(f, x)
u x u x \displaystyle \frac{u x^{u}}{x} xuxu
求导: s i n ( x ) sin({x}) sin(x)
# 1. 定义函数 f
x, f = symbols("x, f")
f = sin(x)
f
sin ( x ) \displaystyle \sin{\left(x \right)} sin(x)
# 2. 对 f 关于自变量 x 求导
diff(f, x)
cos ( x ) \displaystyle \cos{\left(x \right)} cos(x)
[外链图片转存中…(img-kvsNoN4S-1605604520359)]
求导: u ( x ) × v ( x ) u\left( x\right) \times v\left( x\right) u(x)×v(x)
from sympy import *
# 1. 定义代数符号
x, u, v = symbols("x, u, v")
# 2. 创建关于自变量 x 的函数 u, v
u = Function("u")(x)
v = Function("v")(x)
# 3. 定义复合函数 h
h = u * v
h
u ( x ) v ( x ) \displaystyle u{\left(x \right)} v{\left(x \right)} u(x)v(x)
# 4. 对复合函数 h 关于自变量 x 求导
diff(h, x)
u ( x ) d d x v ( x ) + v ( x ) d d x u ( x ) \displaystyle u{\left(x \right)} \frac{d}{d x} v{\left(x \right)} + v{\left(x \right)} \frac{d}{d x} u{\left(x \right)} u(x)dxdv(x)+v(x)dxdu(x)
微分: 化整为零,把研究对象划分成非常非常小的单元去研究
积分: 把很小很小单元累加在一起看对结果的影响
分析加综合的研究方法
微分
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-p2C4vb6E-1605604520361)(attachment:image.png)]
泰勒展开式
在数学中,泰勒公式(英语:Taylor’s Formula)是一个用函数在某点的信息描述其附近取值的公式。
这个公式来自于微积分的泰勒定理(Taylor’s theorem),泰勒定理描述了一个可微函数,如果函数足够光滑的话,在已知函数在某一点的各阶导数值的情况之下,泰勒公式可以用这些导数值做系数构建一个多项式来近似函数在这一点的邻域中的值,这个多项式称为泰勒多项式(Taylor polynomial)。
泰勒公式还给出了余项即这个多项式和实际的函数值之间的偏差。泰勒公式得名于英国数学家布鲁克·泰勒。他在1712年的一封信里首次叙述了这个公式,尽管1671年詹姆斯·格雷高里已经发现了它的特例。拉格朗日在1797年之前,最先提出了带有余项的现在形式的泰勒定理。
[外链图片转存中…(img-KHr9hrSc-1605604520361)]
任何函数 都可以用多项式求和公式去模拟
f ( x ) = ∑ n = 0 ∞ f n ( x 0 ) n ! ( x − x 0 ) n f\left( x\right) =\sum ^{\infty }_{n=0}\dfrac{f^{n}\left( x_{0}\right) }{n!}\left( x-x_{0}\right) ^{n} f(x)=∑n=0∞n!fn(x0)(x−x0)n
多项式的项数越多,就越接近原函数
简单的理解泰勒公式,只保留两项
f ( x ) = f ( x 0 ) + f ′ ( x 0 ) Δ x f\left( x\right) =f\left( x_{0}\right) +f'\left( x_{0}\right) \Delta x f(x)=f(x0)+f′(x0)Δx
结 果 = 现 状 + 原 因 X 时 间
f ( x ) = f ( x 0 ) + f ′ ( x 0 ) Δ x + f ′ ′ ( x 0 ) Δ x 2 2 1 + … + … + … f\left( x\right) =f\left( x_{0}\right) +f'\left( x_{0}\right) \Delta x+f''\left( x_{0}\right) \dfrac{\Delta x^{2}}{2^{1}}+\ldots+\ldots+\ldots f(x)=f(x0)+f′(x0)Δx+f′′(x0)21Δx2+…+…+…
结 果 = 现 状 + 直 接 原 因 引 起 的 变 化 + 间 接 原 因 引 起 的 变 化 + …
概念
[外链图片转存中…(img-VCSA9fxv-1605604520362)]
求不定积分
∫ 1 x 2 \int \dfrac{1}{x^{2}} ∫x21
# 1. 声明代数符号
x = Symbol("x")
# 2. 定义函数表达式
expr = 1 / x ** 2
expr
1 x 2 \displaystyle \frac{1}{x^{2}} x21
# 3. 对表达式的x变量求积分
i_expr = integrate(expr, x)
i_expr
− 1 x \displaystyle - \frac{1}{x} −x1
# 4. 对积分结果求导,进行验证
d_expr = diff(i_expr, x)
d_expr
1 x 2 \displaystyle \frac{1}{x^{2}} x21
求定积分
∫ 0 a 3 x 2 − x + 1 \int ^{a}_{0}3x^{2}-x+1 ∫0a3x2−x+1
# 1. 定义代数符号
x = Symbol("x")
# 2. 定义表达式
expr = 3 * x ** 2 - x + 1
expr
3 x 2 − x + 1 \displaystyle 3 x^{2} - x + 1 3x2−x+1
# 3. 对表达式的x变量求定积分
i_expr = integrate(expr, (x, 0, a))
i_expr
a 3 − a 2 2 + a \displaystyle a^{3} - \frac{a^{2}}{2} + a a3−2a2+a
概念
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0Wibwdjm-1605604520364)(attachment:image.png)]
微分方程:维基百科
微分方程(英语:Differential equation,DE)是一种数学方程,用来描述某一类函数与其导数之间的关系。微分方程的解是一个符合方程的函数。而在初等数学的代数方程里,其解是常数值。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-US09rp7W-1605604520365)(attachment:image.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ep5K7fkF-1605604520365)(attachment:image.png)]
求解微分方程1
g ′ ′ ( x ) − 2 g ′ ( x ) + g ( x ) − sin ( x ) = 0 g''\left( x\right) -2g'\left( x\right) +g\left( x\right) -\sin \left( x\right) =0 g′′(x)−2g′(x)+g(x)−sin(x)=0
# 1. 声明关于自变量x的函数g
g = Function("g")(x)
g
g ( x ) \displaystyle g{\left(x \right)} g(x)
# 2. 声明自变量x的代数符号
x = Symbol("x")
x
x \displaystyle x x
# 3. 建立微分方程等式
# g.diff(x, 2): g 关于 x 的二阶导数
# g.diff(x, 1): g 关于 x 的一阶导数
eq = Eq(g.diff(x, 2) - 2 * g.diff(x, 1) -sin(x), 0)
eq
− sin ( x ) − 2 d d x g ( x ) + d 2 d x 2 g ( x ) = 0 \displaystyle - \sin{\left(x \right)} - 2 \frac{d}{d x} g{\left(x \right)} + \frac{d^{2}}{d x^{2}} g{\left(x \right)} = 0 −sin(x)−2dxdg(x)+dx2d2g(x)=0
# 4. 求解微分方程
# Solves any (supported) kind of ordinary differential equation and
# system of ordinary differential equations.
dsolve(eq)
g ( x ) = C 1 + C 2 e 2 x − sin ( x ) 5 + 2 cos ( x ) 5 \displaystyle g{\left(x \right)} = C_{1} + C_{2} e^{2 x} - \frac{\sin{\left(x \right)}}{5} + \frac{2 \cos{\left(x \right)}}{5} g(x)=C1+C2e2x−5sin(x)+52cos(x)
求解微分方程1
d y d x = sin ( x ) \dfrac{dy}{dx}=\sin \left( x\right) dxdy=sin(x)
# 1. 声明关于自变量x的函数y
y = Function("y")(x)
y
y ( x ) \displaystyle y{\left(x \right)} y(x)
# 2. 声明自变量代数符号
x = Symbol("x")
x
x \displaystyle x x
# 3. 建立微分方程等式
eq = Eq(y.diff(x, 1), sin(x))
eq
d d x y ( x ) = sin ( x ) \displaystyle \frac{d}{d x} y{\left(x \right)} = \sin{\left(x \right)} dxdy(x)=sin(x)
# 4. 求解微分方程
dsolve(eq)
y ( x ) = C 1 − cos ( x ) \displaystyle y{\left(x \right)} = C_{1} - \cos{\left(x \right)} y(x)=C1−cos(x)
- 图表绘制
- 绘制子表格
# 导包
%matplotlib notebook
# 魔法配置,可以让表格显示在jupyter里面
import matplotlib.pyplot as plt #导包
#绘制折线
fig = plt.figure() # 创建一个图表
plt.plot([2,3,5,4]) #默认会把x轴补齐, (0,2) (1,3) (2,5) (3,4)
plt.show()
#指定绘制的图表大小
fig = plt.figure(figsize=(4,4),dpi=80) # 创建一个图表
plt.plot([2,3,5,4]) #默认会把x轴补齐, (0,2) (1,3) (2,5) (3,4)
plt.show()
# 修改表格线的宽度和颜色
fig = plt.figure(figsize=(4,4),dpi=80) # 创建一个图表
plt.plot([1,2,3,4],[1,2,3,5],color='g',linewidth=1,linestyle="--") #默认会把x轴补齐, (1,1) (2,2) (3,3) (4,5)
plt.show()
#指定x和y轴的范围,和标签内容
fig = plt.figure(figsize=(4,4),dpi=80) # 创建一个图表,设定大小
plt.plot([1,2,3,4],[1,2,3,5],color='g',linewidth=1,linestyle="--") #默认会把x轴补齐, (1,1) (2,2) (3,3) (4,5)
plt.xlim(-2,4)
plt.ylim(0,5)
plt.yticks([1,2,3,4])
plt.xticks([0,1,2,3])
plt.show()
#设置关键点标注
fig = plt.figure(figsize=(4,4),dpi=80) # 创建一个图表
plt.plot([1,2,3,4],[1,2,3,5],color='g',linewidth=1,linestyle="--") #默认会把x轴补齐, (1,1) (2,2) (3,3) (4,5)
# 添加标注
plt.annotate('key point',xy=(3,3),xytext=(+3,+4.5),arrowprops=dict(arrowstyle="->",connectionstyle="arc3"))
#annotate('',xy=(1.8,0.5),xytext=(tx,ty),arrowprops=dict(arrowstyle="->",connectionstyle="arc3"))
plt.show()
# 准备数据
# numpy
import numpy as np
X = np.linspace(-np.pi,np.pi,100)
Y = np.sin(X)
Z = np.cos(X)
fig = plt.figure(figsize=(4,4),dpi=80) # 创建一个图表
plt.plot(X,Y,color='g',linewidth=1,linestyle="--")
plt.plot(X,Z,color='r',linewidth=1,linestyle="-")
plt.show()
import numpy as np
X = np.linspace(-np.pi,np.pi,100)
Y = np.sin(X)
Z = np.cos(X)
fig = plt.figure(figsize=(4,4),dpi=80) # 创建一个图表
# 绘制子表格
plt.subplot(2,1,1) # 在2行1列的地方 绘制在第一个位置
plt.plot(X,Y)
plt.subplot(2,1,2) # 在2行1列的地方 绘制在第二个位置
plt.plot(X,Z)
plt.show()
##打开关闭网格
import numpy as np
X = np.linspace(-np.pi,np.pi,100)
Y = np.sin(X)
Z = np.cos(X)
fig = plt.figure(figsize=(4,4),dpi=80) # 创建一个图表
# 绘制子表格
plt.subplot(2,1,1) # 在2行1列的地方 绘制在第一个位置
plt.plot(X,Y)
plt.subplot(2,1,2) # 在2行1列的地方 绘制在第二个位置
plt.plot(X,Z)
plt.grid('on')
plt.show()