Pe versus the signal amplitude A
1 次查看(过去 30 天)
显示 更早的评论
Hello everybody
I want to know,what is the problem in my program,when I run the
program,nothing appear in my graph only coordinates x and y.
please I want to know, what is the problem here
can anyone help me and I will not forget any help forever
this is my program
A= [0.1:0.1:5];
for k=1:length(A)
error=0;
for i=1:1000
w=randn(1,1)
if (A(k)/2+w)<=0
error=error+1;
end
end
P(k,1)= (error/100);
end
semilogy(length(A),P(k,1),'b.-');
hold on
grid on
legend('Psk');
xlabel('the signal amplitude A');
ylabel('probability of error');
title('Pe versus the signal amplitude A');
0 个评论
采纳的回答
Matt Fig
2011-4-9
Did you mean to plot:
semilogy(A.',P,'b.-');
Also, I would recommend you not name your variables the same name as MATLAB functions. You have a variable named 'error' and one named 'i'. Both of these variables mask the MATLAB functions of the same name. So if you run your code then try to use the MATLAB function, you will get either an error or a strange result.
In addition, if you need to build an array in a FOR loop, the best practice is to pre-allocate the array so that your code runs efficiently. In your case:
P = zeros(size(A));
Here is the code with pre-allocation and other changes to avoid masking.
A = 0.1:0.1:5;
P = zeros(size(A)); % Pre-allocation!
for k = 1:length(A)
err = 0;
for m = 1:1000
w = randn(1,1);
if (A(k)/2+w)<=0
err = err+1;
end
end
P(k) = (err/100);
end
semilogy(A,P,'b.-');
hold on
grid on
legend('Psk');
xlabel('the signal amplitude A');
ylabel('probability of error');
title('Pe versus the signal amplitude A');
.
.
EDIT
.
.
Your FOR loop can be simplified to (at least) the following, which is still readable.
for k = 1:length(A)
P(k) = sum((A(k)/2 + randn(1,1000))<=0)/100;
end
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!