From: Kenzo Mendoza on
In my M-file, I have the following:

function y = PSO_1(x)
if (x>=0) && (x<pi)
y=sin(x);
elseif (x>=pi) && (x<2*pi)
y=-(2*sin(x));
elseif ((x>=2*pi) && (x<3*pi))
y= 3*sin(x);
elseif (x>=3*pi) && (x<4*pi)
y= -(4*sin(x));
elseif (x>=4*pi) && (x<5*pi)
y= 5*sin(x);
elseif (x>=5*pi) && (x<6*pi)
y= -6*sin(x);
else
y=0;
end

and I want to plot a graph, from x=0 to x=6pi, accordingly to the function PSO_1

someone told me to use fplot, but to no luck.

Any response will be greatly appreacited
From: Walter Roberson on
Kenzo Mendoza wrote:
> In my M-file, I have the following:
>
> function y = PSO_1(x)
> if (x>=0) && (x<pi)
> y=sin(x);
> elseif (x>=pi) && (x<2*pi)
> y=-(2*sin(x));

Rewrite using logical indexing.

function y = PSO_1(x)

y = zeros(size(x));

Lv1 = (x >= 0 & x < pi);
y(Lv1) = sin(x(Lv1));

Lv2 = (x >= pi & x < 2*pi);
y(Lv2) = -2*sin(Lv2);

and so on.

Once you have done that, then PSO_1 will be fully vectorized, and you
can use

x = 0:STEPSIZE:6*pi;
y = PSO_1(y);

plot(x,y);
 | 
Pages: 1
Prev: matric vectorization
Next: saveas error