For loop causing an Infinite loop
显示 更早的评论
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 个评论
Rik
2018-11-18
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
2018-11-18
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
2018-11-18
@Ahmad, why should we waste time trying to help you if you're going to close or delete every question that you ask?
回答(1 个)
Guillaume
2018-11-18
0 个投票
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!