2017-02-14 105 views
1

使用符号工具箱(R2016b,Windows)中找到运动方程后,我有以下形式:MATLAB,equationsToMatrix非线性方程组

M(q)*qddot = b(q,qdot) + u

Mb使用equationsToMatrix发现。

现在,我需要b分成科里奥利和潜在的条款,使得

M(q)*qddot + C(q,qdot)*qdot + G(q) = u

这将是非常方便的,如果我能申请

[C,G] = equationsToMatrix(b,qdot)

但不幸的是它不会当b是非线性时,系数为qdot。我不在乎(事实上有必要)Cqqdot的函数,即使在分解了矢量qdot之后。我试过coeffsfactor没有结果。

谢谢。

+2

你不能。非线性意味着“它不能用矩阵来描述”。 MATLABs'equationsToMatrix'清楚地告诉你“将**线性方程组**转换为矩阵形式” –

+0

我认为我会吸引这种评论...我知道文档说什么,我正在寻找解决方法或者不同的功能来做我所需要的。另外,它们绝对可以用矩阵来描述;该矩阵最终具有qdot项。 – abatea

+0

那么它不是一个数值矩阵,它的一个变量矩阵 –

回答

2

发布我自己的解决方案,以便至少有一个答案... 此功能可以工作,但它没有经过严格测试。它的工作原理与我在原始问题中的建议完全相同。随意重命名它,以免与MATLAB内建冲突。

function [A,b] = equationsToMatrix(eq, x) 
%EQUATIONSTOMATRIX equationsToMatrix for nonlinear equations 
% factors out the vector x from eq such that eq = Ax + b 
% eq does not need to be linear in x 
% eq must be a vector of equations, and x must be a vector of symbols 

assert(isa(eq,'sym'), 'Equations must be symbolic') 
assert(isa(x,'sym'), 'Vector x must be symbolic') 

n = numel(eq); 
m = numel(x); 

A = repmat(sym(0),n,m); 

for i = 1:n % loop through equations 
    [c,p] = coeffs(eq(i),x); % separate equation into coefficients and powers of x(1)...x(n) 
    for k = 1:numel(p) % loop through found powers/coefficients 
     for j = 1:m % loop through x(1)...x(n) 
      if has(p(k),x(j)) 
       % transfer term c(k)*p(k) into A, factoring out x(j) 
       A(i,j) = A(i,j) + c(k)*p(k)/x(j); 
       break % move on to next term c(k+1), p(k+1) 
      end 
     end 
    end 
end 

b = simplify(eq - A*x,'ignoreanalyticconstraints',true); % makes sure to fully cancel terms 

end