How to calculate iteration of gauss siedel method in matlab - matlab

I want to calculate iĀ“the number of iteration of gauss siedel method in matlab
here is my code
alpha=1;
A=zeros(30,30);
A(1,1)=-(2+alpha);
A(1,2)=1;
for i=2:29
A(i,i-1)=1;
A(i,i)=-(2+alpha);
A(i,i+1)=1;
end
A(30,29)=1;
A(30,30)=-(2+alpha);
D=diag(diag(A));
R=A-D;
x=zeros(30,1);
list=[];
count=0;
for k=1:150
xkk= inv(L)*(b-(U*x));
count=count+1;
list(count,:)=xk;
if(norm(xkk-x)<=10^-5)
break;
end
x=xkk;
end
count
when I ran it I got cound=150 so it seems that count does not works good
How to correct it?
Thanks

Change the last part like this:
xkk= inv(L)*(b-(U*x));
e=norm(xkk-x);
while(e>10^-5)
xkk= inv(L)*(b-(U*x));
count=count+1;
list(count,:)=xk;
e=norm(xkk-x)
x=xkk;
end

Related

Loop doesn't evaluate correctly in MATLAB

I have a problem with a loop not working correctly for some reason, or maybe it is not the loop itself, but rather something I did not pay attention to. I have been trying to solve the issue for hours now, to no avail.
Here's the code:
syms t;
syms m(t);
Nq=3;
L(1,1)=1;
L(2,1)=2;
L(3,1)=3;
w(1,1)=0.2;
w(2,1)=0.1;
w(3,1)=0.7;
G=1;
for x=1:1:2*Nq
m0val(x)=w(1,1)*L(1,1).^(x-1) + w(2,1)*L(2,1).^(x-1) + w(3,1)*L(3,1 ).^(x-1);
end
ode=diff(m,t)==0;
cond = m(0)==m0val(1);
mf(1)=dsolve(ode,cond);
for x=2:1:2*Nq
ode=diff(m,t)==(x-1)*G*mf(x-1);
cond=m(0)==m0val(x);
mf(x)=dsolve(ode,cond);
end
z=1;
for y=0.2:0.2:1
for x=1:1:2*Nq
mfv(z,x)=subs(mf(x),t,y);
end
for x=1:1:2*Nq+1
P(x,1)=eq(x,1);
end
****for x=1:1:2*Nq+1
if x~=2*Nq+1
P(x,2)=((-1).^(x-1))*mfv(z,x);
else
P(x,2)=0;
end****
end
for y=3:1:2*Nq+1
for x=1:1:2*Nq+2-y
P(x,y)=P(1,y-1)*P(x+1,y-2)-P(1,y-2)*P(x+1,y-1);
end
end
alpha(1)=mfv(z,1);
for x=2:1:2*Nq
alpha(x)=P(1,x+1)/(P(1,x)*P(1,x-1));
end
a(1)=alpha(2);
for x=2:1:Nq
a(x)=alpha(2*x)+alpha(2*x-1);
end
for x=1:1:Nq-1
b(x)=-(alpha(2*x+1)*alpha(2*x)).^0.5;
end
for x=1:1:Nq
Jacobi(x,x)=a(x);
end
for x=1:1:Nq-1
Jacobi (x+1,x)=b(x);
Jacobi(x,x+1)=b(x);
end
[evec,eval]=eig(Jacobi);
for x=1:1:Nq
L(x,z+1)=eval(x,x);
w(x,z+1)=mfv(z,1)*evec(1,x).^2;
end
z=z+1;
end
The bit between * is where it's not working properly, because if I compute let's say P(2,2), it should be equal to (-1)^(1)*mfv(z,2) (where z=1 for the first run). It gives a value of 1 which is the value of P(1,2).
I run your code and I find the issue with P data type. P in your code is logic, therefore, it can not store double or so value.
I declare P before using it in the loop then the code works fine.
z=1;
P = zeros(2,2);% the add line
for y=0.2:0.2:1
for x=1:1:2*Nq
mfv(z,x)=subs(mf(x),t,y);
end
for x=1:1:2*Nq+1
P(x,1)=eq(x,1);
end
for x=1:1:2*Nq+1
if x~=2*Nq+1
P(x,2)=((-1).^(x-1))*mfv(z,x);
else
P(x,2)=0;
end
end

How can I change the path in a loop? MATLAB

i want to change the path of a file on each iteration in a loop. How can i do it?
for i=1:N
for j=1:M
image=imread('ORDENADOR/Sample001/img001-00001.png');
end
end
I want to change the Sample001 to Sample002,Sample003...until SampleN.Also i want to change img001-00001 until img001-M . Thank you very much
Consider making a cell array of images to read
imgnames = {'ORDENADOR/Sample001/img001-00001.png', ...
'ORDENADOR/Sample002/img001-00001.png' };
for i=1:length(imgnames)
image=imread(imgnames{i});
end
Alternatively, use num2str with format specifiers:
for i=1:N
for j=1:M
image=imread(['ORDENADOR/Sample' num2str(N,'%.3i') '/img001-' num2str(M,'%.5i') '.png');
end
end
for i=1:N
for j=1:M
image=imread(sprintf('ORDENADOR/Sample%03d/img%03d-%05d.png',i,i,j));
end
end

How to delete decimals that have repeating decimals

I'm working on a code able to work data from star of a public catalogue. I've already got arrays of every single variable that is available:
enter code here fid=fopen('000006+2553.txt','r');
i=1;
while 1
tline=fgetl(fid);
if ~ischar(tline), break, end
A{i}=tline;
i=i+1;
end
k=1;
for j=1:1:length(A)
if length(A{j}) > 50 && length(A{j})<=92
B{k}=A{j};
k=k+1;
end
end
m=1;
for l=1:1:length(B)
C=strread(B{l},'%s','delimiter',' ');
HJD(m)=str2num(C{1});
MAG_3(m)=str2num(C{2});
MAG_0(m)=str2num(C{3});
MAG_1(m)=str2num(C{4});
MAG_2(m)=str2num(C{5});
MAG_4(m)=str2num(C{6});
MER_3(m)=str2num(C{7});
MER_0(m)=str2num(C{8});
MER_1(m)=str2num(C{9});
MER_2(m)=str2num(C{10});
MER_4(m)=str2num(C{11});
FRAME(m)=str2num(C{13});
m=m+1;
end
My problem is, that some of the values in the arrays are repeating decimals, like 29.99999 and 99.99999....etc. Since this numbers are the result of saturation of the sensor, they are wrong data, and must be eliminated. Is there anyway I can tell MATLAB to delete this particular numbers? Any help would be appreciated.

While loop inside for loop in Matlab

I am trying to using a while loop inside a for loop in Matlab. The while loop will repeat the same action until it satifies some criteria. The outcome from the while loop is one iteration in the for loop. I am having a problem to get that correctly.
n=100;
for i=1:n
while b<0.5
x(i)=rand;
b=x(i);
end
end
I am not sure what i am doing wrongly.
Thanks
Approach the problem differently. There's no need to try again if rand doesn't give you the value you want. Just scale the result of rand to be in the range you want. This should do it:
x = 0.5 + 0.5*rand(1, 100);
With the example you showed, you have to initialize b or the while-statement cannot be evaluated when it is first called.
Do it inside the for-loop to avoid false positives after the first for-iteration:
n=100;
for ii=1:n
b = 0;
while b<0.5
x(ii)=rand;
b=x(ii);
end
end
Or, without b:
n=100;
x = zeros(1,100);
for ii=1:n
while x(ii)<0.5
x(ii)=rand;
end
end

How to count the number of iterations

I'm working with k-means on MATLAB. To process the valid cluster, it needs to do a looping until the cluster position doesn't change any more. The looping will show the iterations process.
I want to count how many looping/iteration happens on that clustering process. Here is the snippet of looping/iteration processing part:
while 1,
d=DistMatrix3(data,c); %// calculate the distance
[z,g]=min(d,[],2); %// set the matrix g group
if g==temp, %// if the iteration does not change anymore
break; %// stop the iteration
else
temp=g; %// copy the matrix to the temporary variable
end
for i=1:k
f=find(g==i);
if f %// calculate the new centroid
c(i,:)=mean(data(find(g==i),:),1);
end
end
end
All I know that I have to do is define the iteration variable, and then write the calculation part. But, where do I have to define the variable? And how?
All the answers will be so much appreciated.
Thank you.
A Matlab while-loop is executed until the expression is false. The general setup is like this:
while <expression>
<statement>
end
If you want to count the number of times the while loop was entered, the easiest way is to declare a variable outside the loop and incrementing it inside:
LoopCounter = 0;
while <expression>
<statement>
LoopCounter = LoopCounter + 1;
end
The question whether to increment the LoopCounter before or after the <statement> depends on whether you need it to access vector entries. In that case, it should be incremented before the <statement> because 0 is not a valid index in Matlab.
Define before your loop, update in your loop.
iterations=0;
while 1,
d=DistMatrix3(data,c); % calculate the distance
[z,g]=min(d,[],2); % set the matrix g group
if g==temp, % if the iteration doesn't change anymore
break; % stop the iteration
else
temp=g; % copy the matrix to the temporary variable
end
for i=1:k
f=find(g==i);
if f % calculate the new centroid
c(i,:)=mean(data(find(g==i),:),1);
end
end
iterations=iterations+1;
end
fprintf('Did %d iterations.\n',iterations);