在matlab中求解矩阵DAE系统

时间:2016-10-13 23:05:51

标签: matlab matrix symbolic-math ode differential-equations

可以找到方程here。正如你所看到的那样,它是由8个标量方程组成的,它们接近3个矩阵方程。为了让Matlab知道方程是矩阵式的,我将变量时间相关的向量函数声明为:

syms t p1(t) p2(t) p3(t)
p(t) = symfun([p1(t);p2(t);p3(t)], t);
p = formula(p(t));  % allows indexing for vector p
% same goes for w(t) and m(t)...

已知的矩阵声明如下:

A = sym('A%d%d',[3 3]);
Fq = sym('Fq%d%d',[2 3]);
Im = diag(sym('Im%d%d',[1 3]));

现在可以根据guide

对系统进行建模
eqs = [diff(p) == A*w + Fq'*m,...
       diff(w) == -Im*p,...
       Fq*w == 0];
vars = [p; w; m];

此时,当我尝试减少索引(因为它等于2)时,我收到以下错误:

[DAEs,DAEvars] = reduceDAEIndex(eqs,vars);

Error using sym/reduceDAEIndex (line 95)
Expecting as many equations as variables.

如果我们将所有变量声明为标量,则不会出现错误:

syms A Im Fq real p(t) w(t) m(t)

引用symfun documentation(提示部分):

  

符号函数总是标量,因此,您无法索引函数。

然而,我很难相信不可能以矩阵方式解决这些方程式。显然,可以将它扩展为8个标量方程,但这里涉及的多体系统非常简单,目的是能够解决复杂的方程 - 因此问题是:是否有可能在Matlab中求解矩阵DAE,如果是这样的话 - 必须修复哪些才能使其正常工作?

聚苯乙烯。我对Matlab DAE求解器有另一个问题:我的模型的输入变量(已知系数函数)是时变的。就example而言,它们在所有领域都是不变的,但是对于我的问题,它们会及时改变。这个问题已经提出here。如果你提到它,如果你有任何解决方案,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

最后,我设法找到了这个问题的正确语法。我错误地将矩阵变量(例如AFq)视为单个实体。下面我介绍使用矩阵方法的代码并解决这个特定的DAE:

% Define symbolic variables.
q = sym('q%d',[3 1]);        % state variables
a = sym('a'); k = sym('k');  % constant parameters
t = sym('t','real');         % independent variable

% Define system variables and group them in vectors:
p1(t) = sym('p1(t)'); p2(t) = sym('p2(t)'); p3(t) = sym('p3(t)');
w1(t) = sym('w1(t)'); w2(t) = sym('w2(t)'); w3(t) = sym('w3(t)');
m1(t) = sym('m1(t)'); m2(t) = sym('m2(t)');
pvect = [p1(t); p2(t); p3(t)];
wvect = [w1(t); w2(t); w3(t)];
mvect = [m1(t); m2(t)];

% Define matrices:
mass = diag(sym('ms%d',[1 3]));
Fq = [0 -1 a;
      0  0 1];
A = [1  0  0;
     0  1  a;
     0  a -q(1)*a] * k;

% Define sets of equations and aggregate them into one set: 
set1 = diff(pvect,t) == A*wvect + Fq'*mvect;
set2 = mass*diff(wvect,t) == -pvect;
set3 = Fq*wvect == 0;
eqs = [set1; set2; set3];
% Close all system variables in one vector:
vars = [pvect; wvect; mvect];

% Reduce index of the system and remove redundnat equations:
[DAEs,DAEvars] = reduceDAEIndex(eqs,vars);
[DAEs,DAEvars] = reduceRedundancies(DAEs,DAEvars);
[M,F] = massMatrixForm(DAEs,DAEvars);

对于两个变量2x2p1(t),我们收到非常简单的w1(t) ODE。请记住,在减少冗余之后,我们摆脱了状态向量q中的所有元素。这意味着所有左变量(kmass(1,1))都不依赖于时间。如果系统中的某些变量存在时间依赖性,那么案件将难以解决。

% Replace symbolic variables with numeric ones:
M = odeFunction(M, DAEvars,mass(1,1));
F = odeFunction(F, DAEvars, k);
k = 2000; numericMass = 4;
F = @(t, Y) F(t, Y, k);
M = @(t, Y) M(t, Y, numericMass);

% set the solver:
opt = odeset('Mass', M); % Mass matrix of the system
TIME = [1; 0];           % Time boundaries of the simulation (backwards in time)
y0 = [1 0]';             % Initial conditions for left variables p1(t) and w1(t)

% Call the solver
[T, solution] = ode15s(F, TIME, y0, opt);
% Plot results
plot(T,solution(:,1),T,solution(:,2))