CODE error correction

13 Ansichten (letzte 30 Tage)
milad babaei
milad babaei am 25 Sep. 2011
Beantwortet: Vikash Anand am 13 Dez. 2021
HI all,
would somebody please help me find out what should i do to plot and correct this script?
clear
clc;
B=1000; L=2000;Df=200; Sgama=.8;
nsamples=3000;
rmem = zeros(3,nsamples);
qult = zeros(1,nsamples);
for K=1:nsamples
while true
C = normrnd(620,147.64);
if C<=450 || C >= 800 || ismember(C,rmem(1,1:K-1)); continue; end
gama = normrnd(1.96,0.02);
if gama <= 1.92 || gama >= 1.98 || ismember(gama,rmem(2,1:K-1)); continue; end
fi = normrnd(3.76,1.1034);
if fi <= 2.7 || fi >= 16.3 || ismember(fi,rmem(3,1:K-1)); continue; end
rmem(:,K) = [C; gama; fi];
break
end
q=107.25+(100*fi);
dq=1+(.4*tan(fi*pi/180)*(1-sin(fi*pi/180)^2));
Sq=1+(.5*sin(fi*pi/180));
Nq = tan((pi/4)+(pi*fi/360)) * tan((pi/4)+(pi*fi/360)) * exp(pi*tan(fi*pi/180));
Nc = (Nq-1)*cot(fi*pi/180);
Sc=1+(.5* Nq / Nc);
Ngama = 2*(Nq+1)*tan(fi*pi/180);
qult(K)=(1.08*C*Nc*Sc)+(q*Nq*Sq*dq)+(429*Ngama);
end
surf( C(1,1:nsamples), fi(1,1:nsamples), qult );
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
ERROR :
??? Index exceeds matrix dimensions.
Error in ==> graphQ at 27 surf( C(1,1:nsamples), fi(1,1:nsamples), qult );

Antworten (2)

UJJWAL
UJJWAL am 25 Sep. 2011
Hi ,
I don't see any reason as to why thic code should not give an error. C is not a vector. It is a single number(or let us say C is a vector of dimension 1 by 1. In the surf function you are trying to access C(1,1:nsamples) which obviously goes beyond the length of C . So an error must occour.
As far as correction is concerned, it is difficult unless you tell the nature of this code and what it is that you want to do. I defined a vector C1 and stored the individual values of C in it. I did the same for fi . However in that case surf would not work as it will require a 2D matrix in the form of qult to work. I believe that you want to plot qult for corresponding values of C and fi. However for that u need to use Plot3. I used it but I got something weird. So unless the nature of the code is not made clear it is extremely difficult to comment on the nature of the solution.
Happy to Help
  2 Kommentare
Walter Roberson
Walter Roberson am 25 Sep. 2011
Good point about surf() being the wrong call.
Walter Roberson
Walter Roberson am 25 Sep. 2011
UUJWAL is correct: with that flow of code, you will need to store each accepted C and fi value. Just before your q= assignment would seem to be an appropriate location.

Melden Sie sich an, um zu kommentieren.


Vikash Anand
Vikash Anand am 13 Dez. 2021
clc;
clear all;
close all;
n=7;k=4;
num_bit=10000;
genpoly=cyclpoly(n,k,'max');
SNRdB=0:10;
SNR=10.^(SNRdB/10);
for i=1:length(SNR)
msg = randi(num_bit,k,[0,1]);
code=encode(msg,n,k,'cyclic/binary',genpoly);
[row, column]=size(code);
codevec=reshape(code.',1,row*column);
noise=awgn(codevec,SNRdB(i));
y=codevec+noise;
error=0;
for j=1:length(y)
if (y(j)>1&&codevec(j)==0)||(y(j)<0&&codevec(j)==1)
error=error+1;
end
end
error=error/num_bit;
m(i)=error;
end
y(y>0)=1;
y(y<0)=0;
decode_y=decode(y,n,k,'cyclic/binary',genpoly);
decmsg=reshape(decode_y,num_bit,k);
semilogy(SNRdB,m,'r','linewidth',2),grid on;
title(' Bit Error Rate verses SNR for Cyclic Code');
xlabel(' SNR(dB)');
ylabel('BER');

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by