Iterations are taking too long
1 view (last 30 days)
Show older comments
Marisa Micallef
on 24 Jun 2014
Commented: Titus Edelhofer
on 26 Jun 2014
Hi,
My coding is taking too long to solve and I think that I have a lot of improvements which I can make. I am also a beginner in MATLAB and thus I require some help.
Basically I have a code that solves the motion of 3 DOF system. Similar to a mass on a spring. I have two for loops where one iterates for the force and the other iterates for the time response. I am solving the time response using the Newmark integration scheme. Now after that all iterations are done, the code requires that the results are written to an excel sheet for the three degrees of freedom together with plotting of graphs of the corresponding displacements.
I am using xlswrite for each degree of freedom meaning I have 3 xlswrite commands.
Are there any suggestions on how I can improve my computational time?
Thanks!
3 Comments
Adam
on 24 Jun 2014
Using the Matlab profiler would be a good start point.
In its simplest form you can just put
profile on
...Your code...
profile off
profile viewer
to give you a run down of where in your code the time is being spent.
Then, in response to that there are many things you can possibly do, but until you find out where the time is actually being spent you may spend time optimising a piece of code that is only taking 0.1% of the total time.
Accepted Answer
Titus Edelhofer
on 25 Jun 2014
Hi,
two things to start with:
- I'm missing the initialization of U: add U = zeros(nt, nz) before entering the loop (same for a, F?)
- to me it looks as if quite some code can be vectorized (in the space direction, i.e., the loop over j):
j = 0:(nz-1);
U(i,:) = c1.*exp(-k.*dz.*j).*cos(k*zeta(1,i) +(-omega.*((i+1)*dt)));
Or at least pull the cos(k*zeta...) out of the loop over j, since it's independent of j.
Hope this helps getting started.
Titus
0 Comments
More Answers (2)
Marisa Micallef
on 25 Jun 2014
1 Comment
Titus Edelhofer
on 26 Jun 2014
Hi,
U(i,j) means reading (or assigning) a single entry in the matrix at row i and column j. U(i,:) means reading (or assigning) a vector, namely the i-th row.
Titus
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!