Did you mean to plot:
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:
Here is the code with pre-allocation and other changes to avoid masking.
A = 0.1:0.1:5;
P = zeros(size(A));
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
Best Answer