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
相关推荐
itzixiao1 分钟前
L1-051 打折(5分)[java][python]
java·python·算法
Tiger_shl10 分钟前
C# 托管对象、非托管对象 讲解
开发语言·c#
HappyAcmen10 分钟前
10.常见报错排查与基础调试
开发语言·python
山川而川-R14 分钟前
Windows新系统_安装anaconda-2026-4.24
python
ID_1800790547318 分钟前
Python 实现京东商品详情 API 数据准确性校验(极简可直接用)
java·前端·python
码农的神经元19 分钟前
配电网智能决策平台:从风险感知到自愈控制的 Python 实现
开发语言·python
xlq2232226 分钟前
46.线程池
linux·开发语言
LF男男29 分钟前
Action- C# 内置的委托类型
java·开发语言·c#
记录无知岁月30 分钟前
【C/C++】头文件包含问题分析
c语言·开发语言·c++
zhaoshuzhaoshu35 分钟前
主流 AI 编程助手工具特点与对比
人工智能·python