p1项目system_model.py代码

python 复制代码
import random

class ThermalSystem:
    """
    Simple first-order thermal system:
    dT/dt = -(T - ambient)/tau + k * heater_power
    """

    def __init__(self, ambient_temp=20.0, tau=12.0, gain = 4.0, noise_std=0.2):
        self.temperature = ambient_temp
        self.ambient = ambient_temp
        self.tau = tau
        self.gain = gain
        self.noise_std = noise_std

    def update(self, heater_power: float, dt: float) -> float:
        # Add noise to simulate imperfect measurement (ADC noise)
        noise = random.gauss(0, self.noise_std)

        # Basic physics update
        dTdt = -(self.temperature - self.ambient) / self.tau + self.gain * heater_power
        self.temperature += dTdt * dt

        return self.temperature + noise

这个文件在模拟一件事:开加热器,温度会上升;不加热,温度会慢慢回到环境温度;测量时还带一点噪声。

PART1-注释说明

python 复制代码
"""
Simple first-order thermal system:
dT/dt = -(T - ambient)/tau + k * heater_power
"""

它写的是一个简单的一阶热系统模型:

意思是:温度变化速度 = 自然散热 + 加热器加热

PART2-初始化函数 __init__

python 复制代码
def __init__(self, ambient_temp=20.0, tau=12.0, gain = 4.0, noise_std=0.2):

ambient_temp=20.0 环境温度,默认 20℃

tau=12.0 tau 是时间常数,tau 越大,系统反应越慢,tau 越小,系统反应越快

gain=4.0 加热增益,意思是同样的加热功率,能带来多强的升温效果

noise_std=0.2 这是噪声强度的标准差,意思是测量值会随机抖动,抖动大小大概由这个参数决定

PART-3保存系统状态

python 复制代码
self.temperature = ambient_temp
self.ambient = ambient_temp
self.tau = tau
self.gain = gain
self.noise_std = noise_std

这几行是在把参数存进系统对象里。

self.temperature = ambient_temp 一开始系统温度就等于环境温度

PART4-函数update()

python 复制代码
def update(self, heater_power: float, dt: float) -> float:

这个函数的作用是:给系统一个加热功率,让它往前走 dt 秒,然后返回新的测量温度

1.加噪声

python 复制代码
noise = random.gauss(0, self.noise_std)

生成一个均值为 0、波动大小由 noise_std 决定的随机噪声

2.计算温度变化率

python 复制代码
dTdt = -(self.temperature - self.ambient) / self.tau + self.gain * heater_power

自然散热项 -(self.temperature - self.ambient) / self.tau 这部分表示:当前温度越高于环境温度,散热越明显

加热项 self.gain * heater_power 这部分表示:加热器开多大,就给系统多大的升温推动

3.更新温度

python 复制代码
self.temperature += dTdt * dt

新温度 = 旧温度 + 温度变化率 × 时间步长

4.返回带噪声的测量值

python 复制代码
return self.temperature + noise
相关推荐
你驴我几秒前
WhatsApp Cloud API 速率限制下的令牌桶与退避策略实践
网络·后端·python
AC赳赳老秦4 分钟前
司法公开数据采集应用:OpenClaw 抓取裁判文书公开信息,批量整理同类参考案例
java·大数据·python·数据挖掘·数据分析·php·openclaw
格林威30 分钟前
工业相机Chunk功能全解析:图像嵌入时间戳、编码器元数据(附堡盟C#代码)
开发语言·人工智能·数码相机·计算机视觉·c#·视觉检测·工业相机
小大宇1 小时前
Python 集成 Conda
开发语言·python·conda
还是奇怪1 小时前
Simon Willison 用 DSPy 优化 Datasette Agent 提示词:提示工程正在变成可测试的软件工程
java·开发语言·软件工程
初学者,亦行者1 小时前
利用Pyecharts绘制堆叠柱状图
python·信息可视化·数据分析
林泽毅1 小时前
PyTRIO:当强化学习不再需要本地GPU
人工智能·python·深度学习·机器学习
小陈phd2 小时前
QAnything 阅读优化策略05——检索
人工智能·python·机器学习
ruofu332 小时前
wsl端是py312,但是项目是py311, 如何下载py311的依赖包(wheels)
python·docker