-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment_2.py
More file actions
41 lines (28 loc) · 773 Bytes
/
assignment_2.py
File metadata and controls
41 lines (28 loc) · 773 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#Q1
def Fibonacci(n):
assert n > 0 and isinstance(n, int)
if n == 1:
return [1]
sequence = [1, 1]
while len(sequence) < n:
nvalue = sequence[-1] + sequence[-2]
sequence.append(nvalue)
return sequence
#Q2
def integral_calculation(func, l, u, n=9527):
assert u > l
dx = (u - l) / n
total_area = 0.0
for i in range(n):
x_rectangle = l + i * dx
height = func(x_rectangle)
area = height * dx
total_area += area
return total_area
#Q2_test
def square(x):
return x**2
if __name__ == "__main__":
print(f"Fibonacci数列的前10项为:{Fibonacci(10)}")
integral_result = integral_calculation(square, -2, 2)
print(f"积分结果为:{integral_result}")