My for loop is working but can't plot the out put

조회 수: 1 (최근 30일)
feeroz babu
feeroz babu 2020년 11월 16일
답변: Walter iacomacci 2020년 11월 16일
format long
l=1/2;
u=1/3;
I=[1,0,0;0,1,0;0,0,1];
A=[1,0,0;0,1,0;0,0,1];
%epsilon = input('Enter accuracy (Eps):');
%A=[6,3,1;8,7,5;3,6,2];
%AdjA= [-16,-1,27;0,9,-27;8,-22,18];
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 + A*((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 = norm(X_i)
n=[3:1:100];
end
plot(n,E)

채택된 답변

Mathieu NOE
Mathieu NOE 2020년 11월 16일
hello
your E remains a scalar in your code; I assumed it was supposed to be indexed (with i )
so my suggestion below :
format long
l=1/2;
u=1/3;
I=[1,0,0;0,1,0;0,0,1];
A=[1,0,0;0,1,0;0,0,1];
%epsilon = input('Enter accuracy (Eps):');
%A=[6,3,1;8,7,5;3,6,2];
%AdjA= [-16,-1,27;0,9,-27;8,-22,18];
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 + A*((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); % look here
end
n=[3:1:100];
% plot(n,E)
plot(n,E(n)) % and here

추가 답변 (1개)

Walter iacomacci
Walter iacomacci 2020년 11월 16일
Hi Feeroz, I see what you are trying to dom try this:
format long
l=1/2;
u=1/3;
I=[1,0,0;0,1,0;0,0,1];
A=[1,0,0;0,1,0;0,0,1];
%epsilon = input('Enter accuracy (Eps):');
%A=[6,3,1;8,7,5;3,6,2];
%AdjA= [-16,-1,27;0,9,-27;8,-22,18];
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)];
E=0;
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 + A*((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)
n=[0:1:100];
end
plot(n,E)
Basically I changed only 2 things, added a value for E as 0 before the for loop and added that little E(i) inside the loop which will store the value of norm(X_i) in E on the i position every iteration, if you don't include this you will have a flat value of E and thats why you can't see anything when you plot n vs e. Also changed n size since E and n must have equal sizes so you can plot that otherwise you will have plot error because sizes are different.

카테고리

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

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by