簡體   English   中英

MATLAB,用於非線性方程的 equationsToMatrix

[英]MATLAB, equationsToMatrix for nonlinear equations

使用符號工具箱(R2016b,Windows)找到運動方程后,我有以下形式:

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沒有結果。

謝謝。

發布我自己的解決方案,這樣至少可以有一個答案......這個功能有效,但沒有經過嚴格測試。 它的工作原理與我在原始問題中建議的完全一樣。 請隨意重命名它,以免與 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM