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

조회 수: 1 (최근 30일)
feeroz babu
feeroz babu 2020년 11월 18일
댓글: feeroz babu 2020년 11월 19일
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
Geoff Hayes
Geoff Hayes 2020년 11월 18일
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 2020년 11월 18일
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

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

채택된 답변

Geoff Hayes
Geoff Hayes 2020년 11월 18일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by