Matlab 使用foor循环进行绘图

Matlab 使用foor循环进行绘图,matlab,for-loop,plot,Matlab,For Loop,Plot,我写了以下脚本。然而,我试图: 绘图fvsL 在不同的绘图中同时绘图(fvsL),(Hrvs.L),(HtvsL)(我不想为wach绘图创建不同的m文件) 对于第一项,我添加了一个for循环,但是,它不能绘图,也不显示任何内容,也不显示绘图。你能告诉我这里有什么吗? 代码是: clc; clear; close all; %Enter EIRP and Gain prompt='Is Pt entered in dBw or Watt? Enter dB or W: '; Pt=input(

我写了以下脚本。然而,我试图: 绘图
f
vs
L
在不同的绘图中同时绘图(
f
vs
L
),(
Hr
vs.L),(
Ht
vs
L
)(我不想为wach绘图创建不同的
m
文件) 对于第一项,我添加了一个for循环,但是,它不能绘图,也不显示任何内容,也不显示绘图。你能告诉我这里有什么吗? 代码是:

clc;
clear;
close all;

%Enter EIRP and Gain

prompt='Is Pt entered in dBw or Watt? Enter dB or W: ';
Pt=input('Enter the Transmiiting Power: ');

inp = input(prompt,'s');
%inp = upper(inp);
if strcmpi(inp,'W')
    Pt = 10*log10(Pt);
    disp(Pt);
end
Gt=input('Enter Transmitter Antenna Gain: ');
Gr=input('Enter Reciever Antenna Gain: ');
f=input('Enter Operating Frequency in MHz (150 MHz < f < 2000 MHz): ');

while (f<150 || f>2000)
fprintf(2,'\nFrequency must be between 150 MHz to 1920 MHz\n');
f=input('Enter Operating Frequency in MHz (150 MHz < f < 2000 MHz): ');
end

% Extended Hata Model
    
if (f>1920)
    fprintf(2,'\nYou need to use Extended Hata Model (Cost-231)\n');
    prompt='\n Medium city (M)\n Suburban Area (S) \n Metropolitan Center (C)\n';
    inp = input(prompt,'s');
    inp = upper(inp);
   for i=150:50:length(f)
    Ht=input('BS antenna Height in m (30m<Ht<200m): ');
    while (Ht<30 || Ht>200)
      fprintf(2,'\nFrequency must be between 3m to 1000m\n');
      Ht=input('BS antenna Height in m (30m<Ht<1000m): ');
    end
    
    Hr=input('MS anenna Height in m(1m<Hr<10m): ');
     while (Hr<1 || Hr>10)
      fprintf(2,'\nReciever Height must be between 1m to 10m\n');
      Hr=input('Mobile antenna Height in m (1m<Hr<10m): ');
     end
         
      
    d=input('Distance bt Tx and Rx in Km (1Km<d<20Km) : ');
     while (d<1 || d>20)
      fprintf(2,'\nDistance must be between 1Km to 20Km\n');
      d=input('Distance bt Tx and Rx in Km (1Km<d<20 Km): ');
     end
     if strcmp(inp,'M')
         for i=150:50:length(f)
           cm=0;
           ahr=(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;
         end
           plot(f,L)
     elseif strcmp(inp,'S')
          for i=150:50:length(f)
          cm=1;
          ahr=(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;
          end
          plot(f,L)
     else 
          for i=150:50:length(f)
          cm=3;
          ahr=3.2*(log10(11.75*Hr))^2-4.97;
          L(i)=46.3+33.9*log10(f(i))-13.82*log10(Ht)-ahr+(44.9-6.55*log10(Hr))*log10(d)+cm;
          end
          plot(f,L)
     end
   end
elseif (f>1500 && f<=1920)
    fprintf(2,'\nYou need to use Okumura Model or Extended Hata Model (Cost-231)\n');
    prompt = 'Which Model do you intend to use? (Insert O for Okumora or E for Exctended Hata (Cost-231)';
        inp = input(prompt,'s');
        inp = upper(inp);
        if strcmp(inp,'O')
            warning('Okumora Model is selceted')
           
            Ht=input('BS antenna Height in m (30m<Ht<1000m): ');
             while (Ht<30 || Ht>1000)
                   fprintf(2,'\nFrequency must be between 3m to 1000m\n');
                   Ht=input('BS antenna Height in m (30m<Ht<1000m): ');
             end
    
             Hr=input('MS anenna Height in m(3m<Hr<10m): ');
              while (Hr<3 || Hr>10)
              fprintf(2,'\nReciever Height must be between 30m to 100m\n');
              Hr=input('Mobile antenna Height in m (30m<Ht<1000m): ');
              end
         
        
              d=input('Distance bt Tx and Rx in Km (1Km<d<100 Km) : ');
              while (d<1 || d>100)
              fprintf(2,'\nDistance must be between 30m to 100m\n');
              d=input('Distance bt Tx and Rx in Km (1Km<d<100 Km): ');
              end
    
              Amu=input('Enter the Median Attenuation in dB: ');
              Ga=input('Enter Correction Factor due to environment type: ');
              Kter=input('Correction factor on Rolling Hilly Terain (Undulation): ');
              Ksp=input('Correction Factor on Sloping Terrain');
              Kls=input('Correction factor on Land Sea Terrain');
              Lf=32.45+20*log(f)+20*log10(d);
              GHt=20*log(Ht/200);
                if(Hr>3)
                   GHr=20*log10(Hr/3);
                else
                   GHr=10*log10(Hr/3);
                end
                
        else
            fprintf(2,'\nYou need to use Extended Hata Model (Cost-231)\n');
            prompt='\n Medium city (M)\n Suburban Area (S) \n Metropolitan Center (C)\n';
            inp = input(prompt,'s');
            inp = upper(inp);
            Ht=input('BS antenna Height in m (30m<Ht<200m): ');
            while (Ht<30 || Ht>200)
            fprintf(2,'\nFrequency must be between 3m to 1000m\n');
            Ht=input('BS antenna Height in m (30m<Ht<1000m): ');
            end
    
            Hr=input('MS anenna Height in m(1m<Hr<10m): ');
            while (Hr<1 || Hr>10)
            fprintf(2,'\nReciever Height must be between 30m to 100m\n');
            Hr=input('Mobile antenna Height in m (30m<Ht<1000m): ');
            end
         
      
            d=input('Distance bt Tx and Rx in Km (1Km<d<20Km) : ');
            while (d<1 || d>20)
            fprintf(2,'\nDistance must be between 1Km to 20Km\n');
            d=input('Distance bt Tx and Rx in Km (1Km<d<20 Km): ');
            end
            if strcmp(inp,'M')
               cm=0;
               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)+cm;
        
            elseif strcmp(inp,'S')
                   cm=1;
                   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)+cm;
            else 
                   cm=3;
                   ahr=3.2*(log10(11.75*Hr))^2-4.97;
                   L=46.3+33.9*log10(f)-13.82*log10(Ht)-ahr+(44.9-6.55*log10(Hr))*log10(d)+cm;
            end
                
        end     
                L=Lf+Amu-GHt-GHr-Ga;
        
        disp(L)
        plot(Hr,L);
        title('Frequency versus Loss in dB for suburben area using Ukumara Model ');
        xlabel('Frequency in MHz');
        ylabel('Path Loss in dB');
 else
        prompt = 'Which Model do you intend to use? (Insert O for Okumora or H for Hata or E for Extended Hata(Cost-231))';
        inp = input(prompt,'s');
        inp = upper(inp);
        if strcmp(inp,'O')
            warning('Okumora Model is selceted');
            Ht=input('BS antenna Height in m (30m<Ht<1000m): ');
             while (Ht<30 || Ht>1000)
                    fprintf(2,'\nFrequency must be between 3m to 1000m\n');
                    Ht=input('BS antenna Height in m (30m<Ht<1000m): ');
             end
    
            Hr=input('MS anenna Height in m(3m<Hr<10m): ');
              while (Hr<3 || Hr>10)
                     fprintf(2,'\nReciever Height must be between 30m to 100m\n');
                     Hr=input('Mobile antenna Height in m (30m<Ht<1000m): ');
              end
         
      
            d=input('Distance bt Tx and Rx in Km (1Km<d<100 Km) : ');
              while (d<1 || d>100)
                    fprintf(2,'\nDistance must be between 30m to 100m\n');
                    d=input('Distance bt Tx and Rx in Km (1Km<d<100 Km): ');
              end
            Amu=input('Enter the Median Attenuation in dB: ');
            Ga=input('Enter Correction Factor due to environment type: ');
            Kter=input('Correction factor on Rolling Hilly Terain (Undulation): ');
            Ksp=input('Correction Factor on Sloping Terrain');
            Kls=input('Correction factor on Land Sea Terrain');
            Lf=32.45+20*log(f)+20*log10(d);
            GHt=20*log10(Ht/200);
             if(Hr>3)
                GHr=20*log(Hr/3);
             else
             GHr=10*log10(Hr/3);
             end
              L=Lf+Amu-GHt-GHr-Ga;
        
              disp(L)
              plot(Hr,L);
              title('Frequency versus Loss in dB for suburban area using Ukumara Model ');
              xlabel('Frequency in MHz');
              ylabel('Path Loss in dB');
              grid on;
        elseif strcmp(inp,'H')
             fprintf(2,'\n Hata Model is selected \n');
             Ht=input('Basestaion anenna Height in m(3m<Hr<1000m): ');
             while (Ht<30 || Ht>1000)
                    fprintf(2,'\nFrequency must be between 3m to 1000m\n');
                    Ht=input('BS antenna Height in m (30m<Ht<1000m): ');
             end
    
            Hr=input('MS anenna Height in m(3m<Hr<10m): ');
              while (Hr<3 || Hr>10)
                     fprintf(2,'\nReciever Height must be between 30m to 100m\n');
                     Hr=input('Mobile antenna Height in m (30m<Ht<1000m): ');
              end
         
      
            d=input('Distance bt Tx and Rx in Km (1Km<d<200 Km) : ');
              while (d<1 || d>200)
                    fprintf(2,'\nDistance must be between 30m to 200m\n');
                    d=input('Distance bt Tx and Rx in Km (1Km<d<100 Km): ');
              end
             prompt=' Which area is targeted? \n Small or Medium Sized City? (M) \n Large City (L)\n Suburban area (S) \n Open rural area(O) \n';
             inp=input(prompt,'s');
             inp = upper(inp);
             if strcmp(inp,'S')
                 ahr=(1.1*log10(f)-0.7)*Hr-1.56*log10(f)-0.8;
                 L=68.55+26.16*log10(f)-13.82*log10(Ht)-ahr+(44.9-6.55*log10(Ht))*log10(d);
             elseif strcmp(inp,'L') 
                 if (f>=300)
                    ahr=8.9*(log10(1.54*Hr))^2-1.1;
                    L=68.55+26.16*log10(f)-13.82*log10(Ht)-ahr+(44.9-6.55*log10(Ht))*log10(d);
                 else
                    ahr=3.2*(log10(11.75*Hr))^2-4.97;
                 end
                 
                    L=68.55+26.16*log10(f)-13.82*log10(Ht)-ahr+(44.9-6.55*log10(Ht))*log10(d);                    
             elseif strcmp(inp,'S')
                 ahr=(1.1*log10(f)-0.7)*Hr-1.56*log10(f)-0.8;
                 L=68.55+26.16*log10(f)-13.82*log10(Ht)-ahr+(44.9-6.55*log(Ht))*log10(d)-2*(log10(f/28))^2-5.4;
             elseif strcmp(inp,'O')   
                 ahr=(1.1*log10(f)-0.7)*Hr-1.56*log10(f)-0.8;
                 L=68.55+26.16*log10(f)-13.82*log10(Ht)-ahr+(44.9-6.55*log10(Ht))*log10(d)-4.78*(log10(f))^2-18.33*log10(f)-40.98;                
         end    
        else
               prompt='\n Medium city (M)\n Suburban Area (S) \n Metropolitan Center (C)\n';
               inp = input(prompt,'s');
               inp = upper(inp);
             
               Ht=input('BS antenna Height in m (30m<Ht<200m): ');
               while (Ht<30 || Ht>200)
               fprintf(2,'\nFrequency must be between 3m to 1000m\n');
               Ht=input('BS antenna Height in m (30m<Ht<1000m): ');
               end
    
               Hr=input('MS anenna Height in m(1m<Hr<10m): ');
               while (Hr<1 || Hr>10)
               fprintf(2,'\nReciever Height must be between 30m to 100m\n');
               Hr=input('Mobile antenna Height in m (30m<Ht<1000m): ');
               end
         
      
              d=input('Distance bt Tx and Rx in Km (1Km<d<20Km) : ');
              while (d<1 || d>20)
              fprintf(2,'\nDistance must be between 1Km to 20Km\n');
              d=input('Distance bt Tx and Rx in Km (1Km<d<20 Km): ');
              end
                if strcmp(inp,'M')
                   cm=0;
                   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)+cm;
        
                elseif strcmp(inp,'S')
                       cm=1;
                       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)+cm;
                else 
                      cm=3;
                      ahr=3.2*(log10(11.75*Hr))^2-4.97;
                      L=46.3+33.9*log10(f)-13.82*log10(Ht)-ahr+(44.9-6.55*log10(Hr))*log10(d)+cm;
                 end
                 
           
               
        end
end
clc;
清楚的
全部关闭;
%输入EIRP和增益
提示符=“Pt是以dBw还是瓦特输入的?输入dB或W:';
Pt=输入('输入传输功率:');
inp=输入(提示,'s');
%inp=上(inp);
如果strcmpi(inp,'W')
Pt=10*log10(Pt);
disp(Pt);
结束
Gt=输入('输入发射机天线增益:');
Gr=输入('输入接收器天线增益:');
f=输入(“输入工作频率,单位为MHz(150 MHz1920)
fprintf(2),\n您需要使用扩展Hata模型(Cost-231)\n’;
提示符='\n中等城市(M)\n郊区(S)\n大都会中心(C)\n';
inp=输入(提示,'s');
inp=上(inp);
i=150:50时:长度(f)

Ht=input('BS天线高度,单位为m(30mDebug)是发现问题的好方法。下面是第32行中的错误:

for i=150:50:length(f)

你知道,
f
是一个数字,所以
f
的长度是1。因此,for循环将不会运行。

Nice catch!!!没有必要将代码放入代码段中,除非它是HTML、CSS或JavaScript。你只需使用大括号按钮
{}
要添加代码块或突出显示文本,请在Windows或
⌘ + K
在MacOS上。谢谢你的回答。那么,解决方案是什么呢?f是一个数字,比如1500,我想在第50步找到f=150到1500之间的所有值,然后绘制。我应该使用哪个命令?@Majid你可以使用“ff=150:50:f;”创建一个向量。然后将“for I=150:50:length(f)”更改为“for I=1:length(ff)”