Error in my MATLAB code
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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)
size(B)
size(K)
% 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);
% 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
0 Kommentare
Antworten (1)
Torsten
am 30 Mai 2023
Bearbeitet: Torsten
am 30 Mai 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.
Siehe auch
Kategorien
Mehr zu State-Space Control Design and Estimation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!