Plot Error Please help

2 ビュー (過去 30 日間)
Ryan Williams
Ryan Williams 2021 年 3 月 14 日
コメント済み: Star Strider 2021 年 3 月 14 日
I'm trying to plot a piecewise function that I've created and it keeps giving me an error saying "vectors must be same length." this is what I have. Please help.
theta = linspace(0,2*pi,500);
for i=1:numel(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*(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/2))/(pi/4))^2;
elseif (theta(i) >= (2*pi/3)) && (theta(i) <= (4*pi/3))
y(i) = 0.75 - 0.75*(1 - (theta(i) -(7*pi/4))/(pi/4))^2;
end
end
plot(theta,y,'r','linewidth',2)

採用された回答

Star Strider
Star Strider 2021 年 3 月 14 日
It’s necessary to account for absolutely all possibilities.
Try this:
theta = linspace(0,2*pi,500);
for i=1:numel(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*(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/2))/(pi/4))^2;
elseif (theta(i) >= (2*pi/3)) && (theta(i) <= (4*pi/3))
y(i) = 0.75 - 0.75*(1 - (theta(i) -(7*pi/4))/(pi/4))^2;
else % Added Condition
y(i) = 0; % Added Assignment
end
end
figure
plot(theta,y,'r','linewidth',2)
That ran without error when I tested it.
  2 件のコメント
Ryan Williams
Ryan Williams 2021 年 3 月 14 日
Thank you!
Star Strider
Star Strider 2021 年 3 月 14 日
As always, my pleasure!

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

その他の回答 (1 件)

KALYAN ACHARJYA
KALYAN ACHARJYA 2021 年 3 月 14 日
編集済み: KALYAN ACHARJYA 2021 年 3 月 14 日
There must be one else condition (to avoid any mismatch between vectors length). If none of the conditions is true, then such condition y will be the "else" condition. Sure, some cases theta(i) maynot be satisfy any of those conditions.
Pre-allocation also helps to avoid a mismatch between the lengths of the vectors, but whether this is true in the case of your condition, if a theta (i) if-and, is not part of the conditions, then y should be zero .
Or assign last one as an "else" condition (Check the coditions)
theta = linspace(0,2*pi,500);
y=zeros(1,numel(theta)); % Thise also helps to avoid mismatch between vectors length
for i=1:numel(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*(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/2))/(pi/4))^2;
else
%(theta(i) >= (2*pi/3)) && (theta(i) <= (4*pi/3))
y(i) = 0.75 - 0.75*(1 - (theta(i) -(7*pi/4))/(pi/4))^2;
end
end
plot(theta,y,'r','linewidth',2)

カテゴリ

Help Center および File ExchangeLanguage Fundamentals についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by