MATLAB: Problem with vectors and if statements

if statementvectors

Hello everyone, I am trying to write a function that takes in a value of N and a vector x and run the vector through an if statement to sort the numbers and plot them accordingly. I want all plots on the same graph.
function [ blah] = tanplot( x,N)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
hold on
if abs(tan(x)) <= N
y = tan(x);
plot(x,y)
elseif tan(x) > N
y = N;
plot(x,y,'-m')
elseif tan(x) < -N
y = -N;
plot(x,y,'-r')
end
hold off
title('Tan Plot')
end

Best Answer

function tanplot( x,N)
tanX=tan(x);
x1=x(tanX<-N);
y1(1:length(x1))=-N;
idx=abs(tanX)<=N;
x2=x(idx);
y2=tanX(idx);
x3=x(tanX>N);
y3(1:length(x3))=+N;
plot(x1,y1,'-r',x2,y2,x3,y3,'-m');
title('Tan Plot')