# i want to add a loop so that it works like this.

2 views (last 30 days)
SAHIL SAHOO on 10 Oct 2022
Commented: SAHIL SAHOO on 11 Oct 2022
ti = 0;
tf = 1E-3;
tspan=[ti tf];
KC = 4E-3;
y0= [ (10e-6)*rand(2,1); ((-3.14).*rand(1,1) + (3.14).*rand(1,1));
(10e-6)*rand(2,1); ((-3.14).*rand(1,1) + (3.14).*rand(1,1));
(10e-6)*rand(2,1); ((-3.14).*rand(1,1) + (3.14).*rand(1,1));
(10e-6)*rand(2,1); ((-3.14).*rand(1,1) + (3.14).*rand(1,1));
(10e-6)*rand(2,1); ((-3.14).*rand(1,1) + (3.14).*rand(1,1));
((-3.14).*rand(5,1) + (3.14).*rand(5,1))];
yita_mn = [
0 1 0 0 1;
1 0 1 0 0;
0 1 0 1 0;
0 0 1 0 1;
1 0 0 1 0;
]*(KC);
N = 5;
t = 1E-3;
[T,Y]= ode45(@(t,y) rate_eq(t,y,yita_mn,N),tspan,y0);
figure(1)
plot(T./t,(Y(:,16)),'linewidth',0.8);
hold on
for m = 16:20
plot(T./t,(Y(:,m)),'linewidth',0.8);
end
hold off
grid on
xlabel("time")
ylabel("phase difference")
set(gca,'fontname','times New Roman','fontsize',18,'linewidth',1.8);
function dy = rate_eq(t,y,yita_mn,N,o)
dy = zeros(4*N,1);
dGdt = zeros(N,1);
dOdt = zeros(N,1);
P = 0.05;
a = 5;
T = 2E3;
Gt = y(1:3:3*N-2);
At = y(2:3:3*N-1);
Ot = y(3:3:3*N-0);
k = 4E-3;
for i = 1:N
dGdt(i) = (P - Gt(i) - (1 + 2.*Gt(i)).*(At(i))^2)./T ;
dOdt(i) = -a.*(Gt(i));
for j = 1:N
dOdt(i) = dOdt(i)+yita_mn(i,j).*((At(j)/At(i)))*cos(Ot(j)-Ot(i));
end
n1 = (1:5)';
n2 = circshift(n1,-1);
n16 = n1 + 15;
n17 = circshift(n16,-1);
n20 = circshift(n16,1);
j2 = 3*(1:5)-1;
j5 = circshift(j2,-1);
j8 = circshift(j2,-2);
j19 = circshift(j2,1);
%%%%%%%%%%%%%%%%%%%%%%%%%%% here i wanted to add a loop so that when
% Gt(i) = Gt(1) then the Gt(i+1) = 2 and when the Gt(i) = Gt(5) then the Gt(i+1) = Gt(1)
%i dont know how to addd this loop pelase help me in it.
dy(n16) = -a.*(Gt(i+1) -Gt(i)) - (k).*(y(j2)./y(j5)).*sin(y(n16)) - (k).*(y( j5)./y(j2)).*sin(y(n16)) + (k).*(y(j8)./y(j5)).*sin(y(n17)) + (k).*(y(j19)./y(j2)).*sin(y(n20));
end
dy(1:3:3*N-2) = dGdt;
dy(3:3:3*N-0) = dOdt;
end

Jeffrey Clark on 10 Oct 2022
Edited: Jeffrey Clark on 10 Oct 2022
@SAHIL SAHOO, since Gt(1) is not changed after the initial setting from the y input, and Gt(i+1) is not refereced before where you say you want to change Gt(i+1), you can just set Gt(2:end) before the i loop:
Gt = y(1:3:3*N-2);
% Gt(i) = Gt(1) then the Gt(i+1) = 2 and when the Gt(i) = Gt(5) then the Gt(i+1) = Gt(1)
Gt([false Gt(1:end-1)==Gt(1)]) = 2;
Gt([false Gt(1:end-1)==Gt(5)]) = Gt(1);
SAHIL SAHOO on 11 Oct 2022
i get coreect solution by using the n