How to solve "index exceeds the number of array elements"

1 回表示 (過去 30 日間)
Ryan Majid
Ryan Majid 2020 年 12 月 24 日
コメント済み: Ryan Majid 2020 年 12 月 24 日
Hi,
Why I can't plot the following code? The error is: Index exceeds the number of array elements (1).
What I'm trying to achive is that a user enters a scalar value f (such as 1500) anf Ht and Hr. So, I want to find value of ahr and L accondingliy and then plot L vs f. SO on f axis I shoud have 100,150,200,250,300,....,1500 and corrsponding values on L axis.
for i=100:50:f
cm=0;
ahr(i)=(1.1*log10(f(i))-0.7)*Hr-1.56*log10(f(i))-0.8;
L(i)=46.3+33.9*log10(f(i))-13.82*log10(Ht)-ahr+(44.9-6.55*log10(Hr))*log10(d)+cm;
plot(f,L)
end

採用された回答

Mischa Kim
Mischa Kim 2020 年 12 月 24 日
編集済み: Mischa Kim 2020 年 12 月 24 日
Hi Ryan, try something like this:
fl = 100;
fu = 1500; % replace as necessary
N = 100;
f = linspace(fl,fu,N);
cm = 0;
for ii = 1:numel(f) % careful with "i", this is also the imaginary unit
ahr(ii) = (1.1*log10(f(ii))-0.7)*Hr-1.56*log10(f(ii))-0.8;
L(ii) = 46.3+33.9*log10(f(ii))-13.82*log10(Ht)-ahr(ii)+(44.9-6.55*log10(Hr))*log10(d)+cm;
end
plot(f,L)
  3 件のコメント
Mischa Kim
Mischa Kim 2020 年 12 月 24 日
編集済み: Mischa Kim 2020 年 12 月 24 日
With
for i=100:50:f
i is assigned values: 100, 150, 200, 250, etc. As a result, in
L(ii) = 46.3+33.9*log10(...
you create a (rather long) vector L and assign values to L(100), L(150), and so on and so forth. At the same time, f is a scalar (just one number). For the plot() command to work f and L need to be vectors of the same size.
Ryan Majid
Ryan Majid 2020 年 12 月 24 日
Understood. So appreciated.

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

その他の回答 (0 件)

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by