help with while loop
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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)
0 Kommentare
Antworten (1)
Geoff Hayes
am 10 Nov. 2014
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 Kommentare
Geoff Hayes
am 10 Nov. 2014
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?
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!