简体   繁体   English

MATLAB ode45 耦合矩阵 ODE

[英]MATLAB ode45 coupled matrix ODE

I have learned two coupled matrix ODEs for the linear quadratic tracking problem in optimal control, which are below:我已经学习了优化控制中线性二次跟踪问题的两个耦合矩阵 ODE,如下所示: 在此处输入图片说明

where在哪里

在此处输入图片说明

I am trying to write a MATLAB that solves the differential equations simultaneously.我正在尝试编写一个同时求解微分方程的 MATLAB。 Here is what I have so far:这是我到目前为止所拥有的:

function [dSdt dGdt] = mRiccati2(t, S, A, B, Q, R, G, r, h)
    k = 1+floor(t/h);
    S = reshape(S, size(A)); %Convert from "n^2"-by-1 to "n"-by-"n"
    dSdt = A'*S + S*A - S*B*inv(R)*B'*S + Q; %Determine derivative
    dGdt = -(A'- S*B*inv(R)*B')*G + Q*r(:,k);
    dSdt = dSdt(:); %Convert from "n"-by-"n" to "n^2"-by-1
end

And I try to call the function as我尝试将该函数称为

[T S G] = ode45(@(t, S, G)mRiccati2(t, S, A, B, Q, R, G, r, h), [0:h:Tfinal], S0, G0);

Unfortunately, I am getting this error:不幸的是,我收到此错误:

Not enough input arguments.

Error in HW5 (line 24)
[T S G] = ode45(@(t, S, G)mRiccati2(t, S, A, B, Q, R, G, r, h), [0:h:Tfinal], S0, G0);

Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:});   % ODE15I sets args{1} to yp0.

Error in ode45 (line 115)
  odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);

Error in HW5 (line 24)
[T S G] = ode45(@(t, S, G)mRiccati2(t, S, A, B, Q, R, G, r, h), [0:h:Tfinal], S0, G0);

Is there a general way to properly solve a coupled matrix ODE with ode45 ?有没有一种通用的方法可以用ode45正确求解耦合矩阵 ODE?

Matlab makes many small things to make it easier to get to nice results, but it is not intelligent. Matlab 做了很多小事情来更容易得到好的结果,但它并不智能。 You need to adapt your problem to the interfaces of Matlab, there is no automatic detection.您需要使您的问题适应 Matlab 的接口,没有自动检测。 So the function you give to ode45 needs to consume a flat array for the state and return one flat array for the derivatives.因此,您提供给 ode45 的函数需要为状态消耗一个平面数组,并为导数返回一个平面数组。 You already found the answer on how to do the transformation, you only need to carry it out to the end.您已经找到了如何进行转换的答案,您只需要将其执行到底即可。

function dXdt = mRiccati2(t, X, A, B, Q, R, r, h)
    k = 1+floor(t/h);
    n = size(A(1,:))
    X = reshape(X, [n+1,n]); %Convert from flat to matrix, first the rows of S, then G
    S = X(1:n,:);
    G = X(n+1,:);
    dSdt = A'*S + S*A - S*B*inv(R)*B'*S + Q; %Determine derivative
    dGdt = -(A'- S*B*inv(R)*B')*G + Q*r(:,k);
    dXdt = [ dSdt(:) dGdt(:) ]; %Convert from matrix objects to flat arrays
end

Then of course call the integrator accordingly, constructing the initial data as flat array from the initial matrices然后当然相应地调用积分器,从初始矩阵将初始数据构造为平面数组

[T X] = ode45(@(t, X)mRiccati2(t, X, A, B, Q, R, r, h), [0:h:Tfinal], [S0(:) G0(:) ]);

To use the result you need to reconstruct the matrices from the rows of X in the same way as done in the derivatives function.要使用结果,您需要以与在导数函数中所做的相同的方式从X的行重建矩阵。 You could make explicit helper functions out of it.你可以用它来制作显式的辅助函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM