I can not write piecewise function

2 views (last 30 days)
I need to write piecewise function and plot it on matlab. But I couldn't do it. Here is my code. I need T_16 in terms of theta_12 with following conditions:
T_16 = 10*theta_12/0.17 if 0<=theta_12<=0.17
10 if 0.17<=theta_12<=3.5
215.88-10*theta_12/0.17 if 3.5<t=heta_12<=3.68
0 if 3.68<=theta_12<=2*pi()
clear; close all;
AB=140; A0_A=30; A0_B0=70; B0_Q=80; Q_C0=30;
sampling=3600;
theta_12=linspace(0,2*pi(),sampling+1);
theta_12=theta_12(1:end-1)';
theta_13=zeros(sampling,1);
s_43=zeros(sampling,1);
s_65=zeros(sampling,1);
theta_16=zeros(sampling,1);
for a=1:1:sampling
s_43(a)=(A0_A*A0_A*(1+2*(A0_B0/A0_A)*cos(theta_12(a)))+A0_B0*A0_B0)^0.5;
theta_13(a)=atan2(A0_A*sin(theta_12(a)),A0_B0+A0_A*cos(theta_12(a)));
theta_16(a)=atan2(-(AB-s_43(a))*sin(theta_13(a))-B0_Q,-(AB-s_43(a))...
*cos(theta_13(a))+Q_C0);
s_65(a)=(Q_C0*Q_C0+B0_Q*B0_Q+(AB-s_43(a))*(AB-s_43(a))-...
2*Q_C0*(AB-s_43(a))*cos(theta_13(a))+2*B0_Q*...
(AB-s_43(a))*sin(theta_13(a)))^0.5;
end
figure (1)
plot(theta_12*180/pi(),theta_13*180/pi(),...
theta_12*180/pi(),theta_16*180/pi());
legend("theta_{13}","theta_{14}");
xlabel("Angle theta_{12} (degree)");
ylabel("Angle (degree)");
title("Angles");
figure (2)
plot(theta_12*180/pi(),s_43,theta_12*180/pi(),s_65);
legend("s_{43}","s_{65}");
xlabel("Angle theta_{12} (degree)");
ylabel("Distance mm");
title("Angles");
syms theta_12
T_16 = piecewise((0<theta_12)&(theta_12<0.17),10*theta_12/0.17 ...
,(0.17<theta_12)&(theta_12<3.5),10 ...
,(3.5<theta_12)&(theta_12<3.68),(215.88-10*theta_12/0.17) ...
,(3.68<theta_12)&(theta_12<(2*pi())),0);
fplot(T_16)

Accepted Answer

Voss
Voss on 14 Jan 2022
sampling=3600;
theta_12=linspace(0,2*pi(),sampling+1);
T_16 = zeros(1,sampling+1);
% idx = theta_12 >= 0 & theta_12 <= 0.17;
% theta_12 is always >= 0 so you don't need to specify that condition
idx = theta_12 <= 0.17;
T_16(idx) = 10*theta_12(idx)/0.17;
idx = theta_12 >= 0.17 & theta_12 <= 3.5;
T_16(idx) = 10;
idx = theta_12 >= 3.5 & theta_12 <= 3.68;
T_16(idx) = 215.88-10*theta_12(idx)/0.17;
% % T_16 is initialized to zeros, so you don't need the last calculation
% idx = theta_12 >= 3.68 & theta_12 <= 2*pi;
% idx = theta_12 >= 3.68;
% T_16(idx) = 0;
figure();
plot(theta_12,T_16);

More Answers (0)

Categories

Find more on Just for fun in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!