寻求Maxima中Matlab lsim函数的等效实现方案
lsim 的功能来复现控制系统仿真结果 Hey there! I get the frustration of trying to replicate Matlab's lsim behavior in Maxima—especially when you're aiming to match the official MathWorks example results. Let me walk you through the correct way to do this using the COMA library, since your initial approach (multiplying the transfer function by a square wave) isn't how system input-response calculations work.
1. 先搞定系统定义与库加载
First, make sure you've loaded the COMA library correctly, then define your transfer function to match the Matlab example. For reference, let's use the standard lsim example system: $G(s) = \frac{s^2 + 2s + 2}{s^3 + 4s^2 + 3s}$
load(coma); % 定义分子和分母多项式系数(按s的降幂排列) num : [1, 2, 2]; % 对应 s² + 2s + 2 den : [1, 4, 3, 0];% 对应 s³ + 4s² + 3s % 创建传递函数对象 sys : tf(num, den);
2. 生成输入信号(方波)
你的方波定义方向是对的,我们把它和标准时间向量对齐,匹配Matlab的采样逻辑:
% 创建时间向量:0到10秒,步长0.1秒 t : makelist(i*0.1, i, 0, 100); % 生成周期为2秒的方波输入,和Matlab示例一致 u : makelist((1 + signum(sin(%pi*ti)))/2, ti, t);
3. 直接使用COMA内置的lsim函数!
关键来了:COMA库其实自带了和Matlab同名的lsim函数,它会自动处理卷积、数值积分等底层逻辑,直接计算系统对输入的响应:
% 计算系统在输入u、时间t下的输出响应 [y, t_out] : lsim(sys, u, t);
4. 绘制结果
现在可以用plot2d可视化输入信号和系统响应:
% 绘制输入信号 plot2d([[t, u]], [xlabel, "Time (s)"], [ylabel, "Input u(t)"], [title, "Square Wave Input"], [style, linespoints]); % 绘制系统响应 plot2d([[t_out, y]], [xlabel, "Time (s)"], [ylabel, "Output y(t)"], [title, "System Response"], [style, linespoints]); % 也可以把输入和响应画在同一张图里对比 plot2d([[t, u], [t_out, y]], [xlabel, "Time (s)"], [legend, "Input u(t)", "Output y(t)"], [style, [lines, lines]], [color, [blue, red]]);
为什么你之前的方法行不通?
直接把传递函数和方波相乘是代数运算,不是系统仿真逻辑。计算系统对输入的响应,本质是求输入信号与系统脉冲响应的卷积,而lsim函数已经帮你封装好了这个过程,不需要手动实现。
只要系统参数、输入信号、时间范围和Matlab示例完全一致,你用Maxima得到的结果就会和MathWorks文档里的示例完全匹配。
内容的提问来源于stack exchange,提问作者Baratao00

