读书笔记+画图
print("02 17向悦")
import numpy as np
# 创建两个矩阵
a = np.array([[1,2,3],[4,5,6]])
b = np.array([[7,8],[9,10],[11,12]])
# 计算矩阵乘积
c = np.dot(a,b)
# 打印结果
print(c)
import scipy.optimize as opt
# 定义方程组的函数
def f(x):
return [x[0]**2 + x[1]**2 - 1, x[0] - x[1] + 0.5]
# 用fsolve函数求解,给定初始值
x = opt.fsolve(f, [0,0])
# 打印结果
print(x)
import pandas as pd
# 读取txt文档中的内容
with open('B附件2.txt', 'r') as f:
lines = f.readlines()
# 跳过前面几行
skip_lines = 13
data = []
for line in lines[skip_lines:]:
line = line.strip()
# 使用split()函数将每一行的数据分割成多个元素
row = line.split()
row_data = []
for element in row:
try:
# 尝试将元素转换为浮点数
row_data.append(float(element))
except ValueError:
# 如果转换失败,将元素作为字符串存储
row_data.append(element)
# 将每一行的数据储存在嵌套列表中
data.append(row_data)
# 将数据写入Excel
df = pd.DataFrame(data)
df.to_excel('附件2.xlsx', index=False, header=False)
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.family'] = 'SimHei'
plt.rcParams['font.sans-serif'] = ['SimHei']
def draw(pcolor, nt_point, nt_text, nt_size):
plt.plot(x, y, label='$exp_decay$', color=pcolor, linewidth=3, linestyle="-")
plt.plot(x, z, "b--", label='$cos(x^2)$', linewidth=1)
plt.xlabel('时间(s)')
plt.ylabel('幅度(mv)')
plt.title("阻尼衰减曲线绘制")
plt.annotate('$\cos(2 \pi t) \exp(-t)$', xy=nt_point, xytext=nt_text, fontsize=nt_size,
arrowprops=dict(arrowstyle='->', connectionstyle="arc3, rad=.1"))
def shadow(a, b):
ix = (x > a) & (x < b)
plt.fill_between(x, y, 0, where=ix, facecolor='grey', alpha=0.25)
plt.text(0.5 * (a + b), 0.2, r"$\int_a^b f(x) \mathrm{d}x$", horizontalalignment='center')
def xy_axis(x_start, x_end, y_start, y_end):
plt.xlim(x_start, x_end)
plt.ylim(y_start, y_end)
plt.xticks([np.pi/3, 2 * np.pi/3, 1 * np.pi, 4 * np.pi/3, 5 * np.pi/3],
['$\pi/3$', '$2\pi/3$', '$\pi$', '$4\pi/3$', '$5\pi/3$'])
x = np.linspace(0.0, 6.0, 100)
y = np.cos(2 * np.pi * x) * np.exp(-x) + 0.8
z = 0.5 * np.cos(x ** 2) + 0.8
note_point, note_text, note_size = (1, np.cos(2*np.pi)*np.exp(-1)+0.8), (1, 1.4), 14
fig = plt.figure(figsize=(8, 6), facecolor="white")
plt.subplot(111)
draw("red", note_point, note_text, note_size)
xy_axis(0, 5, 0, 1.8)
shadow(0.8, 3)
plt.legend()
plt.show()


浙公网安备 33010602011771号