help with while loop

조회 수: 6 (최근 30일)
Andrea
Andrea 2014년 11월 10일
댓글: Geoff Hayes 2014년 11월 10일
Hi, I'm trying to find the maximum value of n for which the error is 0.001875. Im not sure if my code is right. I have to functions. They are related as the output of one is the input of the other. Any suggestions? I keep on getting n=0
n=0;
[CC, ww]=fourier_analysis(step_signal1,1,n);
[ss,tt]=fourier_synthesis(CC,1,1,16384);
while (ER>.001875)
ER=0.3750-mean(ss.^2);
n=n+1;
end
display(n)

답변 (1개)

Geoff Hayes
Geoff Hayes 2014년 11월 10일
Andrea - how is ER initialized? Since you are iterating until a certain condition is met (i.e. ER<=0.001875), your two functions should be within the while loop so that ss changes at each iteration
ER = 1;
maxIters = 100;
n=0;
while (ER>.001875 && n<=maxIters)
[CC, ww]=fourier_analysis(step_signal1,1,n);
[ss,tt]=fourier_synthesis(CC,1,1,16384);
ER=0.3750-mean(ss.^2);
n=n+1;
end
display(n)
Note how the maxIters local variable is used to control the maximum number of iterations of your while loop. We do this so that we do not become stuck in an infinite loop.
  댓글 수: 3
Andrea
Andrea 2014년 11월 10일
Im asking because I changed the value of maxIters and got different answers
Geoff Hayes
Geoff Hayes 2014년 11월 10일
Andrea - you must have chosen something for ER because without it being initialized, you should see an error message similar to
Undefined function or variable 'ER'.
I chose 1 only because it is larger than your threshold.
If you are getting different answers for different maxIters then that is because your ER is not falling below the 0.001875 threshold. You should plot the values of ER from each iteration to see what is happening over time. Are they getting smaller, larger, fluctuating? Do they appear to converge to some number?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by