matlab中for循环出错

发布于 2024-11-08 12:20:50 字数 851 浏览 0 评论 0原文

我尝试从 for 循环中进行绘制,并且成功了(在小程序中),但是当我尝试使用更大的程序时,出现以下错误: “{使用 ==> 符号学时出错 没有足够的输入参数。 错误==>测试6 27 符号学(pe1,'b',pe2,'r');} 我不知道为什么 我希望有人可以看看并帮助我

我的代码是

clc;
clear;
for n=0:45;

    n=n+1;
   q=55;
    w=42;
    r=-228.6;
    y(n+1)=n+34+w-q-r;
end 

b=36;
o=0.2;
x=b/(1+o);                        % RB for Bpsk
k=2*b/(1+o);                      % Rb for Qpsk
z=y-x;                            % Eb/No for Bpsk
m=y-k;                            % Eb/No for Qpsk
g=0; 
s=0;  
pe1= zeros(1, 47);
pe2= zeros(1, 47);

for i=0:45; 
    g=10.^(0.1*z);
    pe1=0.5*erfc(sqrt(g));
    s=10.^(0.1*m);
    pe2=0.5*erfc(sqrt(s));
end

semilogy(pe1,'b',pe2,'r');
xlabel('energy per bit per noise power spectral density (Eb/No) (dB) ');
ylabel('Bit error rate (Pe)');
legend('Bpsk','Qpsk');
grid;

I try to plot from for loop and I succeed in it(in small program) but when i try with a larger program, I get the following error:
"{Error using ==> semilogy Not enough input arguments.
Error in ==> test6 at 27
semilogy(pe1,'b',pe2,'r');}

and i dont why
I wish someone can have a look and help me

my code is

clc;
clear;
for n=0:45;

    n=n+1;
   q=55;
    w=42;
    r=-228.6;
    y(n+1)=n+34+w-q-r;
end 

b=36;
o=0.2;
x=b/(1+o);                        % RB for Bpsk
k=2*b/(1+o);                      % Rb for Qpsk
z=y-x;                            % Eb/No for Bpsk
m=y-k;                            % Eb/No for Qpsk
g=0; 
s=0;  
pe1= zeros(1, 47);
pe2= zeros(1, 47);

for i=0:45; 
    g=10.^(0.1*z);
    pe1=0.5*erfc(sqrt(g));
    s=10.^(0.1*m);
    pe2=0.5*erfc(sqrt(s));
end

semilogy(pe1,'b',pe2,'r');
xlabel('energy per bit per noise power spectral density (Eb/No) (dB) ');
ylabel('Bit error rate (Pe)');
legend('Bpsk','Qpsk');
grid;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦过后 2024-11-15 12:20:50

导致错误的问题是 semilogy (如 plot)需要采用 semilogy(X1,Y1,'b',X2,Y2,'r ') 如果您想同时绘制多条线。因此,您的semilogy 应该是semilogy(z,pe1,'b',m,pe2,'r')。您还需要在 semilogy 调用中使用 zm 来确保 x 轴比例正确。

您还有其他一些问题。我并没有让它看起来完全像我会做的那样,因为我希望你的代码能够被你识别。但您可能希望将您的代码与以下代码进行逐行比较。我做过的一件事可能是错误的,即将 r 更改为其负数,否则 y 太大(因此 pe1>pe2 为零,通过 erfc)。

你的代码已经矢量化,所以我摆脱了 for 循环。请注意,for n= 循环中不会有 n=n+1;它会自动增加。

clc;
clear;
n=0:45;
q=55;
w=42;
r=228.6;
y=n+34+w-q-r;

b=36;
o=0.2;
x=b/(1+o);                        % RB for Bpsk
k=2*b/(1+o);                      % Rb for Qpsk
z=y-x;                            % Eb/No for Bpsk
m=y-k;                            % Eb/No for Qpsk

g=10.^(0.1*z);
pe1=0.5*erfc(sqrt(g));
s=10.^(0.1*m);
pe2=0.5*erfc(sqrt(s));

semilogy(z,pe1,'b',m,pe2,'r');
xlabel('energy per bit per noise power spectral density (Eb/No) (dB) ');
ylabel('Bit error rate (Pe)');
legend('Bpsk','Qpsk');
grid;

The problem causing your error is that semilogy (like plot) expects inputs in the form semilogy(X1,Y1,'b',X2,Y2,'r') if you want to plot more than one line at the same time. So, your semilogy should be semilogy(z,pe1,'b',m,pe2,'r'). You also need the z and m in the semilogy call to get your x-axis scale correct.

You have a few other problems. I have not made it look exactly like I'd do it, since I wanted your code to be recognizable to you. But you may wish to compare your code line by line to the following. One thing I've done that is probably wrong is change r to its negative since otherwise y is far to large (so pe1 and pe2 were zero, via erfc).

Your code was already vectorized, so I got rid of the for loops. Note that you wouldn't have n=n+1 in a for n= loop; it's incremented automatically.

clc;
clear;
n=0:45;
q=55;
w=42;
r=228.6;
y=n+34+w-q-r;

b=36;
o=0.2;
x=b/(1+o);                        % RB for Bpsk
k=2*b/(1+o);                      % Rb for Qpsk
z=y-x;                            % Eb/No for Bpsk
m=y-k;                            % Eb/No for Qpsk

g=10.^(0.1*z);
pe1=0.5*erfc(sqrt(g));
s=10.^(0.1*m);
pe2=0.5*erfc(sqrt(s));

semilogy(z,pe1,'b',m,pe2,'r');
xlabel('energy per bit per noise power spectral density (Eb/No) (dB) ');
ylabel('Bit error rate (Pe)');
legend('Bpsk','Qpsk');
grid;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文