2017-02-14 102 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