In a 'for loop' not getting difference of two consecutive iteration

1 Ansicht (letzte 30 Tage)
format long
l=1/2;
u=1/3;
I=[1,0,0;0,1,0;0,0,1];
A=[4,3,3;2,4,1;3,1,2];
B=det(A)*inv(A);
G_1=[1/3,0,0;0,1/2,0;0,0,1];
G_2=[1/4,0,0;0,1/5,0;0,0,1/6];
T=[1,0,0;0,1,0;0,0,1];
F = [1/2,0,0;0,1/2,0;0,0,1/2];
i=1;
x(1) =1;
y(1) =1;
z(1) =1;
X_i=[x(i);y(i);z(i)];
for i = 1:100
a_i = 1/i^(1/2);
t = 1/5;
Z_i = X_i-t*[(I-inv(I+l*G_1))*X_i + B*((I-inv(I+u*G_2)))*A*X_i];
i = i+1;
X_i = a_i*F*X_i+(1-a_i)*T*Z_i
E(i) = norm(X_i);
L(i) = ((x(i+1) - x(i))^3 + (y(i+1) - y(i))^3 + (z(i+1) - z(i))^3)^(1/3); % main problem is here
end
n=[2:1:100];
plot(n,E(n))
  2 Kommentare
Geoff Hayes
Geoff Hayes am 18 Nov. 2020
feeroz - where in the above code are you trying to get the difference between two consecutive iterations? How should this difference be calculated?
feeroz babu
feeroz babu am 18 Nov. 2020
I am runing this code in GNU Octave, version 4.2.2. See we get X_i = [x(i); y(i); z(i)] in each iteration. So I want L(i). When I run this code, I got
error: x(3): out of bound 1

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 18 Nov. 2020
feeroz - you never update your x, y, and z after their initialization, so perhaps you are intending to use X_i instead. Try the following which may be what you want
X_i = zeros(3,101);
X_i(:,1) = [1; 1; 1];
for i = 1:100
a_i = 1/i^(1/2);
t = 1/5;
Z_i = X_i(:,i)-t*[(I-inv(I+l*G_1))*X_i(:,i) + B*((I-inv(I+u*G_2)))*A*X_i(:,i)];
X_i(:,i+1) = a_i*F*X_i(:,i)+(1-a_i)*T*Z_i;
E(i) = norm(X_i(:,i+1));
L(i) = ((X_i(1,i+1) - X_i(1,i))^3 + (X_i(2,i+1) - X_i(2,i))^3 + (X_i(3,i+1) - X_i(3,i))^3)^(1/3); % main problem is here
end
In the above, we pre-size the X_i array which we updated on each iteration of the loop. We then use that instead of the x, y and z when updating L(i). Note that you could pre-size E and L too.

Weitere Antworten (0)

Kategorien

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

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by