Iterations are taking too long

1 次查看(过去 30 天)
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 个评论
Sara
Sara 2014-6-24
Use the profiler and find the bottleneck
Adam
Adam 2014-6-24
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.

请先登录,再进行评论。

采纳的回答

Titus Edelhofer
Titus Edelhofer 2014-6-25
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

更多回答(2 个)

Marisa Micallef
Marisa Micallef 2014-6-25
Ok, so a a brief of how my code looks like is as follows:
z = (0 : -dz : -d); %Depth vector (m)
time_vector = (0 : dt : time); %Time vector (s)
nt = fix((time_vector(length(time_vector))- time_vector(1))/dt) %Time iterations
nz = -fix((z(length(z)) - z(1))/dz) ; %Depth iterations
zeta = zeros(n,nt);
zeta_dot = zeros(n,nt);
zeta_double = zeros(n,nt);
dF_D = zeros(1,nz);
dF_I = zeros(1,nz);
F_hydrodynamic1 = zeros(1,nt);
if strcmp(wave_type,'4') && strcmp(wave_classification,'1') %conditions for deep Stoke's second order waves
for i = 1:nt
for j = 1:nz
U(i,j) = c1.*exp(k.*((j-1)*-dz)).*cos(((k*zeta(1,i))) +(-omega.*((i+1)*dt)));
a1(i,j) = c4 .* exp(k.*((j-1)*-dz)).*sin(((k*zeta(1,i))) -(omega.*((i+1)*dt)));
dF_D(i+1,j) = drag1* (U(i,j)- zeta_dot(1,i) ) * sqrt((U(i,j) - zeta_dot(1,i))^2);
dF_I(i+1,j) = inertia1* (d0_hull^2) * a1(i,j) ;
end
... %Solving for 5 other variables
%Newmark integration scheme
F(:,i+1) = [F_hydrodynamic1(i+1); F_hydrodynamic3(i+1) ; F_hydrodynamic5(i+1)] + ...M*(a0.*zeta(:,i) + a2*(zeta_dot(:,i)) + a3*(zeta_double(:,i)));
zeta(:,i+1) = inv(K_hat)*F(:,i+1);
zeta_double(:,i+1) = a0*(zeta(:,i+1) - zeta(:,i)) - (a2*zeta_dot(:,i)) - (a3*zeta_double(:,i));
zeta_dot(:,i+1) = zeta_dot(:,i) + a6*zeta_double(:,i) + a7*zeta_double(:,i+1);
end
The above is repeated for 3 other if conditions.
Output:
vector = [time_vector((nt/(12/11)):nt)', zeta(1,((nt/(12/11)):nt))'];
xlswrite('surge.xls', vector);
vector2 = [time_vector((nt/(12/11)):nt)', zeta3(2,((nt/(12/11)):nt))'];
xlswrite('heave.xls', vector2);
vector3 = [time_vector((nt/(12/11)):nt)', zeta(3,((nt/(12/11)):nt))'];
xlswrite('pitch.xls', vector3);
figure;
plot (time_vector((nt/1.2):nt),zeta(1,((nt/1.2):nt)), 'r')
xlabel ('Time in seconds');
ylabel ('Surge displacement in metres');
title('\it{Newmark - Surge displacement vs. Time}','FontSize',16)
Thanks !

Marisa Micallef
Marisa Micallef 2014-6-25
Thanks for the reply!
Thank you for noting that I have missed some sizing there. cos(k*zeta) mathematically is not independent of j so i cannot pull it out of the loop unfortunately.
What happens exactly by changing (i,j) to (i,:) please?
Thanks!
  1 个评论
Titus Edelhofer
Titus Edelhofer 2014-6-26
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Scripts 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by