简体   繁体   中英

MATLAB ode45 coupled matrix ODE

I have learned two coupled matrix ODEs for the linear quadratic tracking problem in optimal control, which are below: 在此处输入图片说明

where

在此处输入图片说明

I am trying to write a MATLAB that solves the differential equations simultaneously. 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 ?

Matlab makes many small things to make it easier to get to nice results, but it is not intelligent. You need to adapt your problem to the interfaces of Matlab, there is no automatic detection. So the function you give to ode45 needs to consume a flat array for the state and return one flat array for the derivatives. 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. You could make explicit helper functions out of it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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