当前位置: 首页 > news >正文

Python打发无聊时光:3.实现简单电路的仿真

        看到这个标题肯定有人会问:好好的multisim、 proteus之类的专门电路仿真软件不用,非要写一个简陋的python程序来弄,是不是精神失常了。实际上,我也不知道为什么要这么干,前两篇文章是我实际项目中的一些探索,但是这个纯属突发奇想。

 第一步:装matplotlib库

pip install matplotlib

 第二步:复制并运行代码

        我设计了一个计算了"串联分压"电路中的总电流以及每个电阻上的电压降的程序,如下。

import matplotlib.pyplot as plt
import matplotlib.patches as patchesplt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = Falsedef simulate_series_circuit(V, resistances):"""模拟一个给定电压(V)和一系列电阻值的串联电路。"""# 计算总电阻R_total = sum(resistances)# 根据欧姆定律计算电流: V = I * RI = V / R_total if R_total > 0 else 0# 计算每个电阻上的电压降voltage_drops = [I * R for R in resistances]return I, voltage_dropsdef draw_circuit(resistances, voltage_drops, current):"""绘制电路图并显示电压降和电流。"""fig, ax = plt.subplots()# 创建电池图例battery = patches.Rectangle((1, -0.25), 0.2, 0.5, edgecolor='black', facecolor='grey', label='电源')ax.add_patch(battery)plt.text(1.1, 0, '电源', horizontalalignment='center', verticalalignment='center')# 绘制电阻并显示电压for i, (R, V) in enumerate(zip(resistances, voltage_drops)):resistor_x = 2 + i * 1.5resistor = patches.Rectangle((resistor_x, -0.25), 1, 0.5, edgecolor='black', facecolor='orange',label=f'电阻 R{i + 1}' if i == 0 else "")ax.add_patch(resistor)plt.text(resistor_x + 0.5, 0, f'{V:.2f}V', horizontalalignment='center', verticalalignment='center')# 绘制导线plt.plot([1.2, 2], [0, 0], color='black', label='导线')for i in range(len(resistances) - 1):plt.plot([3 + i * 1.5, 3.5 + i * 1.5], [0, 0], color='black')plt.plot([2 + len(resistances) * 1.5, 3 + len(resistances) * 1.5], [0, 0], color='black')# 绘制从电路末端返回电池的线路plt.plot([3 + len(resistances) * 1.5, 3 + len(resistances) * 1.5, 1], [0, -0.25, -0.25], color='black')# 添加电流标签plt.text(1.5 + len(resistances) * 1.5, 0.3, f'电流 = {current:.2f}A', horizontalalignment='center',verticalalignment='center')# 设置限制并关闭坐标轴ax.set_xlim(0, 4 + len(resistances) * 1.5)ax.set_ylim(-1, 1)plt.axis('off')# 显示图例handles, labels = ax.get_legend_handles_labels()plt.legend(handles, labels, loc='upper right')plt.show()# 输入参数
V = float(input("请输入电源电压 (伏特): "))
resistances = [float(x) for x in input("请输入电路中的电阻值 (欧姆),用空格分隔: ").split()]# 运行仿真
current, voltage_drops = simulate_series_circuit(V, resistances)# 绘制并显示电路
draw_circuit(resistances, voltage_drops, current)

第三步:输入总电压和每个电阻并观察运行结果

        在运行窗口输入总电压(例:220V),每个电阻(56Ω,78Ω,90Ω,100Ω)

        观察运行结果如下:

http://www.lryc.cn/news/301861.html

相关文章:

  • MyBatis-Plus:通用分页实体封装
  • MVC 、DDD(domain-driven design,软件主动学习业务)、中台、Java SPI(Service Provider Interface)
  • 添加环境变量
  • 学习Android的第十六天
  • 若依项目改造
  • 相机图像质量研究(34)常见问题总结:图像处理对成像的影响--拖影
  • 算法学习系列(三十五):贪心(杂)
  • 嵌入式面试:瑞芯微
  • 【性能测试】分布式压测之locust和Jmeter的使用
  • ABC341A-D题解
  • 计算机网络——07协议层次及服务模型
  • Netty Review - NIO空轮询及Netty的解决方案源码分析
  • PAM | 账户安全 | 管理
  • Leetcode 16-20题
  • 【开源训练数据集1】神经语言程式(NLP)项目的15 个开源训练数据集
  • 【AIGC】Stable Diffusion的ControlNet参数入门
  • 静态curl库编译与使用(c++)
  • element 表单提交图片(表单上传图片)
  • Android 15 第一个开发者预览版
  • anomalib1.0学习纪实-续1:增加新算法
  • Java+Vue+MySQL,国产动漫网站全栈升级
  • 机器人常用传感器分类及一般性要求
  • C++-opencv的imread、imshow、waitkey、namedWindow
  • 开源语音识别faster-whisper部署教程
  • 使用IntelliJ IDEA配置Maven (入门)
  • 汽车金融市场研究:预计2029年将达到482亿美元
  • 关于举办第十五届蓝桥杯大赛电子赛5G全网规划与建设赛项的通知
  • Vue3快速上手(七) ref和reactive对比
  • 8、内网安全-横向移动RDPKerberos攻击SPN扫描WinRMWinRS
  • 《数据结构与算法之美》读书笔记