MATLAB: How to plot exact user defined function

plotuser defined fuction

I have defined a function like this
function z=agni(x)
if x > 0
z=x;
else
z = x.^2;
end
But in the time of plotting plot(x,agni(x)) with x =[-2:.01:2] generate only the curve of x^2. WHY?

Best Answer

if x > 0
means
if all(x > 0)
so if you pass in a vector of x then if any x in the vector is <= 0 then the "if" will not be true.
You should research logical indexing.
Related Question