Plotting over a certain range, what am i doing wrong here?

1 回表示 (過去 30 日間)
Sandip Ghatge
Sandip Ghatge 2020 年 6 月 2 日
コメント済み: Star Strider 2020 年 6 月 2 日
I am looking to plot this sinusoidal curve over just a desired range, from theta=120 to theta=150 here
It must be zero before 120 and after 150
The code is as written below,
x = 0:0.1:3.14;
y = sin(x);
theta = 0:360;
if theta(theta(1,:)<120) && theta(theta(1,:)>150)
yy = 0;
else
yy = y;
end
plot(theta,yy)
Its returning me errors saying,
Operands to the || and && operators must be convertible to logical scalar values.
How do i approach this problem?

採用された回答

Star Strider
Star Strider 2020 年 6 月 2 日
Mixing radian and degree angle units makes this a bit more challenging.
Try this:
yfcn = @(a) sin(a) .* ((120*pi/180 < a) & (150*pi/180 > a));
x = 0:0.1:pi;
figure
plot(x, yfcn(x))
grid
Note that in your original code, ‘theta’ and ‘x’ are vastly different sizes, so the plot would never work regardless. Beyond that, to compare vector elements, use single logical operators, not double logical operators.
.
  4 件のコメント
Sandip Ghatge
Sandip Ghatge 2020 年 6 月 2 日
Thanks for the explanation:)
Star Strider
Star Strider 2020 年 6 月 2 日
As always, my pleasure!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by