【问题标题】:MATLAB, how to solve Index exceeds the number of array elements (1)?MATLAB,如何解决Index超过数组元素个数(1)?
【发布时间】:2020-12-24 15:49:58
【问题描述】:

为什么我不能绘制以下代码?错误是:

索引超过了数组元素的数量 (1)。

我想要实现的是用户输入一个标量值f(例如 1500)和HtHr。所以,我想找到ahrL 的值,然后绘制Lf。所以在f 轴上,我应该在L 轴上有 100,150,200,250,300,....,1500 和相应的值。

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

【问题讨论】:

    标签: matlab for-loop plot


    【解决方案1】:

    使用向量化方法评估绘图

    假设dHtHr 是标量,以下代码应该可以工作。此脚本使用矢量化方法,其中f 是用于计算方程ahrL 的向量。现在将对f 中的每个元素评估方程ahrL,从而导致ahrL 的长度与f 相同。此方法无需 for 循环,并与 MATLAB 的优势/功能一起使用。

    %Asking user for input values%
    fmax = input("Please type in the maximum f: ");
    Ht = input("Please type in the value of Ht: ");
    Hr = input("Please type in the value of Hr: ");
    cm = 0;
    d = 1;
    
    %Creating the vector of points to plot on%
    f = (100:50:fmax);
    
    ahr = (1.1*log10(f)-0.7)*Hr-1.56*log10(f) - 0.8;
    L = 46.3 + 33.9*log10(f)-13.82*log10(Ht)-ahr+(44.9-6.55*log10(Hr))*log10(d);
    
    clf;
    plot(f,L,'Marker','.');
    title("Plot L vs f");
    xlabel("f"); ylabel("L");
    xticks(f);
    grid;
    

    使用 MATLAB 2019b 运行

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-04
      • 1970-01-01
      • 2011-02-23
      • 2018-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多