フィルターのクリア

How to make a piecewise function and plot it ?

1 回表示 (過去 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);
  2 件のコメント
Luca Amicucci
Luca Amicucci 2020 年 9 月 30 日
Thankss!!!
Sudhakar Shinde
Sudhakar Shinde 2020 年 10 月 1 日
Welcome

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops 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