def gen_30_point():"""生成30个随机数,3个为一组""" point_list =[]while len(point_list)< TOTAL: a = random.randint(0,10) b = random.randint(0,10)# 要求a必须大于b,生成随机数如果a小于等于b,则丢弃该组数据if a <= b:continue c = random.randint(0,10) point_list.append((a, b, c))return point_list
根据随机数计算出电荷量和电子质量
def calculate_q_m(point_list):"""根据a,b,c计算出q,m""" q_m_list =[]for a, b, c in point_list: q =(a - b)* ELECTRON_CHARGE
m =(a + c)* ELECTRON_QUALITY
q_m_list.append((q, m))return q_m_list
轻松计算出问题一的v
def calculate_v(q, m):"""计算出速度v""" v =2* q *(2/ PI + E **2- E)/ m
v = math.sqrt(v)return v
计算出问题二的R
def calculate_r(q, m, v):"""计算出半径R""" r = m * v /(2* B * q)return r
计算出问题三的x
def calculate_x(n, r):"""计算出x""" x =0if n > PI: x =2* r
elif n < PI /2: x =0else: x = r + r * math.sin(PI-n)+ r * math.cos(PI-n)/math.tan(PI-n)return x
"""
运行方法:
1. 安装依赖文件
pip3 install matplotlib -i https://pypi.tuna.tsinghua.edu.cn/simple
pip3 install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple
2. 运行代码
python3 physics_code.py
题目:
步骤一: (完成)
随机取30个点{a,b,c} a,b,c三个点都是0到10中随机取得
要求 a>b, 计算:q=a-b m=a+c
步骤二:求出V (完成)
步骤三:求出R (完成)
步骤四:求出X,取top3 (完成)
步骤五:画出轨迹 (完成)
"""import math
import random
import matplotlib.pyplot as plt
import numpy as np
TOTAL =30PI =3.14E =2.71B =10**-4T =2*10**-4ELECTRON_CHARGE =1.602*10**-19ELECTRON_QUALITY =1.672*10**-27def gen_30_point():"""生成30个随机数,3个为一组""" point_list =[]while len(point_list)< TOTAL: a = random.randint(0,10) b = random.randint(0,10)# 要求a必须大于b,生成随机数如果a小于等于b,则丢弃该组数据if a <= b:continue c = random.randint(0,10) point_list.append((a, b, c))return point_list
def calculate_q_m(point_list):"""根据a,b,c计算出q,m""" q_m_list =[]for a, b, c in point_list: q =(a - b)* ELECTRON_CHARGE
m =(a + c)* ELECTRON_QUALITY
q_m_list.append((q, m))return q_m_list
def calculate_v(q, m):"""计算出速度v""" v =2* q *(2/ PI + E **2- E)/ m
v = math.sqrt(v)return v
def calculate_r(q, m, v):"""计算出半径R""" r = m * v /(2* B * q)return r
def calculate_n(v, r):"""计算出n""" n = v * T / r
return n
def calculate_x(n, r):"""计算出x""" x =0if n > PI: x =2* r
elif n < PI /2: x =0else: x = r + r * math.sin(PI-n)+ r * math.cos(PI-n)/math.tan(PI-n)return x
def draw_track(n, r):# 设置圆的参数 radius = r # 半径 circle_center =(-r,2)# 圆心坐标,例如(2, 3)# 生成圆的x和y坐标 theta = np.linspace(0, n,100)# 生成0到2π的100个点 x = circle_center[0]+ radius * np.cos(theta) y = circle_center[1]+ radius * np.sin(theta)# 绘制圆 plt.plot(x, y,"k")# 标记圆心 plt.plot(circle_center[0], circle_center[1],'ko')# 蓝色圆点标记圆心# 设置坐标轴范围 plt.xlim(circle_center[0]- radius -2, circle_center[0]+ radius +2) plt.ylim(circle_center[1]- radius -2, circle_center[1]+ radius +2)# 直线 x = np.linspace(-30, x[-1],20) y =-x / math.tan(n)+ r * math.sin(n)+2- r / math.tan(n)+ r * math.cos(n)/ math.tan(n) plt.plot(x, y)# y = 2 横线 x = np.linspace(-4* r,2* r,10) y = np.array([2]* len(x)) plt.plot(x, y,"k:")# 坐标系 new_ticks = np.linspace(-5,5,11) plt.yticks(new_ticks) ax = plt.gca() ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') ax.xaxis.set_ticks_position('bottom') ax.spines['bottom'].set_position(('data',0)) ax.yaxis.set_ticks_position('left') ax.spines['left'].set_position(('data',0))# 隐藏掉y轴的0坐标,不然和x轴重了,不好看,0位于从下到上第6个 yticks = ax.yaxis.get_major_ticks() yticks[5].label1.set_visible(False)# 设置坐标轴标题 plt.title('electronics track')# 显示图形 plt.show()if __name__ =="__main__": point_list = gen_30_point() q_m_list = calculate_q_m(point_list) x_list =[]for q, m in q_m_list: v = calculate_v(q, m) r = calculate_r(q, m, v) n = calculate_n(v, r) x = calculate_x(n, r) x_list.append((x, n, r))print(f"q={q}, m={m}, v={v}, r={r}, n={n}, x={x}") x_list.sort(reverse=True, key=lambda i : i[0])for x, n, r in x_list[:3]:print(f"x中最大的三个值:{x}")for x, n, r in x_list[:3]: draw_track(n, r)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)