Simple Code taking too much time
41 vues (au cours des 30 derniers jours)
the code below is part of a large program and is looped through about 50 times total. I haven't worked on this code in a while but it was running fine. I made some very slight changes to other parts of the program and now it is taking too much time to run (it used to take around 2 hours for the 50 loops, now I leave it for around 16 hours and its is still in the 40t loop). below is the part of the code which is taking more than 99% of the run time. I used profiler and the line between asteriks is the one taking a very long time to run although it is a simple addition. this starts happening after the 20th loop roughly (even if I start at the 20th loop, I still have the issue).
if (abs(sqrt(Tdist)/tXD) < 8)
Any ideas as to what could be causing such delay in such a simple step. dS is already preallocated. I'm not an expert in MATLAB but id doesn't make sense for a simple addition step to take so long while more complicated steps are executed quicker.
Guru le 3 Juil 2013
This simply looks to be a case where you are trying to make some result converge to an answer, and the system is not set up to converge nicely with the given settings.
The issue that you have is not a result of the code that is asterisked takes very long to run, it's that you have a nested while loop where the adjustments you are making simply do not converge to anything. MATLAB however will still try to optimize it, and to handle this problem you should use the optimization functions within the Optimization toolbox rather than try to use your own optimization routines unless you build in some kind of metric that detects there is no solution that it can converge to.