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