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
相关推荐
危笑ioi1 小时前
helm部署skywalking链路追踪 java
java·开发语言·skywalking
静心观复2 小时前
Python 虚拟环境与 pipx 详解
开发语言·python
卷心菜狗2 小时前
Re.从零开始使用Python构建本地大模型网页智慧聊天机器人
开发语言·python·机器人
书到用时方恨少!2 小时前
Python NumPy 使用指南:科学计算的基石
开发语言·python·numpy
2501_933329552 小时前
技术深度拆解:Infoseek舆情系统的全链路架构与核心实现
开发语言·人工智能·分布式·架构
Chan163 小时前
MCP 开发实战:Git 信息查询 MCP 服务开发
java·开发语言·spring boot·git·spring·java-ee·intellij-idea
web前端进阶者3 小时前
Rust初学知识点快速记忆
开发语言·后端·rust
L-李俊漩3 小时前
荆华密算 面试题(大模型开发)
python
lucky九年3 小时前
GO语言模拟C++封装,继承,多态
开发语言·c++·golang