How to make a piecewise function and plot it ?

조회 수: 4 (최근 30일)
Luca Amicucci
Luca Amicucci 2020년 9월 29일
댓글: Sudhakar Shinde 2020년 10월 1일
Hi guys!
I want to write a piecewise function , with 4 case , and so I made this :
H = 2;
w = 209.439509; % 2000 rpm
a = 0;
for i = 1:360
w2(i,1) = w; % w2 angular velocity (rad/s)
a2(i,1) = a; % a2 angular acceleration (rad/s^2)
th2(i,1) = i*pi/180; % radiant
th2d(i,1) = i; % degree
% first
if (i) < 90
h1(i,1) = 0;
v1(i,1) = 0;
a1(i,1) = 0;
% second
else if (i) >= 90 && (i) < 180
beta = pi-pi/2;
h2(i,1) = H/pi*( (pi*(th2(i,1)-pi/2))./beta - 1/2*sin((2*pi*(th2(i,1)-pi/2))./beta) );
v2(i,1) = (H.*w2(i,1))./(beta) .* ( 1 - cos( (2*pi*(th2(i,1)-pi/2))./beta) );
a2(i,1) = ( 2*H*pi.*w2(i,1).^2 ./ beta^2 ).*sin( (2*pi.*(th2(i,1)-pi/2))./(beta) );
% third
else if (i) >= 180 && (i) < 240
h3(i,1) = 0;
v3(i,1) = 0;
a3(i,1) = 0;
% fourth
else (i) >= 240 && (i) < 360
beta2 = -pi/2;
h4(i,1) = H/pi*( (pi*(th2(i,1)-pi*4/3))./beta2 - 1/2*sin((2*pi*(th2(i,1)-pi*4/3))./beta2) );
v4(i,1) = (H.*w2(i,1))./(beta2) .* ( 1 - cos( (2*pi*(th2(i,1)-pi*4/3))./beta2) );
a4(i,1) = ( 2*H*pi.*w2(i,1).^2 ./ beta2^2 ).*sin( (2*pi.*(th2(i,1)-pi*4/3))./(beta2) );
end
end
end
end
figure(1)
plot(th2(1:89),h1);
hold on;
plot(th2(90:179),h2)
hold on;
.....
But it doesn't work because, at the first if statement the vector has the right size (89) , but then at the second if it has the size of the previous one + his correct size ( 89) and so on...
Anyone can help me??
Thanks in advance,
Luca

채택된 답변

Sudhakar Shinde
Sudhakar Shinde 2020년 9월 29일
Instead of using h1, h2,h3 and h4 , you can use h variable for all 4 pieces of code.
then use plot
plot(th2,h);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by