"Joe " <jrwelle@hotmail.com> wrote in message <iqgvfk$rog$1@newscl01ah.mathworks.com>...
> I am trying to setup an iterative program over a range of values. For example I need to solve for a certain variable over a range of speeds. I am having issues with the loop competeing the entire range. For example, the program below only returns the value of the first i instead of through the entire range.
>
> xx=1:10;
> dx=0;
> for i=1:length(xx)
> x=2*xx(i);
>
> while dx<100
> x_new=2*x;
> dx=x_newx;
> x=x_new;
> end
> x_vec(i,1)=x_new;
> end
> x_vec
>
> x_vec =
>
> 256
> 256
> 256
> 256
> 256
> 256
> 256
> 256
> 256
> 256
Hello,
I believe you forgot to update dx to zero after your while loop. When than loops ends, dx has a certain value that is <100. At the next iteration of i, the while loop doesn't meet the criteria at all(dx<100) so x_new is not modified.
A couple of observations if I may :
don't use i as a loop variable ; try using ii or jj
preallocate x_vec ; x_vec=zeros(1,length(xx));
you don't need to write x_vec(i,1) ; x_vec(i) suffice.
use debugger to see where is your problem. In this case you don't actually need it, but in more complicated cases it can prove very helpful.
Regards,
Florin
