how can I use "for loop" in this to get a correct result?

조회 수: 1 (최근 30일)
SAHIL SAHOO
SAHIL SAHOO 2022년 7월 13일
댓글: SAHIL SAHOO 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) [
(((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
Unrecognized function or variable 'I'.

Error in solution (line 31)
(P1 - ((y(3)./V) * (((abs(y(1)))^2 ./I) + 1))) / tf;

Error in odearguments (line 92)
f0 = ode(t0,y0,args{:}); % ODE15I sets args{1} to yp0.

Error in ode45 (line 107)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
disp(Vf)
in this how I should apply for loop to get Vf for the different value of I ?

답변 (1개)

Walter Roberson
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
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)
SAHIL SAHOO
SAHIL SAHOO 2022년 7월 13일
ohh okay I got it, thanks

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by