MATLAB,非线性方程式的equationsToMatrix [英] MATLAB, equationsToMatrix for nonlinear equations

查看:944
本文介绍了MATLAB,非线性方程式的equationsToMatrix的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Symbolic Toolbox(R2016b,Windows)找到运动方程后,我具有以下形式:

After finding equations of motion using the Symbolic Toolbox (R2016b, Windows), I have the following form:

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

Mb.

现在,我需要将b分为科里奥利(Coriolis)和潜在条款,这样

Now, I need to separate b into Coriolis and Potential terms such that

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

如果我可以申请的话,将会非常方便

It would be extremely convenient if I could apply

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

,但是不幸的是,当b是非线性的时,它不会将qdot排除在外.我不在乎(实际上是有必要的),即使在剔除向量qdot之后,Cqqdot的函数.我试过coeffsfactor没有结果.

but unfortunately it will not factor out qdot when b is nonlinear. I do not care (and in fact it is necessary) that C is a function of q and qdot, even after factoring out the vector qdot. I have tried coeffs and factor without results.

谢谢.

推荐答案

发布我自己的解决方案,以便至少可以有一个答案... 此功能有效,但未经严格测试.它的工作方式与我在原始问题中建议的完全一样.随时对其进行重命名,以免与内置的MATLAB冲突.

Posting my own solution so there can be at least one answer... This function works, but it is not heavily tested. It works exactly as I suggested in my original question. Feel free to rename it so it doesn't conflict with the MATLAB builtin.

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

这篇关于MATLAB,非线性方程式的equationsToMatrix的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆