The infinity while loop

1 visualización (últimos 30 días)
Alex Phang
Alex Phang el 11 de En. de 2018
Respondida: Steven Lord el 11 de En. de 2018
The codes below are having an infinity loop. If I change the while function to if function, it only give one value. What should I change to get a results to plot a graph?
q=1.6e-19; % Charge of Electron
Ib=202e-6; % Background Noise Current + Interference
N0=2*q*Ib; % Noise Spectral Density
R=1; % Photodetector responsitivity
Rb=1e6; % Bit rate
Tb=1/Rb; % Bit duration
sig_length=ceil(20e-3/Tb); % Number of bits
nsamp=10; % Samples per symbols
Tsamp=Tb/nsamp; % Sampling time
EbN0_db=30; % Signal-to-noise ratio in dB
BER=1; % Initializing ber
index=1;
maxerr=30; % Maximum error per simulation
while (BER>1E-4)
terr=0; % Total error
tsym=0; % Total bits
SNR=10.^(EbN0_db./10); % Signal-to-noise ratio
P_avg=sqrt(N0*Rb*SNR/(2*R^2)); % Average transmitted optical power
end;

Respuesta aceptada

Steven Lord
Steven Lord el 11 de En. de 2018
Nowhere inside your while loop do you change BER. That means if the while condition is satisfied initially, it will always be satisfied and so your while loop will never end. You need to change BER inside your loop.
While P_avg depends upon SNR which is computed inside the loop, SNR only depends on values that don't change inside the loop so P_avg doesn't actually change. So defining BER in terms of SNR and P_avg won't work to change it for purposes of the while condition.
As an analogy, if you said "While I'm not home, I'm going to walk forward one step from work" you'll never get home (unless you work at home.) If you said "While I'm not home, I'm going to walk forward one step from where I am now" you will probably eventually get home (assuming you're facing in the right direction.)

Más respuestas (0)

Categorías

Más información sobre Just for fun en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by