简体   繁体   English

如何告诉 GNU Octave Symbolic (SymPy) 有一个只有标量 1 的变量?

[英]How to tell GNU Octave Symbolic (SymPy) to have one variable with only scalar 1?

I'm using GNU Octave with Symbolic (SymPy) and I want all s^2 should have scalar 1 .我正在使用带有符号(SymPy)的 GNU Octave,我希望所有s^2都应该有标量1

% Load packet
pkg load symbolic

% Symbolic
syms Vin Vout dVout ddVout R C Rf R1

A = 1 + Rf/R1;
eq = Vin == Vout/A + R*C*1/A*dVout + R*(C*1/A*dVout + C*((1/A*dVout + R*C*1/A*ddVout)- dVout));

% More symbolic
syms U s Y
eq2 = subs(eq, {Vin, Vout, dVout, ddVout}, {U, Y, Y*s, Y*s^2});
eq3 = expand(eq2);

% Transfer function G = Y/U
G = simplify(solve(eq3, Y)/U)

Output: Output:

G = (sym)

                  R1 + Rf
  ----------------------------------------
   2  2     2
  C *R *R1*s  + 2*C*R*R1*s - C*R*Rf*s + R1

If you run that code above, you will get a transfer function G of second order.如果您运行上面的代码,您将获得二阶传输 function G But it's not on the second order standard form.但它不在二阶标准表格上。

The standard form requries that s^2 have a scalar one.标准形式要求s^2有一个标量。 Then I can compute the rest of greek letters.然后我可以计算希腊字母的 rest。

在此处输入图像描述

Question:问题:

Is it possible in GNU Octave Symbolic (SymPy) to tell that s^2 should have scalar 1?在 GNU Octave Symbolic (SymPy) 中是否可以告诉s^2应该有标量 1?

With symbolic computations there are many ways to achieve the same simplification.对于符号计算,有许多方法可以实现相同的简化。 Having said that, I'm not familiar with Octave.话虽如此,我对 Octave 并不熟悉。 Looking at the symbolic package documentation it appears that it lacks many many Sympy's function that are exposed on Python.查看符号 package 文档,它似乎缺少许多在 Python 上公开的 Sympy 的 function。 However, by looking at the package source code, it is relatively quick to implement them.但是,通过查看 package 源代码,实现它们相对较快。 So, we are going to implement a couple of them:因此,我们将实现其中的几个:

pkg load symbolic

function y = collect(expr, t)
% Collects common powers of a term in an expression.

  cmd = 'return sp.collect(*_ins),';

  y = pycall_sympy__ (cmd, expr, t);

end

function [num, den] = fraction(expr)
% Returns a pair with expression’s numerator and denominator.

  cmd = 'return sp.fraction(*_ins),';

  y = pycall_sympy__ (cmd, expr);
  num = y{1,1}
  den = y{1,2}

end

Now we can apply them to our use case:现在我们可以将它们应用到我们的用例中:

% retrieve the numerator and denominator of G
[num, den] = fraction(G)
% num = (sym) R₁ + Rf
% den = (sym)
% 
%    2  2     2
%   C ⋅R ⋅R₁⋅s  + 2⋅C⋅R⋅R₁⋅s - C⋅R⋅Rf⋅s + R₁

% Let's modify the denominator
den_mod = collect(expand(den / (C^2 * R^2 * R1)), s)
% In the previous command:
% 1. we divide "den" by (C^2 * R^2 * R1)
% 2. we expand the expression so that the division gets executed term by term.
%    At this points, the term containing "s^2" will have a coefficient of 1.
% 3. we collect the terms having a common "s".
% Output:
% den_mod = (sym)
% 
%    2     ⎛ 2      Rf  ⎞     1
%   s  + s⋅⎜─── - ──────⎟ + ─────
%          ⎝C⋅R   C⋅R⋅R₁⎠    2  2
%                           C ⋅R
%

% Now we simply reconstruct G. Since we divided "den" by (C^2 * R^2 * R1)
% we also have to divide "num" by the same amount
G_mod = num / (C^2 * R^2 * R1) / den_mod
% G_mod = (sym)
% 
%                   R₁ + Rf
%   ────────────────────────────────────────
%    2  2    ⎛ 2     ⎛ 2      Rf  ⎞     1  ⎞
%   C ⋅R ⋅R₁⋅ ⎜s  + s⋅⎜─── - ──────⎟ + ─────⎟
%            ⎜       ⎝C⋅R   C⋅R⋅R₁⎠    2  2⎟
%            ⎝                        C ⋅R ⎠

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM