Error in my MATLAB code

1 view (last 30 days)
Mostafa Sallam
Mostafa Sallam on 30 May 2023
Commented: Mostafa Sallam on 1 Jun 2023
Hello,
when I try to simulate the code:
% Define the communication topology
L = [1 -1 0 0;
0 0 0 0;
0 -1 1 0;
0 0 -1 1];
% Define the coupling strengths
c = [0.1; 0.15; 0.15; 0.2];
% Define the initial conditions
x0 = [0.1 0.2;
0.25 -0.05;
0.1 -0.15;
-0.25 0.3];
% Define the state space equations
A = zeros(8,8);
B = zeros(8,4);
C = [1 0 0 0;
0 1 0 0];
for i = 1:4
% Define the state equations for agent i
A((2*i-1):(2*i), (2*i-1):(2*i)) = [1/(1+x0(i,1)^2) 1;
-c(i)*cos(x0(i,1)-x0(i,2))*exp(-(x0(i,1))^2+(x0(i,2))^4) -c(i)*sin(x0(i,1)-x0(i,2))*exp(-(x0(i,1))^2+(x0(i,2))^4)];
% Define the input equation for agent i
B((2*i-1):(2*i), i) = [0; 1];
% Define the coupling equations for agent i
for j = 1:4
if L(i,j) == 1
A((2*i-1):(2*i), (2*j-1):(2*j)) = [-1 0;
0 -1];
elseif L(i,j) == -1
A((2*i-1):(2*i), (2*j-1):(2*j)) = [1 0;
0 1];
end
end
end
% Define the time span
tspan = [0 400];
% Define the initial state vector
x0_vec = reshape(x0', [8,1]);
% Define the weighting matrices Q and R
Q = eye(8);
R = eye(4);
% Compute the optimal feedback gain matrix K using LQR control
[K,~,~] = lqr(A,B,Q,R);
% Define the reference signal
yd = @(t) sin(t);
size(A)
ans = 1×2
8 8
size(B)
ans = 1×2
8 4
size(K)
ans = 1×2
4 8
% Simulate the closed-loop system with LQR control and reference signal
[t,x] = ode45(@(t,x) (A-B*K)*x + B*yd(t), tspan, x0_vec);
Error using odearguments
@(T,X)(A-B*K)*X+B*YD(T) must return a column vector.

Error in ode45 (line 107)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
% Reshape the output vector x into a matrix
x = reshape(x, [size(x,1), 2, 4]);
% Plot the results
figure;
for i = 1:4
subplot(2,2,i);
plot(t, squeeze(x(:,1,i)), t, yd(t), 'g--');
xlim([0 400]);
title(['Agent ', num2str(i)]);
xlabel('Time');
ylabel('State');
legend('x_1', 'y_d');
end
figure;
for i = 1:4
subplot(2,2,i);
plot(t, squeeze(x(:,2,i)), t, yd(t), 'g--');
xlim([0 400]);
title(['Agent ', num2str(i)]);
xlabel('Time');
ylabel('State');
legend('x_2', 'y_d');
end
it give me the following error:
Error using odearguments (line 93)
@(T,X)(A-B*K)*X+B*YD(T) must return a column vector.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in New_Model22 (line 59)
[t,x] = ode45(@(t,x) (A-B*K)*x + B*yd(t), tspan, x0_vec);
can anyone help me?
Thanks

Answers (1)

Torsten
Torsten on 30 May 2023
Edited: Torsten on 30 May 2023
Explicitly listing the sizes of the matrices involved (see above), I come to the conclusion that yd(t) must be a 4x1 column vector instead of a 1x1 scalar.
  3 Comments
Torsten
Torsten on 31 May 2023
Edited: Torsten on 31 May 2023
We don't know what your equations represent.
It's up to you to define yd(t) appropriately as a function that returns a 4x1 vector, e.g.
yd = @(t) sin(t)*ones(size(B,2),1)
Mostafa Sallam
Mostafa Sallam on 1 Jun 2023
Thanks so much

Sign in to comment.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!