# Thread: [SOLVED] Matlab plotting help:peicewise with loops

1. ## [SOLVED] Matlab plotting help:peicewise with loops

so i've tried to plot this funtion multiple times using every trick i can come up with and the loop still only goes through the first ifelse statement in the function file. here is a copy of my function file:

Code:
function y=f(x)
if x<=-1;
y=15;
elseif -1<=x<=1;
y=-5.*x+10;
elseif 1<=x<=3;
y=-10.*x.^2+35.*x-20;
elseif 3<=x<=4;
y=-5.*x+10;
elseif 4<=x<=5 ;
y=-10;
end
end
using this file I try to plot in my main file which is:

Code:
delete g228x07.txt; diary g228x07.txt
clear; clc; close all; echo on
% Gilat 228/7
x=[-2:.01:5];
n=length(x);
for i=1:n;
y(i)=f(x(i));

end
plot(x,y);
grid on

echo off; diary off
the first three lines are just a reference header and do not impact the problem. I don't know if the loop fails in the for loop or in the ifelse on the function file.

2. The problem is here:

Code:
-1<=x<=1
when x=2 that condition is still returning true. The way I usually write these expressions is shown below (im sure there is probably a better way). Try paste this code inside a file called "mhfFoo.m" and run is. It work OK on my computer.

Code:
function mhfFoo
x=[-2:0.1:5];
n=length(x);
for i=1:n;
y(i)=f(x(i));
end
plot(x,y);
grid on
end
function y=f(x)
if x<=-1;
y=15;
elseif x>=-1 && x<=1;
y=-5.*x+10;
elseif x>=1 && x<=3;
y=-10.*x.^2+35.*x-20;
elseif x>=3 && x<=4;
y=-5.*x+10;
elseif x>=4 && x<=5 ;
y=-10;
end
end
Regards Elbarto

3. thanks a lot. Its wierd that the code has to be like that but it worked.

4. Originally Posted by apocolypto
so i've tried to plot this funtion multiple times using every trick i can come up with and the loop still only goes through the first ifelse statement in the function file. here is a copy of my function file:

Code:
function y=f(x)
if x<=-1;
y=15;
elseif -1<=x<=1;
y=-5.*x+10;
elseif 1<=x<=3;
y=-10.*x.^2+35.*x-20;
elseif 3<=x<=4;
y=-5.*x+10;
elseif 4<=x<=5 ;
y=-10;
end
end
using this file I try to plot in my main file which is:

Code:
delete g228x07.txt; diary g228x07.txt
clear; clc; close all; echo on
% Gilat 228/7
x=[-2:.01:5];
n=length(x);
for i=1:n;
y(i)=f(x(i));

end
plot(x,y);
grid on

echo off; diary off
the first three lines are just a reference header and do not impact the problem. I don't know if the loop fails in the for loop or in the ifelse on the function file.
multiple condition ifs should be of the form

Code:
if 0<x & x<1
#if body
elseif
:
:
end
[/code]

5. No problem apocolypto.

Further the CB's reply, MATLAB has support for short circuiting behavior when evaluating statements so writing the expression as follows is better for evaluating logical scalar values (at least that seems to be the convention mathworks is using).

Code:
if 0<x && x<1% Note the "&&" vs "&"
#if body
elseif
:
:
end
It is my understand that this means if "0<x" is false, then the whole statement will be false so "x<1" does not need to be evaluated. This is probably pretty trivial but thought I would mention it as it is starting become more frequent in recent code and it is something that confused me when I was starting out.

Regards Elbarto