Problem with getting results for plotting (X,Y)

  • Question is:
  • X axis (t) = from 0 to 30;
  • Y axis (A) = if command; if t>=10 and if not,
Error:
Subscript indices must either be real positive integers or logicals.
Error in SpecifyLineStyleColorAndMarkerExample (line 9)
A(t) = rav * t;
A=ones(1,30);
rav = 0.5;
rln = 0.1;
for t = linspace (0,30,31);
if t<=10
A(t) = rav * t;
else
A(t)= rav*10 + rln * (t-10);
end
end

 採用された回答

DGM
DGM 2021 年 3 月 27 日

0 投票

The index t increments through the integers 0 to 30. While it's true that t is always an integer, it is not a positive integer. For the first pass through the loop, t=0. A has no zeroth element. Matlab uses a 1-based indexing convention, so perhaps:
for t=1:30
would work (provided all the other math works out).

4 件のコメント

Armin Majidian
Armin Majidian 2021 年 3 月 27 日
Thank you
If I put t=1:30 insted of t = linspace (0,30,31), the answer for t is just 30, not 1 to 30 (and not zero).
Please help me through this
A=ones(1,30);
rav = 0.5;
rln = 0.1;
for t = 1:30;
if t<=10
A(t) = rav * t;
else
A(t)= rav*10 + rln * (t-10);
end
end
ANS:
t=
30
DGM
DGM 2021 年 3 月 27 日
Yes. The last value of t is 30. That's what it is in the last pass through the loop.
If you want to generate vectors to plot, t should be a vector not a loop iterator. We can simplify this whole thing:
% define parameters
rav = 0.5;
rln = 0.1;
% define vectors
t=1:30;
A=zeros(size(t));
% do the piecewise math
A(t<=10) = rav*t(t<=10);
A(t>10) = rav*10 + rln*(t(t>10)-10);
% plot A vs t
plot(t,A)
Armin Majidian
Armin Majidian 2021 年 3 月 27 日
That was really helpful. Thank you for your guidance,
And now how can I connect the (0,0) to the rest of the plot?
DGM
DGM 2021 年 3 月 27 日
Oh, my bad. In this context, since t is not an index, you can use:
t=0:30;

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

質問済み:

2021 年 3 月 27 日

コメント済み:

DGM
2021 年 3 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by