From: Jessica Christy on
Without posting the whole code, I am having a problem with a while loop.
rel_err=1;
xerr=0.001;
xi=ones(1,l-1);
while rel_err>=xerr
for i=1:10;
x=T*xi'+C
rel_err=abs(xi-x')/abs(x')
xi=x';
end
end

The for loop runs its entire ten iterations, even though the while loop should stop it after four or five. What am I doing wrong? thanks in advance.
From: Jan Simon on
Dear Jessica!

> Without posting the whole code, I am having a problem with a while loop.
> rel_err=1;
> xerr=0.001;
> xi=ones(1,l-1);
> while rel_err>=xerr
> for i=1:10;
> x=T*xi'+C
> rel_err=abs(xi-x')/abs(x')
> xi=x';
> end
> end
>
> The for loop runs its entire ten iterations, even though the while loop should stop it after four or five. What am I doing wrong? thanks in advance.

Matlab is less intelligent as you assume.
The WHILE loop is checked only, when its personal END statement is reached, but not magically in each included line.
Try this:
rel_err=1;
xerr=0.001;
xi=ones(1,l-1);
for i=1:10;
x=T*xi'+C
rel_err=abs(xi-x')/abs(x')
if rel_err < xerr
break;
end
xi=x';
end

Good luck, Jan
From: Jessica Christy on
Thanks so much, Jan. That did it!

"Jan Simon" <matlab.THIS_YEAR(a)nMINUSsimon.de> wrote in message <i08ksd$a9q$1(a)fred.mathworks.com>...
> Dear Jessica!
>
> Matlab is less intelligent as you assume.
> The WHILE loop is checked only, when its personal END statement is reached, but not magically in each included line.
> Try this:
> rel_err=1;
> xerr=0.001;
> xi=ones(1,l-1);
> for i=1:10;
> x=T*xi'+C
> rel_err=abs(xi-x')/abs(x')
> if rel_err < xerr
> break;
> end
> xi=x';
> end
>
> Good luck, Jan