Index exceeds the number of array elements. Index must not exceed 101.

139 次查看(过去 30 天)
Please help me fix the following error: "Index exceeds the number of array elements. Index must not exceed 101." thank you very much!
%% Phase plane
clear all
clc
%% conditions
A=[-0.8 0.1;0.15 -0.15];
A_h=[-0.4 0.2;0.1 0.04];
A_d=[0.2 0.5;-0.3 0.05];
A_u=[0.1;-0.1];
A_c=[-76.0089 387.8927;75.3589 -387.9427];
stime=1.0;
endtime=30;
h=0.01;
t=-stime:h:endtime;
N_0=stime/h;
N_1=endtime/h;
N=N_0+N_1;
%bound=110;
for i=1:N+1
if i<=N_0+1;
x1(:,i)=0.05*sin((i-N_0-1)*h);
x2(:,i)=0.05*cos((i-N_0-1)*h);
else
d_1=floor(1.5+0.5*sin(1.5*i*h));
d_2=floor(2+cos(2.5*i*h));
if i<=4
x1(:,i)=x1(:,i-1)+h*(A_c(1,1)*x1(:,i-1)+A_c(1,2)*x2(:,i-1));
x2(:,i)=x2(:,i-1)+h*(A_c(2,1)*x1(:,i-1)+A_c(2,2)*x2(:,i-1));
else
x1(:,i)=x1(:,i-1)+h*(A_c(1,1)*x1(:,i-1)+A_c(1,2)*x2(:,i-1)+A_h(1,1)*x1(:,i-d_1-1)+A_h(1,2)*x2(:,i-d_1-1)+(1/2)*(A_d(1,1)*(x1(i-1)+x1(i-d_2-1)+2*(x1(i-d_2)+x1(i-d_2+1)))+A_d(1,2)*(x2(i-1)+x2(i-d_2-1)+2*(x2(i-d_2)+x2(i-d_2+1)))));
x2(:,i)=x2(:,i-1)+h*(A_c(2,1)*x1(:,i-1)+A_c(2,2)*x2(:,i-1)+A_h(2,1)*x1(:,i-d_1-1)+A_h(2,2)*x2(:,i-d_1-1)+(1/2)*(A_d(2,1)*(x1(i-1)+x1(i-d_2-1)+2*(x1(i-2)+x1(i-3)+x1(i-4)))+A_d(2,2)*(x2(i-1)+x2(i-d_2-1)+2*(x2(i-2)+x2(i-3)+x2(i-4)))));
end
end
end
%% Plotting Graphs
figure
grid on
hold on
box on
plot(t,x1(1,:),'-g',t,x2(1,:),'r-','linewidth',2.5)
hold on
xlabel('Time (t)')
%ylabel('State responses')
legend('x_{1}(t)','x_{2}(t)');
xlim([0,30]);
ylim([-100,100]);
%
  15 个评论
Torsten
Torsten 2024-8-3,19:35
So you didn't use this code to produce the graphics ?
sol = ddesd(@ddex1de,@ddex1delays,@ddex1hist,[0,30]);
plot(sol.x,[sol.y(1,:);sol.y(2,:)])
function d = ddex1delays(t,y)
d = [t-1.5-0.5*sin(1.5*t);...
t-2-cos(2*t)];
end
function h = ddex1hist(t)
h = [0.05*sin(t);...
0.05*cos(t);...
(cos(cos(2*t)-t+2)-cos(t))/30;...
(sin(cos(2*t)-t+2)+sin(t))/30];
end
function dydt = ddex1de(t,y,Z)
dydt = [-0.8*y(1)+0.1*y(2)-0.4*Z(1,1)+0.2*Z(2,1)+0.2*y(3)+0.5*y(4);...
0.15*y(1)-0.15*y(2)+0.1*Z(1,1)+0.04*Z(2,1)-0.3*y(3)+0.05*y(4);...
y(1)-Z(1,2)*(2*sin(2*t)+1);...
y(2)-Z(2,2)*(2*sin(2*t)+1)];
end
Obviously, code and graphics in your contribution don't fit together.
Umar
Umar 2024-8-3,20:05
编辑:Walter Roberson 2024-8-4,19:45
Hi @ Torsten,
Please feel more comfortable to glance through my screenshots as shown below. Hope, you will find something that I did not catch.
They are shared in this post.

请先登录,再进行评论。

回答(2 个)

Star Strider
Star Strider 2024-8-2,16:41
I am not certain how to get these into a form that the MATLAB ODE integrators could use, so I did the next best thing, and calculated the derivatives and then integrated them numerically.
Try this —
%% conditions
A=[-0.8 0.1;0.15 -0.15];
A_h=[-0.4 0.2;0.1 0.04];
A_d=[0.2 0.5;-0.3 0.05];
A_u=[0.1;-0.1];
d_1 = @(t) floor(1.5+0.5*sin(1.5*t));
d_2 = @(t) floor(2+cos(2.5*t));
x1 = @(t) 0.05*sin(t);
x2 = @(t) 0.05*cos(t);
x1_int = @(t,d) integral(@(t) x1(t), t-d_1(t), t, 'ArrayValued',1);
x2_int = @(t,d) integral(@(t) x2(t), t-d_2(t), t, 'ArrayValued',1);
tv = linspace(-3, 0, 250);
for k = 1:numel(tv)
t = tv(k);
xdot(:,k) = A*[x1(t); x2(t)] + A_h*[x1(t-d_1(t)); x2(t-d_2(t))] + A_d*[x1_int(t,d_1(t)); x2_int(t,d_2(t))];
end
xdot
xdot = 2x250
-0.0357 -0.0357 -0.0357 -0.0356 -0.0356 -0.0355 -0.0354 -0.0354 -0.0353 -0.0352 -0.0351 -0.0350 -0.0349 -0.0348 -0.0347 -0.0346 -0.0345 -0.0344 -0.0343 -0.0341 -0.0340 -0.0339 -0.0337 -0.0336 -0.0334 -0.0333 -0.0331 -0.0329 -0.0327 -0.0326 0.0029 0.0029 0.0028 0.0028 0.0028 0.0027 0.0027 0.0026 0.0026 0.0025 0.0025 0.0024 0.0024 0.0024 0.0023 0.0023 0.0022 0.0022 0.0021 0.0021 0.0020 0.0020 0.0019 0.0019 0.0018 0.0018 0.0017 0.0017 0.0016 0.0016
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
xm = cumtrapz(tv, xdot, 2)
xm = 2x250
0 -0.0004 -0.0009 -0.0013 -0.0017 -0.0021 -0.0026 -0.0030 -0.0034 -0.0039 -0.0043 -0.0047 -0.0051 -0.0055 -0.0060 -0.0064 -0.0068 -0.0072 -0.0076 -0.0080 -0.0084 -0.0089 -0.0093 -0.0097 -0.0101 -0.0105 -0.0109 -0.0113 -0.0117 -0.0121 0 0.0000 0.0001 0.0001 0.0001 0.0002 0.0002 0.0002 0.0003 0.0003 0.0003 0.0004 0.0004 0.0004 0.0004 0.0005 0.0005 0.0005 0.0005 0.0006 0.0006 0.0006 0.0006 0.0007 0.0007 0.0007 0.0007 0.0008 0.0008 0.0008
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
figure
yyaxis left
plot(tv, xdot(1,:))
ylabel('$\dot{x_1}(t)$', 'Interpreter','LaTeX')
yyaxis right
plot(tv, xdot(2,:))
ylabel('$\dot{x_2}(t)$', 'Interpreter','LaTeX')
grid
xlabel('Time')
figure
yyaxis left
plot(tv, xm(1,:))
ylabel('x_1(t)')
yyaxis right
plot(tv, xm(2,:))
ylabel('x_2(t)')
grid
xlabel('Time')
The derivatives look a bit strange to me, however you have access to the paper and know what it wants, so I defer to you for any necessary corrections.
I do not see anywhere in the excerpt from the paper where ‘A_c’ appears, so I did not use it.
.
  15 个评论
Le
Le 2024-8-5,5:29
Hi @Umar : Below is a code about state feedback controller.
clc
clear all;
close all;
%Closed-loop system
A=[0.5 1;0.45 0.85];
A_d=[0.04 0.1;0.1 0.2];
B=[1.5;1.3];
D=[0.5;0.4];
g=7;
K=[-0.3202 -0.7155];
A_c=A+B*K;
n=40;
v=zeros(1,n);
w=zeros(1,n);
Xh=zeros(n,n); Xv=zeros(n,n);U=zeros(n,n);W=zeros(n,n);
for i=1:1:10
Xh(1,i)=0;
end
for i=11:1:n-1
Xh(1,i)=0;
end
for i=1:1:10
Xv(i,1)=0;
end
for i=11:1:n-1
Xv(i,1)=0;
end
x1=zeros(2,1);x0=zeros(2,1);xt=zeros(2,1);xd=ones(2,1);
for i=1:1:n
v(1,i)=i;
w(1,i)=i;
end
for s=1:1:4
for i=s:1:4
x0(1,1)=Xh(s,i);x0(2,1)=Xv(s,i);
x1=(A_c*x0)+(D*(9*exp(-0.065*pi*(s+i))));
Xh(s+1,i)=x1(1,1);
Xv(s,i+1)=x1(2,1);
U(s,i)=K*x0;
W(s,i)=9*exp(-0.065*pi*(s+i));
end
end
for s=1:1:4
for i=5:1:n-1
x0(1,1)=Xh(s,i);x0(2,1)=Xv(s,i);
xt(1,1)=Xh(s,i-3); xt(2,1)=Xv(s,i-3);
x1=(A_c*x0)+(A_d*xt)+(D*(9*exp(-0.065*pi*(s+i))));
Xh(s+1,i)=x1(1,1);
Xv(s,i+1)=x1(2,1);
U(s,i)=K*x0;
end
end
for s=5:1:n-1
for i=s:1:n-1
x0(1,1)=Xh(s,i);
x0(2,1)=Xv(s,i);
xt(1,1)=Xh(s-3,i-3);
xt(2,1)=Xv(s-3,i-3);
x1=(A_c*x0)+(A_d*xt)+(D*(9*exp(-0.065*pi*(s+i))));
Xh(s+1,i)=x1(1,1);
Xv(s,i+1)=x1(2,1);
U(s,i)=K*x0;
end
end
for i=1:1:4
for s=i:1:4
x0(1,1)=Xh(s,i);x0(2,1)=Xv(s,i);
x1=(A_c*x0)+(D*(9*exp(-0.065*pi*(s+i))));
Xh(s+1,i)=x1(1,1);
Xv(s,i+1)=x1(2,1);
U(s,i)=K*x0;
end
end
for i=1:1:4
for s=5:1:n-1
x0(1,1)=Xh(s,i);
x0(2,1)=Xv(s,i);
xt(1,1)=Xh(s-3,i);
xt(2,1)=Xv(s-3,i);
x1=(A_c*x0)+(A_d*xt)+(D*(9*exp(-0.065*pi*(s+i))));
Xh(s+1,i)=x1(1,1);
Xv(s,i+1)=x1(2,1);
U(s,i)=K*x0;
end
end
for i=5:1:n-1
for s=i:1:n-1
x0(1,1)=Xh(s,i);
x0(2,1)=Xv(s,i);
xt(1,1)=Xh(s-3,i-3);
xt(2,1)=Xv(s-3,i-3);
x1=(A_c*x0)+(A_d*xt)+(D*(9*exp(-0.065*pi*(s+i))));
Xh(s+1,i)=x1(1,1);
Xv(s,i+1)=x1(2,1);
U(s,i)=K*x0;
end
end
figure(1)
surf(v,w,Xh)
xlabel('i');
ylabel('j');
zlabel('x^h(i,j)');
axis([0 40 0 40 -1 3])
mycolors = [0 1 1; 1 0 0; 1 0 0];
colormap(mycolors);
%set(gca,'FontSize',18);
figure(2)
surf(v,w,Xv)
xlabel('i');
ylabel('j');
zlabel('x^v(i,j)');
axis([0 40 0 40 -1 3])
mycolors = [1 1 0; 1 0 0; 1 0 0];
colormap(mycolors);
figure(3)
surf(v,w,U)
xlabel('i');
ylabel('j');
zlabel('u(i,j)');
axis([0 40 0 40 -3 1])
mycolors = [0 1 1; 1 1 0; 1 0 1];
colormap(mycolors);
Can you help me replace state feedback controller by event trigggered control u(i,j) =K[x^h(i_p,j);x^v(i,j_q)]?
Thank you very much!
Umar
Umar 2024-8-5,6:57

Hi @Le,

To adress your query regarding, “can you help me replace state feedback controller by event trigggered control u(i,j) =K[x^h(i_p,j);x^v(i,j_q)]?”

To replace the state feedback controller with event-triggered control in your given code, you need to make several modifications: first define the triggering conditions by assuming that the triggering condition is based on the difference between the current state and the previous state. Then, modify the control update only when the triggering condition is satisfied. This can be done by adding an if statement to check if the triggering condition is met. If it is, we will update the control action; otherwise, keep the previous control action. Since event-triggered control is being utilized, the state variables will be updated only when the control action is updated. Therefore, modifying the code to update the state variables accordingly. Here is your modified code:

% Closed-loop system

A = [0.5 1; 0.45 0.85];

A_d = [0.04 0.1; 0.1 0.2];

B = [1.5; 1.3];

D = [0.5; 0.4];

g = 7;

K = [-0.3202 -0.7155];

A_c = A + B * K;

n = 40;

v = zeros(1, n);

w = zeros(1, n);

Xh = zeros(n, n);

Xv = zeros(n, n);

U = zeros(n, n);

W = zeros(n, n);

prev_control = zeros(2, 1); % Initialize previous control action

for i = 1:1:10

    Xh(1, i) = 0;

end

for i = 11:1:n-1

    Xh(1, i) = 0;

end

for i = 1:1:10

    Xv(i, 1) = 0;

end

for i = 11:1:n-1

    Xv(i, 1) = 0;

end

x1 = zeros(2, 1);

x0 = zeros(2, 1);

xt = zeros(2, 1);

xd = ones(2, 1);

for i = 1:1:n

    v(1, i) = i;
    w(1, i) = i;

end

for s = 1:1:4

    for i = s:1:4
        x0(1, 1) = Xh(s, i);
        x0(2, 1) = Xv(s, i);
        x1 = (A_c * x0) + (D * (9 * exp(-0.065 * pi * (s + i))));
        Xh(s + 1, i) = x1(1, 1);
        Xv(s, i + 1) = x1(2, 1);
        % Check triggering condition and update control action
        if norm(x1 - prev_control) > threshold
            U(s, i) = K * x0;
            prev_control = U(s, i);
        else
            U(s, i) = prev_control;
        end
        W(s, i) = 9 * exp(-0.065 * pi * (s + i));
    end

end

After the last end statement, the rest of your code remains the same. So, in this modified code, you can see an if statement is added to check the triggering condition before updating the control action. The triggering condition is based on the difference between the current state x1 and the previous control action prev_control. If the difference exceeds a certain threshold, control action is updated, otherwise, you need to keep the previous control action. Please note that you need to define the threshold value based on your specific requirements. The threshold determines how much the state needs to change before triggering a control update. By implementing this event-triggered control, you can reduce the computational burden and improve the efficiency of the control system. The control action will only be updated when necessary, leading to better resource utilization and potentially improved system performance. I hope this helps! Let me know if you have any further questions.

请先登录,再进行评论。


Voss
Voss 2024-8-2,2:15
The terms x1(i-d_2+1) and x2(i-d_2+1) will cause that error because d_2 is 1.
  4 个评论
Le
Le 2024-8-2,4:31
You can use the method of keeping the d_2=floor(2+ (cos(2.5*i*h))) fixed and adjusting the
x1(:,i)=x1(:,i-1)+h*(A_c(1,1)*x1(:,i-1)+A_c(1,2)*x2(:,i-1)+A_h(1,1)*x1(:,i-d_1-1)+A_h(1,2)*x2(:,i-d_1-1)+(1/2)*(A_d(1,1)*(x1(i-1)+x1(i-d_2-1)+A_d(1,2)*(x2(i-1)+x2(i-d_2-1)))));
and
x2(:,i)=x2(:,i-1)+h*(A_c(2,1)*x1(:,i-1)+A_c(2,2)*x2(:,i-1)+A_h(2,1)*x1(:,i-d_1-1)+A_h(2,2)*x2(:,i-d_1-1)+(1/2)*(A_d(2,1)*(x1(i-1)+x1(i-d_2-1))+A_d(2,2)*(x2(i-1)+x2(i-d_2-1))));
??
Torsten
Torsten 2024-8-2,9:46
You can use the method of keeping the d_2=floor(2+ (cos(2.5*i*h))) fixed and adjusting the ...
@Voss asked for the correct adjustment. It's only you who can tell us.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by