For loop causing an Infinite loop
Ältere Kommentare anzeigen
I have a problem of infinite loop,please find attached three files of two different function and one text file. And find below what I'm trying to pass througth the two functions,( the problem happened at the last line below)
fid=fopen('elcentro.dat.txt.','r');
text=textscan(fid,'%f %f');
fclose(fid);
time=text{1,1};
dt=time(2)-time(1);
xgtt=text{1,2};
Tsoft=1560;
Tspectra=logspace(log10(0.02),log10(50),1560)';
ksi1=0;
ksi2=0.1;
ksi3=0.2;
u0=0;
ut0=0;
maxxgtt=max(abs(xgtt));
[~,~,~,maxxgt,~]=LERS(dt,xgtt,Tsoft,0)
[PSa1,PSv1,Sd1,Sv1,Sa1]=LERS(dt,xgtt,Tspectra,ksi1);
4 Kommentare
Rik
am 18 Nov. 2018
Please don't delete a question and re-post it.
Although the code you've attached here is slightly different, I still notice NewmarksE doesn't use omegaj, so each iteration should still be the same. The mlint will have given you a warning inside that function.
Image Analyst
am 18 Nov. 2018
I do not see any "for" loop in your code:
fid=fopen('elcentro.dat.txt.','r');
text=textscan(fid,'%f %f');
fclose(fid);
time=text{1,1};
dt=time(2)-time(1);
xgtt=text{1,2};
Tsoft=1560;
Tspectra=logspace(log10(0.02),log10(50),1560)';
ksi1=0;
ksi2=0.1;
ksi3=0.2;
u0=0;
ut0=0;
maxxgtt=max(abs(xgtt));
[~,~,~,maxxgt,~]=LERS(dt,xgtt,Tsoft,0)
[PSa1,PSv1,Sd1,Sv1,Sa1]=LERS(dt,xgtt,Tspectra,ksi1);
Where is it? The "last line" you referred to is a function call, not a for loop.
@Image Analyst: The former attached files were deleted by the OP during the last edit.
Guillaume
am 18 Nov. 2018
@Ahmad, why should we waste time trying to help you if you're going to close or delete every question that you ask?
Antworten (1)
Guillaume
am 18 Nov. 2018
0 Stimmen
There are only two loops in your code, one with length(T) steps, one with numel(F)-1 steps. These values are guaranteed to be finite, thus your loops are guaranteed to finish eventually. If length(T) or numel(F) are very large and if the operations inside the loops are very slow, it could of course take a very long time.
The best way for you to understand what is happening is to debug your code. With the debugger, step through it one line at a time. Look at the result of each line and check that it conforms to your expectation. Most likely, you've made a mistake somewhere and some value is much larger than you expected.
If not, and your code work as expected then (and only then) profile your code to find out where it's slow.No point in profiling your code if it has bugs though.
Kategorien
Mehr zu Programming finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!