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
相关推荐
tqs_123451 分钟前
值传递与引用传递
java·开发语言·python
YsyaaabB2 分钟前
Deep Agents 实战课程
python·langchain
金銀銅鐵3 分钟前
斐波那契数列中连续10项的和总是能被11整除吗?
python·数学
databook5 分钟前
用 Python 计算联合概率和条件概率
python·数据分析
一直C10 分钟前
Linux系统编程|信号进阶 + SystemV IPC(消息队列、共享内存)
linux·c语言·开发语言·算法·青少年编程·vim
小王C语言24 分钟前
QT 基础:QT SDK 下载、配置、新建项目、项目代码解释
开发语言·qt
葡萄成熟时 !39 分钟前
泛型知识笔记
开发语言·笔记·python
fengyangaiGEO40 分钟前
GEO 如何分析竞品的 AI 引用情况?
人工智能·python·信息可视化
新时代牛马1 小时前
SPL-TPL 链式启动:spl.c 与CONFIG_TPL 三阶段加载
c语言·开发语言
布莱克6051 小时前
链表详解:定义、作用、应用场景及与数组的区别(C/C++ 实战)
c语言·开发语言·c++·链表