function y=displacement(theta)
y=zeros(1,length(theta));
for i=1:length(theta)
if theta(i)>=0 & theta(i)<= pi/2
y(i)=6*(2*theta(i)-0.5*sin(theta(i)));
elseif theta(i)>= pi/2 & theta(i)<= 2*pi/3
y(i)=6;
elseif theta(i)>= 2*pi/3 & theta(i)<= 4*pi/3
y(i)=6-3*abs(1-0.5*cos(3*theta(i)-2*pi));
elseif theta(i)>= 4*pi/3 & theta(i)<= 3*pi/2
y(i)=3;
elseif theta(i)>= 3*pi/2 & theta(i)<= 7*pi/4
y(i)=3-1.5*((theta(i)-3*pi)/(pi/4))^2;
elseif theta(i)>= 7*pi/4 && theta(i)<= 2*pi
y(i)=0.75-0.75*(1-((theta(i)-(7*pi/4))/(pi/4)))^2;
end
end
Save above code as function file named as displacement.m (MATLAB File) and call the function from another script
theta=linspace(0,2*pi,500);
y=displacement(theta);
plot(theta,y,'r','Linewidth',2)
grid on;
legend({'displacement(theta)'},'fontsize',10)
xlabel('Theta (radians)','fontsize',10)
ylabel('Displacement','fontsize',10)
title('Piecwise functions','fontsize',10)
Best Answer