Code takes forever to run and will never fully execute? Any tips to get it to run faster?
15 views (last 30 days)
Show older comments
I have a code that solves a matrix of 11 variables using \ operator and when it enters this section of the code it never outputs anything. It is quite frustrating and I was wondering if I could get some tips, advice, or any help in resolving this issue? I want to output the solved matrix for 0 to 360 degrees. Thank you!!
P.S. the file "proj3" is attached.
5 Comments
Walter Roberson
on 6 Nov 2017
You have
while abs(DTheta4) > .01*(pi/180) & abs(DR5) > .00001
In order for that to make any progress, one of DTheta4 or DR5 must be updated inside the loop, and must be updated in such a way that eventually the loop could end.
When I traced through how DTheta4 was changed in the loop, I see it depends upon MAT_CB and mat_AB, so the question then becomes whether EpsXB or EpsYB or theta5 or R4 or theta4 can change in the loop. When I started tracing those, I see that theta5 and theta4 are assigned constants at the end of the loop. EpsXB is based on theta2 and theta4 and theta5 and R2 and R4, and my tracing shows that theta2 and theta4 are set to constants. I traced some of the other variables and they either did not change or were set to constants.
I got tired of doing the tracing. Which of those variables that are used to control the loop are you expecting might change, and have you traced your code to verify that at least one of them does change in a way that is certain to be convergent?
It looked to me, when I tested, as if you had a bistable loop, that one of the control values was flipping between two values, with no reason to expect that the calculation would converge.
Answers (1)
per isakson
on 5 Nov 2017
Edited: per isakson
on 9 Nov 2017
The profiler, which you call in the beginning of the script, tells you loud and clear to preallocate sevaral variables. See https://se.mathworks.com/help/matlab/matlab_prog/preallocating-arrays.html
205 The variable 'INPUT_ANGLES' appears to change size on every loop iteration. Consider preallocating for speed.
209 The variable 'THETA3_result' appears to change size on every loop iteration. Consider preallocating for speed.
210 The variable 'R31_result' appears to change size on every loop iteration. Consider preallocating for speed.
211 The variable 'THETA2_inputs' appears to change size on every loop iteration. Consider preallocating for speed.
212 The variable 'MAT_AB_1_result' appears to change size on every loop iteration. Consider preallocating for speed.
214 The variable 'Theta3_PRIME' appears to change size on every loop iteration. Consider preallocating for speed.
215 The variable 'R31_PRIME' appears to change size on every loop iteration. Consider preallocating for speed.
217 The variable 'Theta3_PRIME2' appears to change size on every loop iteration. Consider preallocating for speed.
218 The variable 'R31_PRIME2' appears to change size on every loop iteration. Consider preallocating for speed.
220 The variable 'OMEGA3' appears to change size on every loop iteration. Consider preallocating for speed.
221 The variable 'ALPHA3' appears to change size on every loop iteration. Consider preallocating for speed.
222 The variable 'VEL_C' appears to change size on every loop iteration. Consider preallocating for speed.
223 The variable 'ACC_C' appears to change size on every loop iteration. Consider preallocating for speed.
The Code Analyzer, which signals with the orange strips at the left of the code, says the same thing:
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!