"prabhakaran m" <prabha.gahon@gmail.com> wrote in message <jp9m5e$jbq$1@newscl01ah.mathworks.com>...
> if x >1 & x<=0
> y=1+2*x
> else
> y=12*x
> end
        
You are not using the 'if' condition properly. In Matlab when you apply the 'if' to a logical vector, as you have done here, what follows it is executed only if the vector is true for ALL elements of the vector. In your case the "y=1+2*x" would be executed only if x were between 1 and 0 for all 41 elements of x, which of course is not true. Therefore the "y = 12*x" is executed and applies to all elements of x.
You should either use a forloop with the 'if' in it with the inequalities to be applied to each element of x individually, as in "if x(i)>1&x(i)<=0, etc.", or else write a vectorized version that works properly such as the following:
y = 1 + 4*((x>1)&(x<=0)).*x  2*x;
or more tediously
y = ((x>1)&(x<=0)).*(1+2*x) + ((x<=1)(x>0)).*(12*x);
Notice that these have no 'if' in them.
Roger Stafford
