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

1 visualización (últimos 30 días)
SAHIL SAHOO
SAHIL SAHOO el 10 de Oct. de 2022
Comentada: SAHIL SAHOO el 11 de Oct. de 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);
dAdt = 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 ;
dAdt(i) = (Gt(i).*(At(i)));
dOdt(i) = -a.*(Gt(i));
for j = 1:N
dAdt(i) = dAdt(i)+yita_mn(i,j).*(At(j))*sin(Ot(j)-Ot(i));
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(2:3:3*N-1) = dAdt;
dy(3:3:3*N-0) = dOdt;
end

Respuestas (1)

Jeffrey Clark
Jeffrey Clark el 10 de Oct. de 2022
Editada: Jeffrey Clark el 10 de Oct. de 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);
  3 comentarios
Jeffrey Clark
Jeffrey Clark el 11 de Oct. de 2022
@SAHIL SAHOO, you askeda very specific question which is what I answered and showed the two lines added just after where Gt is defined. I suspect you are asking the more general question as to why you aren't getting the results you want? Please check that the code you pasted in your initial post is what you are running, when I reformatted it slightly and without adding my two lines the code debugger points out that the parameters for the call to rate_eq don't match the functions', and the function doesn't use its first or last parameter t and o (see attached your reformatted code). It is also unlikely that Gt(i) would ever be exactly the same as Gt(1) or Gt(5) since they are random numbers:
[T,Y]= ode45(@(t,y) rate_eq(t,y,yita_mn,N),tspan,y0);
function dy = rate_eq(t,y,yita_mn,N,o)
SAHIL SAHOO
SAHIL SAHOO el 11 de Oct. de 2022
i get coreect solution by using the n

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by