how can I use "for loop" in this to get a correct result?
1 次查看(过去 30 天)
显示 更早的评论
clc
%defining constant
ti = 0; %inital time
tf = 100E-4;% final time
tspan=[ti tf];
o = 2E5; % detuning frequency
tc = 30E-9; %photon life time in cavity
tf = 230E-6; %flouroscence lifetime
a1 = 0.1; %round trip loss
a2 = 0.1;
P1 = 0.2; %pump strenght
P2 = 0.2;
kc = 3E-3; %critical coupling strength
s = 0.17;
k = s.*kc;
%I = 1; %saturation intensity
V = 1;
% define function
%y(1) = A1
%y(2) = A2
%y(3) = G1
%y(4) = G2
%y(5) = phase difference
f = @(t,y) [
(((y(3)./V) - a1) * y(1) + k * y(2) * cos(y(5))) ./ tc;
(((y(4)./V) - a2) * y(2) + k * y(1) * cos(y(5))) ./ tc;
(P1 - ((y(3)./V) * (((abs(y(1)))^2 ./I) + 1))) / tf;
(P2 - ((y(4)./V) * (((abs(y(2)))^2 ./I) + 1))) / tf;
o - (k / tc) * (((y(1) ./ y(2)) + (y(2) ./ y(1))) * sin(y(5)));
];
I =1:1:100;
Vf = zeros(length(I),1) ;
for i = 1:length(I)
[T,Y] = ode45(f,tspan,[1;1;0;0;0].*10E-2);
Vf(i) = (2.*(((mean(Y(:,1).*Y(:,2).*cos(Y(:,5)))).^2 + (mean(Y(:,1).*Y(:,2).*sin(Y(:,5)))).^2).^0.5))/(mean((Y(:,1)).^2)+mean((Y(:,2)).^2));
end
disp(Vf)
in this how I should apply for loop to get Vf for the different value of I ?
0 个评论
回答(1 个)
Walter Roberson
2022-7-13
Move the assignment to f to before the
for i = 1:length(I)
Your call to ode45 within the loop depends upon f already having been defined.
You might as well delete the last lines
%initial consitions
[T,Y] = ode45(f,tspan,[1;1;0;0;0].*10E-2);
Note that your f value is independent of the value of i or I so every time through the for loop you are calculating exactly the same thing.
3 个评论
Torsten
2022-7-13
编辑:Torsten
2022-7-13
clc
%defining constant
ti = 0; %inital time
tf = 100E-4;% final time
tspan=[ti tf];
o = 2E5; % detuning frequency
tc = 30E-9; %photon life time in cavity
tf = 230E-6; %flouroscence lifetime
a1 = 0.1; %round trip loss
a2 = 0.1;
P1 = 0.2; %pump strenght
P2 = 0.2;
kc = 3E-3; %critical coupling strength
s = 0.17;
k = s.*kc;
%I = 1; %saturation intensity
V = 1;
% define function
%y(1) = A1
%y(2) = A2
%y(3) = G1
%y(4) = G2
%y(5) = phase difference
f = @(t,y,I) [
(((y(3)./V) - a1) * y(1) + k * y(2) * cos(y(5))) ./ tc;
(((y(4)./V) - a2) * y(2) + k * y(1) * cos(y(5))) ./ tc;
(P1 - ((y(3)./V) * (((abs(y(1)))^2 ./I) + 1))) / tf;
(P2 - ((y(4)./V) * (((abs(y(2)))^2 ./I) + 1))) / tf;
o - (k / tc) * (((y(1) ./ y(2)) + (y(2) ./ y(1))) * sin(y(5)));
];
I =1:1:100;
Vf = zeros(length(I),1) ;
for i = 1:length(I)
[T,Y] = ode45(@(t,y)f(t,y,I(i)),tspan,[1;1;0;0;0].*10E-2);
Vf(i) = (2.*(((mean(Y(:,1).*Y(:,2).*cos(Y(:,5)))).^2 + (mean(Y(:,1).*Y(:,2).*sin(Y(:,5)))).^2).^0.5))/(mean((Y(:,1)).^2)+mean((Y(:,2)).^2));
end
plot(I,Vf)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!