Finished Homework2.

This commit is contained in:
SJ2050cn
2021-10-29 23:58:21 +08:00
parent b34436ca5c
commit 19681c61d6
28 changed files with 325 additions and 0 deletions
@@ -0,0 +1,29 @@
'''
Author: SJ2050
Date: 2021-10-29 19:29:51
LastEditTime: 2021-10-29 19:50:31
Version: v0.0.1
Description: Solution for homework2.1.
Copyright © 2021 SJ2050
'''
import numpy as np
from matplotlib import pyplot as plt
if __name__ == '__main__':
y_func = lambda x: (x-1)**2+2
N = 50
xs = np.linspace(-5, 5, num=N+1)
ys = np.array([y_func(x) for x in xs])
integrate_sum = 0
for i in range(N):
plt.plot([xs[i], xs[i]], [0, ys[i]], color='black')
plt.plot([xs[i], xs[i+1]], [ys[i], ys[i+1]], color='black')
plt.plot([xs[i+1], xs[i+1]], [ys[i+1], 0], color='black')
integrate_sum += 0.5*(ys[i]+ys[i+1])*(xs[i+1]-xs[i])
plt.plot(xs, ys, color='green')
plt.ylim(0, 40)
plt.show()
print(f'(x-1)^2+2在[-5, 5]区间上使用梯形求积为: {integrate_sum}')
Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.6 KiB

@@ -0,0 +1,12 @@
# 2.1 绘制一个二次函数并用梯形法求积分
## (1). 解题思路
当给定一个二次函数后,在`x`范围内取`n`等分,然后每一份组成的梯形可以通过用直线绘制梯形的上下底以及斜边画出,最后利用梯形求面积公式求出这一个梯形的面积,全部梯形面积累加和极为所求积分。
## (2). 运行结果
![](images/result1.png)
![](images/result2.png)