Finished Homework2.

dev
SJ2050cn 4 years ago
parent b34436ca5c
commit 19681c61d6

@ -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)

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

@ -0,0 +1,12 @@
# 1.1 用0填充数组边界
## (1). 解题思路
`numpy`库中提供了一个名为`pad`的方法可以用来填充数组的边界其提供的功能非常多可以指定上下左右边界的宽度并且可以填充为最小值、平均值、常值等等。这个题目只需把上下左右的边界宽度置为1且用0填充即可。
## (2). 运行结果
![](images/result.png)

@ -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)

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

@ -0,0 +1,12 @@
# 1. 2 设置值1, 2, 3, 4落在5x5矩阵对角线下方位置
## (1). 解题思路
`numpy`库中提供了一个名为`diag`的方法用来提取或者构造对角矩阵,当它用于构造矩阵时,其第一个参数可以指定设置的元素,第二个参数设定元素落入的位置,`大于0`表示位于对角线上方位置,`小于0`表示位于对角线下方的位置,对于本题,设置为为-1。
## (2). 运行结果
![](images/resultpng.png)

@ -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('—————————————————————————————————')

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

@ -0,0 +1,12 @@
# 1.3 创建8x8矩阵并设置成国际象棋棋盘样式
## (1). 解题思路
国际象棋棋盘每一格由黑或白构成,同时,每个格子与其相邻格子的颜色不同,通过观察,偶数行(如果从0开始计数的话)的样式与第0行相同奇数行的样式与第一行相同通过循环可以很快的构建一个描述棋盘样式的矩阵出来。
## (2). 运行结果
![](images/result.png)

@ -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)

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

@ -0,0 +1,17 @@
# 1.4 求解线性方程组
## (1). 解题说明
`numpy`与`scipy`库中提供了常用求解线性方程组的方法包括矩阵求逆、直接求解、LU分解等对于比较特殊的矩阵例如稀疏矩阵还提供了特殊的求解方法。不同方法的优缺点比较如下
| 求解方法 | 优点 | 缺点 |
| :-------------: | :--------------: | :--------------: |
| 1. 矩阵求逆求解 | 简单 | 效率较低 |
| 2. 消元法求解 | 算法直观、稳定 | 效率低 |
| 3. LU分解 | LU分解结果可复用 | 不保证数值稳定性 |
## (2). 运行结果
![](images/result.png)

@ -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)

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

@ -0,0 +1,10 @@
# 1.5 翻转数组
## (1). 解题思路
`python`中的列表数组等支持切片操作,并且切片操作中可以指定步长,如果步长为负,则倒着走,这里需要翻转数组,只需要把步长设为-1即可。
## (2). 运行结果
![](images/result.png)

@ -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}')

Binary file not shown.

After

Width:  |  Height:  |  Size: 108 KiB

@ -0,0 +1,11 @@
# 1.6 寻找10x10数组最大值
## (1). 解题思路
`10x10`的数组可以通过你`numpy.random.rand`函数通过给定`10x10`的维度生成,然后直接调用内置方法`max`与`min`可以找到数组中的最大最小值。
## (2). 运行结果
![](images/result.png)

@ -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)

@ -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()

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

@ -0,0 +1,12 @@
# 2.2 绘制函数并画出标题以及x, y轴
## (1). 解题思路
绘制函数可以直接调用`matplotlib.pyplot`画出而标题以及x, y轴可以通过设置`pyplot`对象中的`title`以及`xlabel`与`ylabel`属性进行绘制,它们支持`Latex`语法。
## (2). 运行结果
![](images/result.png)

@ -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()

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

@ -0,0 +1,11 @@
# 2.3 模拟一个醉汉在二维空间上的随机漫步
## (1). 解题思路
假设醉汉每一步的大小在`x`与`y`轴上的投影不超过1在每个时刻在`x`与`y`方向上都会生成一个[-1, 1]的数,然后与时刻的位置进行叠加,并把每个时刻的位置记录下来,最后就可以绘制出醉汉在二维空间上的随机漫步。
## (2). 运行结果
![](images/result.png)
Loading…
Cancel
Save