Code takes forever to run and will never fully execute? Any tips to get it to run faster?

15 views (last 30 days)
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
Mark
Mark on 6 Nov 2017
So basically I have three for loops in this code. The first two calculate values for angles and distances of mechanism vectors for an input angle (theta2) variation from 0 to 360 degrees. These first two loops then output arrays with 360 values for each variable. Basically I am trying to output the solved matrix for each input variation from 0 to 360 which is the purpose of putting the last loop. I want to output 11 transposed arrays showing what each variable is at each input angle posture. I put the third loop because I couldn't put it in the previous two because i reference the outputs of those loops in the third one. Thanks.
Walter Roberson
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.

Sign in to comment.

Answers (1)

per isakson
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:

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!