MATLAB: Plotting a graph after for and if else if

matlab plotting graph elseif for

i have a project to write a script to give the height a cam pushes up a follower from the angle of the cam and then plot the graph of angle versus height. The radius of the cam is not uniform. i have written the code and have gotten y values but cannot plot these on a graph. any insight into what i am doing wrong? thanks!
Matlab code
for x=0: pi/20: 2*pi; %set up for loop for values between 0 and 2pi
if (x>=0)&&(x<=pi/2)
y=6*(2*x-0.5*sin(x))/pi
elseif (x>=pi/2)&&(x<=2*pi/3)
y=6;
elseif (x>=2*pi/3)&&(x<=4*pi/3)
y=6-3*(1-0.5*cos(3*(x-2*pi/3)))
elseif (x>=4*pi/3)&&(x<=3*pi/2)
y=3
elseif(x>=3*pi/2)&&(x<=7*pi/4)
y=3-1.5*((x-3*pi/2)/(pi/4))^2
else (x>=7*pi/4)&&(x<=2*pi)
y=0.75-0.75*(1-(x-7*pi/4)/(pi/4))^2
end
end
plot(x,y)

Best Answer

You need to subscript the x and y values:
x=0: pi/20: 2*pi; %set up for loop for values between 0 and 2pi
for k1 = 1:length(x)
if (x(k1)>=0)&&(x(k1)<=pi/2)
y(k1)=6*(2*x(k1)-0.5*sin(x(k1)))/pi;
elseif (x(k1)>=pi/2)&&(x(k1)<=2*pi/3)
y(k1)=6;
elseif (x(k1)>=2*pi/3)&&(x(k1)<=4*pi/3)
y(k1)=6-3*(1-0.5*cos(3*(x(k1)-2*pi/3)));
elseif (x(k1)>=4*pi/3)&&(x(k1)<=3*pi/2)
y(k1)=3;
elseif(x(k1)>=3*pi/2)&&(x(k1)<=7*pi/4)
y(k1)=3-1.5*((x(k1)-3*pi/2)/(pi/4))^2;
else (x(k1)>=7*pi/4)&&(x(k1)<=2*pi);
y(k1)=0.75-0.75*(1-(x(k1)-7*pi/4)/(pi/4))^2;
end
end
plot(x,y)