Skip to content

MATLAB - 多项式除法

多项式除法是核心代数运算,用于简化多项式分数、寻找根以及分析控制系统中的传递函数(transfer functions)。在 MATLAB 中,主要的数值多项式除法函数是 deconv,它代表解卷积(deconvolution)——多项式乘法(conv)的逆运算。

deconv 函数将分子多项式(numerator polynomial)除以分母多项式(denominator polynomial),返回商(quotient)和余数(remainder)。

[quotient, remainder] = deconv(numerator_poly, denominator_poly)

其中 numerator_poly 和 denominator_poly 是按降幂排列的系数行向量。输出的 quotient 和 remainder 也是系数向量。

我们将 P(x) = 6x³ + 5x² + 4x + 3 除以 Q(x) = 2x + 1。

%% 定义分子和分母的系数向量
P = [6 5 4 3]; % 表示 6x³ + 5x² + 4x + 3
Q = [2 1]; % 表示 2x + 1
%% 执行多项式除法
[quotient, remainder] = deconv(P, Q);
%% 显示结果
disp('商系数:');
disp(quotient); % 预期:[3 1 1.5]
disp('余数系数:');
disp(remainder); % 预期:[0 0 0 1.5]
fprintf('结果: (%sx^2 + %sx + %s) 余数为 %s\n', ...
num2str(quotient(1)), num2str(quotient(2)), num2str(quotient(3)), ...
num2str(remainder(end)));
% 余数向量用前导零填充以匹配 P 的长度。

您可以使用基本关系:分子 = 商 * 分母 + 余数 来验证除法结果。在 MATLAB 中,这可以通过使用 conv 和 + 运算符来完成。

**专业提示:**在使用浮点数时,余数可能由于精度限制而不完全为零。检查它是否小于一个小的容差(例如,1e-12)是一个好的做法。

我们将 P(x) = x⁴ + 4x³ + 6x² + 4x + 1(即 (x+1)⁴)除以 Q(x) = x + 1。结果应该是 (x+1)³ 且余数为零。

%% 定义用于精确除法的多项式
P = [1 4 6 4 1]; % (x+1)^4
Q = [1 1]; % (x+1)
[quotient, remainder] = deconv(P, Q);
disp('商 (x+1)^3 系数:');
disp(quotient); % 预期:[1 3 3 1]
disp('余数系数:');
disp(remainder); % 预期:[0 0 0 0 0]
%% 验证步骤
% 重构原始多项式 P
reconstructed_P = conv(quotient, Q) + remainder;
% 检查重构结果是否接近原始值
is_verified = all(abs(P - reconstructed_P) < 1e-12);
if is_verified
disp('验证成功:P = conv(Q, Quotient) + Remainder');
else
disp('验证失败。');
end

对于分析工作,当精度至关重要且您希望使用表达式而不仅仅是系数时,符号数学工具箱(Symbolic Math Toolbox)是更好的选择。它执行精确的有理数运算。

% 这需要符号数学工具箱
try
%% 定义符号变量和多项式
syms x;
sym_P = x^4 + 3*x^2 + 1; % 分子
sym_Q = x^2 + x + 1; % 分母
%% `quorem` 函数执行符号除法
[sym_quotient, sym_remainder] = quorem(sym_P, sym_Q, x);
%% 显示符号结果
fprintf('符号商: %s\n', string(sym_quotient));
fprintf('符号余数: %s\n', string(sym_remainder));
catch ME
if strcmp(ME.identifier, 'MATLAB:UndefinedFunction')
disp('符号数学工具箱未安装。跳过此示例。');
else
rethrow(ME);
end
end

在控制理论(control theory)中,传递函数 G(s) = N(s) / D(s) 是两个多项式的比率。如果 N(s) 的次数高于 D(s)(一个非真传递函数,improper transfer function),deconv 可用于将其分离为真传递函数(proper transfer function)和一个前馈项(feed-forward term),这对于分析和仿真至关重要。