You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
856 B
30 lines
856 B
'''
|
|
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}')
|
|
|