# Matlab loop help

• Jul 20th 2008, 09:35 AM
j-lee00
Matlab loop help
Matlab loop help
Code:

```for x = 1:15   if  x < 5     y = 2*x - 5;       elseif  5 < x <10     y = 4*x;     elseif  10< x <15     y = x*x;               end; end; plot(x,y)```
Why doesnt this work?
• Jul 20th 2008, 10:34 AM
CaptainBlack
Quote:

Originally Posted by j-lee00
Matlab loop help
Code:

```for x = 1:15     if  x < 5     y = 2*x - 5;     elseif  5 < x <10     y = 4*x;     elseif  10< x <15     y = x*x;     end;   end;   plot(x,y)```
Why doesnt this work?

That depend on what you expect it to do. x and y are scalars when you get to the plot command, what do you want them to be?

RonL
• Jul 20th 2008, 10:47 AM
CaptainBlack
Quote:

Originally Posted by CaptainBlack
That depend on what you expect it to do. x and y are scalars when you get to the plot command, what do you want them to be?

RonL

One interpretaion of what you are trying to do would be:

Code:

```x = 1:15; y=zeros(1,15); y=y+(x<=5).*(-y+2*x-5); y=y+((5<x)&(x<=10)).*(-y+4*x); y=y+((10<x)&(x<=15)).*(-y+x.*x);   plot(x,y)```
RonL