The Y(:,81) changes linearly with respect to T, I want to modify my code in such a way that when Y(:,81) reaches 2.5E-6 then after that it remains constant through out with T.

1 view (last 30 days)
ti = 0;
tf = 7E-12;
tspan=[ti tf];
k = 1E-6;
h = 1E-2;
y0= [(h)*rand(2,1); ((-3.14).*rand(1,1) + (3.14).*rand(1,1));
(h)*rand(2,1); ((-3.14).*rand(1,1) + (3.14).*rand(1,1));
(h)*rand(2,1); ((-3.14).*rand(1,1) + (3.14).*rand(1,1));
(h)*rand(2,1); ((-3.14).*rand(1,1) + (3.14).*rand(1,1));
(h)*rand(2,1); ((-3.14).*rand(1,1) + (3.14).*rand(1,1));
(h)*rand(2,1); ((-3.14).*rand(1,1) + (3.14).*rand(1,1));
(h)*rand(2,1); ((-3.14).*rand(1,1) + (3.14).*rand(1,1));
(h)*rand(2,1); ((-3.14).*rand(1,1) + (3.14).*rand(1,1));
(h)*rand(2,1); ((-3.14).*rand(1,1) + (3.14).*rand(1,1));
(h)*rand(2,1); ((-3.14).*rand(1,1) + (3.14).*rand(1,1));
(h)*rand(2,1); ((-3.14).*rand(1,1) + (3.14).*rand(1,1));
(h)*rand(2,1); ((-3.14).*rand(1,1) + (3.14).*rand(1,1));
(h)*rand(2,1); ((-3.14).*rand(1,1) + (3.14).*rand(1,1));
(h)*rand(2,1); ((-3.14).*rand(1,1) + (3.14).*rand(1,1));
(h)*rand(2,1); ((-3.14).*rand(1,1) + (3.14).*rand(1,1));
(h)*rand(2,1); ((-3.14).*rand(1,1) + (3.14).*rand(1,1));
(h)*rand(2,1); ((-3.14).*rand(1,1) + (3.14).*rand(1,1));
(h)*rand(2,1); ((-3.14).*rand(1,1) + (3.14).*rand(1,1));
(h)*rand(2,1); ((-3.14).*rand(1,1) + (3.14).*rand(1,1));
(h)*rand(2,1); ((-3.14).*rand(1,1) + (3.14).*rand(1,1));
((-3.14).*rand(20,1) + (3.14).*rand(20,1));
zeros(1,1);
];
yita_mn = [
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0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) + exp(i.*Y(:,30)) + exp(i.*Y(:,33)) ...
+ exp(i.*Y(:,36)) + exp(i.*Y(:,39)) +exp(i.*Y(:,42)) + exp(i.*Y(:,45)) + exp(i.*Y(:,48)) + exp(i.*Y(:,51)) + exp(i.*Y(:,54))+ exp(i.*Y(:,57)) + exp(i.*Y(:,60))));
figure(1)
plot(T,Y(:,81),'linewidth',1.5)
xlabel("Time(in units of t_{p})")
ylabel("coupling")
grid on
set(gca,'fontname','times New Roman','fontsize',18,'linewidth',1.8);
%since it evolving linearly, how should I chnage the code so when it
%reaches 2E-6 it will become constant with respect to T
function dy = rate_eq(t,y,yita_mn,N,o,k)
dy = zeros(4*N+1,1);
dGdt = zeros(N,1);
dAdt = zeros(N,1);
dOdt = zeros(N,1);
P = 0.20;
a = 0;
T = 2000;
tp = 1E-12;
Gt = y(1:3:3*N-2);
At = y(2:3:3*N-1);
Ot = y(3:3:3*N-0);
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) + o(1,i).*tp;
for j = 1:N
dAdt(i) = dAdt(i) + (y(81)).*yita_mn(i,j)*(At(j))*cos(Ot(j)-Ot(i));
dOdt(i) = dOdt(i) + (y(81)).*yita_mn(i,j)*((At(j)/At(i)))*sin(Ot(j)-Ot(i));
end
end
dy(1:3:3*N-2) = dGdt;
dy(2:3:3*N-1) = dAdt;
dy(3:3:3*N-0) = dOdt;
n1 = (1:20)';
n2 = circshift(n1,-1);
n61 = n1 +60;
n62 = circshift(n61,-1);
n80 = circshift(n61,1);
j2 = 3*(1:20)-1;
j5 = circshift(j2,-1);
j8 = circshift(j2,-2);
j59 = circshift(j2,1);
dy(n61) = (o(1,n2).' - o(1,n1).').*tp + a.*(Gt(n2) - Gt(n1)) - (y(81)).*(y(j2)./y(j5)).*sin(y(n61)) - (y(81)).*(y( j5)./y(j2)).*sin(y(n61)) + (y(81)).*(y(j8)./y(j5)).*sin(y(n62)) + (y(81)).*(y(j59)./y(j2)).*sin(y(n80));
dy(81) = k;
end

Answers (1)

Les Beckham
Les Beckham on 3 Jan 2023
Over 100 lines of code to draw a straight line?
This plot (with the maximum Y limit applied) can be made in 8 lines of code:
T = [0 2.5 7];
Y = T*1e-6;
Y(Y>2.5e-6) = 2.5e-6; % apply the limit
plot(T,Y,'linewidth',1.5)
xlabel("Time(in units of t_{p})")
ylabel("coupling")
grid on
set(gca,'fontname','times New Roman','fontsize',18,'linewidth',1.8);

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!