ode45 needs to return a column vector

21 次查看(过去 30 天)
I am trying to solve the Riccati equation below:
Q = [1 0;
0 1/2];
R = 1/4
A = [0 1;
2 -1];
B = [0;
1];
tspan = [5 0];
P0 = 0;
P = ode45(@(t,P) odefun(t,P,Q,A,B,R), tspan, P0);
function dPdt = odefun(t,P,Q,A,B,R)
dPdt = -Q - A.'*P - P*A + P*B*R.^(-1)*B.'*P;
end
But I am getting the error of
Error using odearguments (line 93)
@(T,P)ODEFUN(T,P,Q,A,B,R) must return a column vector.
Error in ode45 (line 106)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in problem_2 (line 11)
P = ode45(@(t,P) odefun(t,P,Q,A,B,R), tspan, P0);

回答(1 个)

Walter Roberson
Walter Roberson 2022-3-31
Q = [1 0;
0 1/2];
R = 1/4
R = 0.2500
A = [0 1;
2 -1];
B = [0;
1];
tspan = [5 0];
P0 = 0;
P = ode45(@(t,P) odefun(t,P,Q,A,B,R), tspan, P0);
ans = 1×2
2 2
Error using odearguments
@(T,P)ODEFUN(T,P,Q,A,B,R) must return a column vector.

Error in ode45 (line 107)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
function dPdt = odefun(t,P,Q,A,B,R)
dPdt = -Q - A.'*P - P*A + P*B*R.^(-1)*B.'*P;
size(dPdt)
end
Q is 2 x 2, so addition or subtraction of Q and something else is going to give you at least a 2 x 2
A is 2 x 2, P is a scalar (because P0 is a scalar), so A.'*P is going to be 2 x 2.
P is a scalar, A is 2 x 2, so P*A is going to be 2 x 2
P is a scalar, B is 2 x 1 sp P*B is 2 x 1. R is a scalar, so P*B*R.^(-1) is 2 x 1. B is 2 x 1 so B.' is 1 x 2, and (2 x 1) * (1 x 2) is 2 x 2. P is scalar, so 2 x 2 * scalar is 2 x 2.
Thus you have multiple terms all of which are 2 x 2.
Your ode function must return a column vector, and the column vector must have the same number of elements as your P0 -- so MATLAB is expecting your function to return a scalar, not 2 x 2. Even if you reshape the 2 x 2 into a column vector, that would give you 4 elements, which would not match the single boundary conditon P0 that you have.
  3 个评论
Walter Roberson
Walter Roberson 2022-4-1
Q = [1 0;
0 1/2];
R = 1/4
R = 0.2500
A = [0 1;
2 -1];
B = [0;
1];
tspan = [5 0];
P0 = zeros(2,2);
[t, P] = ode45(@(t,P) odefun(t,P,Q,A,B,R), tspan, P0);
plot(t, P)
legend({'1', '2', '3', '4'})
function dPdt = odefun(t,P,Q,A,B,R)
P = reshape(P, 2, 2);
dPdt = -Q - A.'*P - P*A + P*B*R.^(-1)*B.'*P;
dPdt = dPdt(:);
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by