Thread Subject: regarding for loop

Subject: regarding for loop

From: Ana

Date: 6 Nov, 2009 19:19:03

Message: 1 of 4

hi there,

I have a question regarding this for loop;
x = 0;
f1 = 100e03;
alpha = 0.055;
for t = 0:0.1:20,
    x = (exp(-alpha*t))*(cos((2*pi*f1*t)));
    plot(t,x)
end

Note that if i do x(t)=... then i will have the error of;??? Subscript indices must either be real positive integers or logicals.

Why is it that I have no errors but he image is still not being plotted, well infact when i enter variable t it is just one value and even x, so I guess that s why...I just cannot see what i have wrong.. thanks alot for the help.

regards,
Ana

Subject: regarding for loop

From: ImageAnalyst

Date: 6 Nov, 2009 19:45:49

Message: 2 of 4

On Nov 6, 2:19 pm, "Ana " <filter_wo...@yahoo.com> wrote:
> hi there,
>
> I have a question regarding this for loop;
> x = 0;
> f1 = 100e03;
> alpha = 0.055;
> for t = 0:0.1:20,
>     x = (exp(-alpha*t))*(cos((2*pi*f1*t)));
>     plot(t,x)
> end
>
> Note that if i do x(t)=... then i will have the error of;??? Subscript indices must either be real positive integers or logicals.
>
> Why is it that I have no errors but he image is still not being plotted, well infact when i enter variable t it is just one value and even x, so I guess that s why...I just cannot see what i have wrong.. thanks alot for the help.
>
> regards,
> Ana

----------------------------------------------------------------------
Ana:
Try this:

alpha = 0.055;
t = 0:0.1:20;
x = (exp(-alpha*t)).*(cos((2*pi*f1*t)));
plot(t,x);


Note the dot star instead of star in the equation for x. I also got
rid of the for loop.

Subject: regarding for loop

From: someone

Date: 6 Nov, 2009 20:36:21

Message: 3 of 4

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <5c258625-04da-41ac-ac3c-dd34d7adaf22@o10g2000yqa.googlegroups.com>...
> On Nov 6, 2:19?pm, "Ana " <filter_wo...@yahoo.com> wrote:
> > hi there,
> >
> > I have a question regarding this for loop;
> > x = 0;
> > f1 = 100e03;
> > alpha = 0.055;
> > for t = 0:0.1:20,
> > ? ? x = (exp(-alpha*t))*(cos((2*pi*f1*t)));
> > ? ? plot(t,x)
> > end
> >
> > Note that if i do x(t)=... then i will have the error of;??? Subscript indices must either be real positive integers or logicals.
> >
> > Why is it that I have no errors but he image is still not being plotted, well infact when i enter variable t it is just one value and even x, so I guess that s why...I just cannot see what i have wrong.. thanks alot for the help.
> >
> > regards,
> > Ana
>
> ----------------------------------------------------------------------
> Ana:
> Try this:
>
> alpha = 0.055;
> t = 0:0.1:20;
> x = (exp(-alpha*t)).*(cos((2*pi*f1*t)));
> plot(t,x);
>
>
> Note the dot star instead of star in the equation for x. I also got
> rid of the for loop.

ImageAnalyst gave you the efficient way of solving your problem.
But, if you are curious as to what you were doing wrong, it is
that you were overwring the value of x (a scalar) in every
iteration of your loop (you weren't incrementing the index).

To use the inefficient loop method, you should have done something like:

indx = 0;
f1 = 100e03;
alpha = 0.055;
for t = 0:0.1:20
    indx = indx + 1;
    x(indx) = (exp(-alpha*t))*(cos((2*pi*f1*t)));
end
plot(t,x)

The loop method would be more efficient if you preallocated x first.
(With something like x = zeros(1,200); before the loop.)
But ImageAnalyst's vectorized method is still the way to go.

Subject: regarding for loop

From: Ana

Date: 7 Nov, 2009 18:22:01

Message: 4 of 4

Hi,

Thanks loads for the help. Very helpful .

Regards,
Ana

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us at files@mathworks.com