98 篇文章 18 订阅

# 2. 举个栗子

f ( x ) = x 3 − 4 x 2 + 4 x + 2 f(x)=x^3-4x^2+4x+2

I = ∫ a b f ( x ) d x = ∫ a b ( x 3 − 4 x 2 + 4 x + 2 ) d x   I = \int_a^bf(x)dx= \int_a^b(x^3-4x^2+4x+2)dx\,

I = ( x 4 4 − 4 x 3 3 + 2 x 2 + 2 x ) ∣ a b = 61 12 ≈ 5.0833 I=(\frac{x^4}{4}-\frac{4x^3}{3}+2x^2+2x)|_{a}^{b}=\frac{61}{12} \approx 5.0833

# 3. 矩形计算面积

## 3.1 左侧边长计算面积

import numpy as np
x = np.linspace(0, 3, 1001)
f = lambda x: x**3 - 4*x**2 + 4*x + 2
a = 0.5
b = 2.5
Ax = np.linspace(a, b, 101)
Ay = f(Ax)
def defInt_left(f, a, b, N):
# left-hand point
result = 0; FX = []; Xn = []
dx = abs(b - a)/N
while a < b:
result += f(a)*dx
FX += [f(a)]
Xn += [a]
a += dx
return result, FX, Xn, dx
N = 4
I_left, FX, Xn, dx = defInt_left(f, a, b, N)
print(I_left)


5.25


## 3.2 右侧边长计算面积

def defInt_right(f, a, b, N):
# right-hand point
result = 0; FX = []; Xn = []
dx = abs(b - a)/N
while a < b:
result += f(a + dx)*dx
FX += [f(a + dx)]
Xn += [a]
a += dx
return result, FX, Xn, dx

N = 4
I_right, FX, Xn, dx = defInt_right(f, a, b, N)
print(I_right)


5.0


## 3.3 中值边长计算面积

def defInt_middle(f, a, b, N):
# middle point
result = 0; FX = []; Xn = []
dx = abs(b - a)/N
while a < b:
result += f(a + dx/2)*dx
FX += [f(a + dx/2)]
Xn += [a]
a += dx
return result, FX, Xn, dx

N = 4
I_mid, FX, Xn, dx = defInt_middle(f, a, b, N)
print(I_mid)


5.0625


# 4. 梯形计算面积

def defInt_trapezoid(f, a, b, N):
# trapezoidal rule
result = 0; FXa, FXb = [], []; Xn = []
dx = abs(b - a)/N
while a < b:
result += (f(a) + f(a + dx))*dx/2
FXa += [f(a)]; FXb += [f(a + dx)]
Xn += [a]
a += dx
return result, FXa, FXb, Xn, dx

N = 4
I_trap, FXa, FXb, Xn, dx = defInt_trapezoid(f, a, b, N)
print(I_trap)


5.125


# 5. 真值比对

Nx = range(1, 11)
I1, I2, I3, I4 = [], [], [], []
for Ni in Nx:
i1, *_ = defInt_left(f, a, b, Ni); I1 += [i1];
i2, *_ = defInt_right(f, a, b, Ni); I2 += [i2];
i3, *_ = defInt_middle(f, a, b, Ni); I3 += [i3];
i4, *_ = defInt_trapezoid(f, a, b, Ni); I4 += [i4];


# 6. 总结

• 3
点赞
• 18
收藏
• 打赏
• 1
评论
09-18
12-03 42
01-12 8819
10-12 394
05-22 1372
04-22 8028
11-20 5710
03-16 6078
06-04 10万+

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

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

¥2 ¥4 ¥6 ¥10 ¥20

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