The infinity while loop

조회 수: 4 (최근 30일)
Alex Phang
Alex Phang 2018년 1월 11일
답변: Steven Lord 2018년 1월 11일
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;

채택된 답변

Steven Lord
Steven Lord 2018년 1월 11일
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.)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Just for fun에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by