Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
For/While loop won't loop

Subject: For/While loop won't loop

From: Joe

Date: 12 May, 2011 15:48:04

Message: 1 of 4

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_new-x;
    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

Subject: For/While loop won't loop

From: Florin Neacsu

Date: 12 May, 2011 16:04:05

Message: 2 of 4

"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_new-x;
> 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
pre-allocate 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

Subject: For/While loop won't loop

From: alistair templeton

Date: 12 May, 2011 16:11:23

Message: 3 of 4

> dx=0;
> for i=1:length(xx)
> x=2*xx(i);
>
> while dx<100
> x_new=2*x;
> dx=x_new-x;
> x=x_new;
> end
> x_vec(i,1)=x_new;
> end

you need to reset dx before the while loop each time.

after the first iteration of your for loop, dx will already be over 100 so it skips the while entirely.

Subject: For/While loop won't loop

From: Florin Neacsu

Date: 12 May, 2011 16:11:23

Message: 4 of 4


> 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.

EDIT : dx>=100 at the end of the while loop

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us