In a 'for loop' not getting difference of two consecutive iteration
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
feeroz babu
le 18 Nov 2020
Commenté : feeroz babu
le 19 Nov 2020
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 commentaires
Geoff Hayes
le 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?
Réponse acceptée
Geoff Hayes
le 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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!