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
相关推荐
ice81303318110 小时前
【Python】Matplotlib折线图绘制
开发语言·python·matplotlib
三品吉他手会点灯10 小时前
C语言学习笔记 - 44.运算符和表达式 - 运算符2 - 除法与取余运算符
c语言·开发语言·笔记·算法
copyer_xyf10 小时前
Python venv 虚拟环境
前端·后端·python
kkeeper~10 小时前
0基础C语言积跬步之动态内存管理
c语言·开发语言
橘右今10 小时前
2026 Java后端高频面试宝典
java·开发语言·面试
微小冷10 小时前
Julia卫星工具箱SatelliteToolbox简介
开发语言·航天·坐标转换·julia·卫星工具箱
2601_colin11 小时前
Codex插件全流程实战指南
开发语言·经验分享·笔记·微信开放平台
林爷万福11 小时前
GitHub 开源光谱数据处理项目推荐
python·光纤光谱仪
Song_da_da_11 小时前
C#与VisionPro联合编程实战:机器视觉二次开发完整指南
开发语言·microsoft·c#
xyzzklk11 小时前
解决Salesforce无法向外发送邮件
android·java·开发语言·网络·crm·salesforce·客户关系管理