@ -0,0 +1,17 @@
|
|||||||
|
'''
|
||||||
|
Author: SJ2050
|
||||||
|
Date: 2021-10-26 13:44:25
|
||||||
|
LastEditTime: 2021-10-26 13:52:13
|
||||||
|
Version: v0.0.1
|
||||||
|
Description: Solution for homework 1.1.
|
||||||
|
Copyright © 2021 SJ2050
|
||||||
|
'''
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
mat = np.array([[10, 34, 54, 23],
|
||||||
|
[31, 87, 53, 68],
|
||||||
|
[98, 49, 25, 11],
|
||||||
|
[84, 32, 67, 88]])
|
||||||
|
mat_padded = np.pad(mat, ((1, 1), (1, 1)), 'constant', constant_values=((0, 0), (0, 0)))
|
||||||
|
print(mat_padded)
|
After Width: | Height: | Size: 13 KiB |
@ -0,0 +1,13 @@
|
|||||||
|
'''
|
||||||
|
Author: SJ2050
|
||||||
|
Date: 2021-10-27 12:50:53
|
||||||
|
LastEditTime: 2021-10-27 12:57:38
|
||||||
|
Version: v0.0.1
|
||||||
|
Description: Solution for homework 1.2.
|
||||||
|
Copyright © 2021 SJ2050
|
||||||
|
'''
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
mat = np.diag(range(1, 5), -1)
|
||||||
|
print(mat)
|
After Width: | Height: | Size: 7.5 KiB |
@ -0,0 +1,27 @@
|
|||||||
|
'''
|
||||||
|
Author: SJ2050
|
||||||
|
Date: 2021-10-27 13:08:32
|
||||||
|
LastEditTime: 2021-10-27 19:06:32
|
||||||
|
Version: v0.0.1
|
||||||
|
Description: Solution for homework1.3.
|
||||||
|
Copyright © 2021 SJ2050
|
||||||
|
'''
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
first_row = np.vectorize(lambda x: (x+1)%2)(np.array([i for i in range(8)]))
|
||||||
|
second_row = np.vectorize(lambda x: x%2)(np.array([i for i in range(8)]))
|
||||||
|
mat = np.zeros((8, 8), dtype=np.int)
|
||||||
|
for i in range(0, 8):
|
||||||
|
if i % 2 == 0:
|
||||||
|
mat[i] = first_row
|
||||||
|
else:
|
||||||
|
mat[i] = second_row
|
||||||
|
# print(mat)
|
||||||
|
# beautify output
|
||||||
|
print('—————————————————————————————————')
|
||||||
|
for r in range(8):
|
||||||
|
for c in range(8):
|
||||||
|
print(f'| {mat[r][c]} ', end='')
|
||||||
|
print('|')
|
||||||
|
print('—————————————————————————————————')
|
After Width: | Height: | Size: 13 KiB |
@ -0,0 +1,54 @@
|
|||||||
|
'''
|
||||||
|
Author: SJ2050
|
||||||
|
Date: 2021-10-29 18:46:40
|
||||||
|
LastEditTime: 2021-10-29 22:41:31
|
||||||
|
Version: v0.0.1
|
||||||
|
Description: Solution for homework1.4.
|
||||||
|
Copyright © 2021 SJ2050
|
||||||
|
'''
|
||||||
|
import numpy as np
|
||||||
|
from scipy.linalg import lu_factor, lu_solve
|
||||||
|
|
||||||
|
def gauss_eliminate(K, b):
|
||||||
|
K = np.array(K)
|
||||||
|
b = np.array(b)
|
||||||
|
n = len(K)
|
||||||
|
x = np.zeros(n)
|
||||||
|
|
||||||
|
for i in range(0, n-1):
|
||||||
|
for j in range(i+1,n):
|
||||||
|
c = -K[j][i]/K[i][i]
|
||||||
|
for k in range(0, n):
|
||||||
|
K[j][k] += K[i][k]*c
|
||||||
|
b[j] += b[i]*c
|
||||||
|
x[n-1]=b[n-1]/K[n-1][n-1]
|
||||||
|
|
||||||
|
for i in range(n-2, -1, -1):
|
||||||
|
for j in range(i+1, n):
|
||||||
|
b[i] -= K[i][j]*x[j]
|
||||||
|
x[i] = b[i]/K[i][i]
|
||||||
|
|
||||||
|
return x
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
K = np.mat([[3, 4, 2],
|
||||||
|
[5, 3, 4],
|
||||||
|
[8, 2, 7]])
|
||||||
|
b = np.mat([[10], [14], [20]])
|
||||||
|
# 1. 系数矩阵求逆求解线性方程组
|
||||||
|
s1 = K**(-1)*b
|
||||||
|
print('1.系数矩阵求逆求解:\n', s1)
|
||||||
|
|
||||||
|
# 2. 直接求解法求解线性方程组
|
||||||
|
s2 = np.linalg.solve(K, b)
|
||||||
|
print('2.直接求解法解线性方程组:\n', s2)
|
||||||
|
|
||||||
|
# 3. 使用LU分解求解线性方程组
|
||||||
|
lu, piv = lu_factor(K)
|
||||||
|
s3 = lu_solve((lu, piv), b)
|
||||||
|
print('3.LU分解求解线性方程组: \n', s3)
|
||||||
|
|
||||||
|
# 4. 使用高斯消元法求解线性方程组
|
||||||
|
s4 = gauss_eliminate(K[:, :]*1.0, [x[0]*1.0 for x in b])
|
||||||
|
print('4.高斯消元法求解线性方程组: \n', s4)
|
After Width: | Height: | Size: 38 KiB |
@ -0,0 +1,15 @@
|
|||||||
|
'''
|
||||||
|
Author: SJ2050
|
||||||
|
Date: 2021-10-29 19:19:32
|
||||||
|
LastEditTime: 2021-10-29 22:43:06
|
||||||
|
Version: v0.0.1
|
||||||
|
Description: Solution for homework1.5.
|
||||||
|
Copyright © 2021 SJ2050
|
||||||
|
'''
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
arr = np.array([i for i in range(10)])
|
||||||
|
reversed_arr = arr[::-1]
|
||||||
|
print('初始数组:\n', arr)
|
||||||
|
print('翻转后数组:\n', reversed_arr)
|
After Width: | Height: | Size: 21 KiB |
@ -0,0 +1,17 @@
|
|||||||
|
'''
|
||||||
|
Author: SJ2050
|
||||||
|
Date: 2021-10-29 19:23:02
|
||||||
|
LastEditTime: 2021-10-29 22:45:38
|
||||||
|
Version: v0.0.1
|
||||||
|
Description: Solution for homework1_6.
|
||||||
|
Copyright © 2021 SJ2050
|
||||||
|
'''
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
random_mat = np.random.rand(10, 10)
|
||||||
|
max_one = np.max(random_mat)
|
||||||
|
min_one = np.min(random_mat)
|
||||||
|
|
||||||
|
print('随机产生的矩阵如下:\n', random_mat)
|
||||||
|
print(f'最大值元素为: {max_one}, 最小值元素为: {min_one}')
|
After Width: | Height: | Size: 108 KiB |
@ -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}')
|
||||||
|
|
After Width: | Height: | Size: 19 KiB |
After Width: | Height: | Size: 9.6 KiB |
@ -0,0 +1,20 @@
|
|||||||
|
'''
|
||||||
|
Author: SJ2050
|
||||||
|
Date: 2021-10-29 19:51:52
|
||||||
|
LastEditTime: 2021-10-29 19:55:28
|
||||||
|
Version: v0.0.1
|
||||||
|
Description: Solution for homework2.2.
|
||||||
|
Copyright © 2021 SJ2050
|
||||||
|
'''
|
||||||
|
import numpy as np
|
||||||
|
from matplotlib import pyplot as plt
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
y_func = lambda x: np.sin(x-2)**2*np.exp(-x**2)
|
||||||
|
xs = np.linspace(0, 2)
|
||||||
|
ys = np.array([y_func(x) for x in xs])
|
||||||
|
plt.plot(xs, ys, color='blue')
|
||||||
|
plt.xlabel('x label')
|
||||||
|
plt.ylabel('y label')
|
||||||
|
plt.title('$sin^2(x-2)e^{-x^2}$')
|
||||||
|
plt.show()
|
After Width: | Height: | Size: 20 KiB |
@ -0,0 +1,24 @@
|
|||||||
|
'''
|
||||||
|
Author: SJ2050
|
||||||
|
Date: 2021-10-29 19:56:04
|
||||||
|
LastEditTime: 2021-10-29 22:57:17
|
||||||
|
Version: v0.0.1
|
||||||
|
Description: Solution for homework2.3.
|
||||||
|
Copyright © 2021 SJ2050
|
||||||
|
'''
|
||||||
|
import numpy as np
|
||||||
|
from matplotlib import pyplot as plt
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
ts = np.array([i for i in range(1000)])
|
||||||
|
xs = np.zeros(1000)
|
||||||
|
ys = np.zeros(1000)
|
||||||
|
xs[0] = np.random.rand(1)*2-1
|
||||||
|
ys[0] = np.random.rand(1)*2-1
|
||||||
|
|
||||||
|
for i in range(1, 1000):
|
||||||
|
xs[i] = xs[i-1]+np.random.rand(1)*2-1
|
||||||
|
ys[i] = ys[i-1]+np.random.rand(1)*2-1
|
||||||
|
|
||||||
|
plt.plot(xs, ys)
|
||||||
|
plt.show()
|
After Width: | Height: | Size: 66 KiB |