大学物理实验 载流圆线圈轴线上的磁场分布
首先说好,本宝宝不定期更新
另外,代码写的比较冗杂,需要拿走,不喜勿喷。
考虑到有可能会有不太精通Python的朋友,首先是说好,本代码仅支持Jupyter Notebook,因为用到了魔法函数。
额。。。反正就是只能用jupyter,具体操作见最后。
实验数据处理
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
# 前提准备
x = np.arange(-7,8)*0.01
u=4*3.14*(10**-7)
N = 500
R = 0.11
I = 0.3
#开始计算
Bl = (u*N*I*R*R)/(2*(R*R+x*x)**(3/2))*1000
print("理论数值为:",np.round(Bl,3))
# 实际测量值为:
Ba =[0.509,0.576,0.643,0.711,0.772,0.823,0.855,0.865,0.855,0.821,0.770,0.709,0.640,0.572,0.504]
# 求相对误差
dif = (Ba-Bl)/Bl*100
print("相对误差为:",np.round(dif,3))
b1 = [0.159,0.182,0.209,0.242,0.275,0.321,0.369,0.422,0.479,0.545,0.616,0.685,0.752,0.807,0.848,0.867,0.864,0.838,0.793,0.736,0.667]
b2 = [0.650,0.719,0.782,0.828,0.860,0.873,0.859,0.824,0.773,0.703,0.640,0.568,0.504,0.443,0.387,0.336,0.293,0.258,0.225,0.199,0.175]
b1 = np.array(b1)
b2 = np.array(b2)
print(b1+b2)
画图
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
# let's draw a picture
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
#数据准备
Bl = np.array([0.514, 0.58, 0.646, 0.711, 0.769, 0.816, 0.846, 0.857, 0.846, 0.816, 0.769, 0.711, 0.646, 0.58, 0.514])
Ba = np.array([0.509,0.576,0.643,0.711,0.772,0.823,0.855,0.865,0.855,0.821,0.770,0.709,0.640,0.572,0.504])
x = np.arange(-7,8)
plt.figure()
plt.ylim((0,1))
#画图
plt.plot(x,Bl,'-or',label='理论曲线')
plt.plot(x,Ba,'ok',label='实验曲线')
plt.legend(loc='lower right')
#调整
plt.xlabel('x/cm')
plt.ylabel('B/mT')
new_ticks = np.array([-8,-6,-4,-2,0,2,4,6,8])
plt.xticks(new_ticks)
plt.grid(True)#网格线
plt.title('图1:载流圆线圈轴线上的磁场分布图')
#gca = get current axis
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.spines['bottom'].set_position(('data', 0))
ax.spines['left'].set_position(('data', 0))
plt.savefig('./图1:载流圆线圈轴线上的磁场分布图.jpg',dpi=400) #保存图片,清晰度400
plt.show()
同理,改改就是另外几个图了,还不快copy paste modify一下!
仅供参考哦
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
# let's draw a picture
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
b1 = np.array([0.159,0.182,0.209,0.242,0.275,0.321,0.369,0.422,0.479,0.545,0.616,0.685,0.752,0.807,0.848,0.867,0.864,0.838,0.793,0.736,0.667])
b2 = np.array([0.650,0.719,0.782,0.828,0.860,0.873,0.859,0.824,0.773,0.703,0.640,0.568,0.504,0.443,0.387,0.336,0.293,0.258,0.225,0.199,0.175])
b12 = np.array([0.806,0.898,0.988,1.070,1.134,1.187,1.222,1.240,1.247,1.249,1.249,1.248,1.246,1.241,1.224,1.195,1.147,1.087,1.008,0.924,0.833])
x = np.arange(-10,11)
plt.figure()
plt.ylim((0,1.5))
plt.plot(x,b1+b2,'-or',label=r'$B_1+B_2$')
plt.plot(x,b12,'ok',label=r'$B_{1+2}$')
plt.legend(loc='lower right')
plt.xlabel('x/cm')
plt.ylabel('B/mT')
plt.grid(True)#网格线
plt.title('图2:亥姆霍兹线圈轴线上的磁场分布图')
new_ticks = np.array([-10,-8,-6,-4,-2,0,2,4,6,8,10])
plt.xticks(new_ticks)
#gca = get current axis
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.spines['bottom'].set_position(('data', 0))
ax.spines['left'].set_position(('data', 0))
plt.savefig('./图2:亥姆霍兹线圈轴线上的磁场分布图.jpg',dpi=400) #保存图片,清晰度400
plt.show()
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
# let's draw a picture
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
b1 = np.array([0.159,0.182,0.209,0.242,0.275,0.321,0.369,0.422,0.479,0.545,0.616,0.685,0.752,0.807,0.848,0.867,0.864,0.838,0.793,0.736,0.667])
b2 = np.array([0.650,0.719,0.782,0.828,0.860,0.873,0.859,0.824,0.773,0.703,0.640,0.568,0.504,0.443,0.387,0.336,0.293,0.258,0.225,0.199,0.175])
x = np.arange(-10,11)
plt.figure()
plt.plot(x,b1,'k')
plt.plot(x,b2,'k')
plt.plot(x,b1+b2,'r')
plt.text(-5.0,0.9,r"$B_2$",fontdict={'size':16})
plt.text(5.0,0.9,r"$B_1$",fontdict={'size':16})
plt.text(5.2,1.3,r"$B=B_1+B_2$",fontdict={'size':16})
plt.ylim((0,1.5))
plt.xlabel('x')
plt.ylabel('B')
plt.grid(True)#网格线
plt.title('图3:亥姆霍兹线圈轴线上的磁场')
#gca = get current axis
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.spines['bottom'].set_position(('data', 0))
ax.spines['left'].set_position(('data', 0))
plt.savefig('./图3:亥姆霍兹线圈轴线上的磁场.jpg',dpi=400) #保存图片,清晰度400
plt.show()
萌新教程
- 代码分为数据处理和画图两个部分,你需要的是拿你的实验数据来替换我的,注意对应好,别理论值实验值傻傻分不清cu。
- 数据处理部分:进行相应的计算,这样比计算器还快,其他的实验也可以批量计算,改改公式即可
- 画图部分的数据,相应的数据由数据处理部分复制而来(也有一点点是自己手输),所以记得分成几个ceil运行代码,我已经给分好了
- 画图坐标:调一下x,y即可,分别是x = np.arange(-7,8)和plt.ylim((0,1)),其中x的原则是你有多少个数据就设置多少,例如15个数据就设置x为(-7,8)因为8-(-7)=15,而y的原则是你的数据显示,比方说这15个数都分布在0到10之间,那我设置y轴为(0,10)就很好,如果分布在0到1呢,可能变换就不明显了,此时应当限制y为(0,1)才能看到明显的曲线变换。
- 画图坐标轴:plt.xlabel(‘x’),这是控制显示在xy轴上的数据的,可以随便改,也可以删掉。
- 画图文本:plt.text(-5.0,0.9,r" B 2 B_2 B2",fontdict={‘size’:16}),同上,可改引号内内容,也可删
- 画图标题:写着汉字,自己改。总之一句话,“调整”部分的代码,自己看着办,不动也行。
- 图片保存:返回jupyter的主页,就能看到,勾选,点Download即可,实在不行截屏也可。
这要是再有人不会,我可就不服了哦!!!
有问题欢迎联系本人或在下方评论区评论。