is the "for loop" is wrong? what can be the solution?

1 Ansicht (letzte 30 Tage)
SAHIL SAHOO
SAHIL SAHOO am 20 Jul. 2022
Kommentiert: SAHIL SAHOO am 20 Jul. 2022
clc
ti = 0;
tf = 100E-4;
tspan=[ti tf];
o = 1E6;
tc = 70E-9;
tf = 240E-6;
a1 = 0.02;
a2 = 0.02;
P1 = 1;
P2 = 1;
k = 0.033;
l = 0.5;
f = @(t,y) [ ((y(2)-a1).*y(1)) + k.*(y(1).*cos(y(3))).*(2/tc);
(P1 - y(2).*(1+y(1)))./tf;
o - (k / tc) * 2 * sin(y(3));
];
[T,Y] = ode45(f,tspan,[1;1;0].*10E-3);
% this is the for loop, maybe this is wrong
Y(:,3) = -3:0.01:3;
U = zeros(length(k),1) ;
for i = 1:length(k)
U(i) = -o.*(Y(:,3)) - 2.*(k./tc).*cos(Y(:,3) - pi/2)
end
%plotting the graphs
plot(T,Y(:,3));
xlim([0 10E-5])
xlabel('t')
ylabel('phase difference')
legend('k = 0.033')
plot(Y(:,3),U)
  6 Kommentare
VBBV
VBBV am 20 Jul. 2022
The for loop code works well as shown in my answer below. which can also be done without a loop.
SAHIL SAHOO
SAHIL SAHOO am 20 Jul. 2022
yeah I mean plot (Y(:,3), U) and the graph is correct too.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

VBBV
VBBV am 20 Jul. 2022
Bearbeitet: VBBV am 20 Jul. 2022
clc
ti = 0;
tf = 100E-4;
tspan=[ti tf];
o = 1E6;
tc = 70E-9;
tf = 240E-6;
a1 = 0.02;
a2 = 0.02;
P1 = 1;
P2 = 1;
k = 0.033;
l = 0.5;
f = @(t,y) [ ((y(2)-a1).*y(1)) + k.*(y(1).*cos(y(3))).*(2/tc);
(P1 - y(2).*(1+y(1)))./tf;
o - (k / tc) * 2 * sin(y(3));
];
[T,Y] = ode45(f,tspan,[1;1;0].*10E-3);
% this is the for loop, maybe this is wrong ... yes this assignment is
% incorrect
Y(:,3) = linspace(-3,3,length(Y)); % change this
U = zeros(length(Y),1) ;
for i = 1:length(Y)
U(i) = -o.*(Y(i,3)) - 2.*(k./tc).*cos(Y(i,3) - pi/2); % also this
end
%plotting the graphs
figure(1)
plot(T,Y(:,2));
figure(2)
plot(T,U)
xlim([0 10E-5])
xlabel('t')
ylabel('phase difference')
legend('','k = 0.033')
Warning: Ignoring extra legend entries.
plot(T,U)
You can plot both U and T variables , but for loop needs modififcation
  2 Kommentare
VBBV
VBBV am 20 Jul. 2022
clc
ti = 0;
tf = 100E-4;
tspan=[ti tf];
o = 1E6;
tc = 70E-9;
tf = 240E-6;
a1 = 0.02;
a2 = 0.02;
P1 = 1;
P2 = 1;
k = 0.033;
l = 0.5;
f = @(t,y) [ ((y(2)-a1).*y(1)) + k.*(y(1).*cos(y(3))).*(2/tc);
(P1 - y(2).*(1+y(1)))./tf;
o - (k / tc) * 2 * sin(y(3));
];
[T,Y] = ode45(f,tspan,[1;1;0].*10E-3);
% this is the for loop, maybe this is wrong ... yes this assignment is
% incorrect
Y(:,3) = linspace(-3,3,length(Y)); % change this
U = zeros(length(Y),1) ;
for i = 1:length(Y)
U(i) = -o.*(Y(i,3)) - 2.*(k./tc).*cos(Y(i,3) - pi/2); % also this
end
%plotting the graphs
figure(1)
plot(Y(:,3),U); % i guess you are looking for this
i guess you are looking for this
SAHIL SAHOO
SAHIL SAHOO am 20 Jul. 2022
yes, this graph is correct that what i expected, without for loop I get a straight line.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Torsten
Torsten am 20 Jul. 2022
ti = 0;
tf = 100E-4;
tspan=[ti tf];
o = 1E6;
tc = 70E-9;
tf = 240E-6;
a1 = 0.02;
a2 = 0.02;
P1 = 1;
P2 = 1;
k = 0.033;
l = 0.5;
f = @(t,y) [ ((y(2)-a1).*y(1)) + k.*(y(1).*cos(y(3))).*(2/tc);
(P1 - y(2).*(1+y(1)))./tf;
o - (k / tc) * 2 * sin(y(3));
];
[T,Y] = ode45(f,tspan,[1;1;0].*10E-3);
% this is the for loop, maybe this is wrong
U = -o.*Y(:,3) - 2.*(k./tc).*cos(Y(:,3) - pi/2);
%plotting the graphs
plot(T,Y(:,3));
xlim([0 10E-5])
xlabel('t')
ylabel('phase difference')
legend('k = 0.033')
plot(Y(:,3),U)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by