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

José-Luis
José-Luis 2014년 6월 24일
Lots of people might be able to help you if you show your code. Otherwise, we are limited to guessing.
One could just say: "Try a faster computer", for example.
Sara
Sara 2014년 6월 24일
Use the profiler and find the bottleneck
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일

0 개 추천

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일

0 개 추천

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일

0 개 추천

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

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by